Allow serialize attribute on alias_attribute

Related to #25998.
This commit is contained in:
Ryuta Kamizono 2021-02-08 20:47:19 +09:00
parent 6515d6985d
commit 86fdb54da1
3 changed files with 19 additions and 4 deletions

@ -207,6 +207,8 @@ module ClassMethods
# methods in ActiveModel::Type::Value for more details.
def attribute(name, cast_type = nil, default: NO_DEFAULT_PROVIDED, **options)
name = name.to_s
name = attribute_aliases[name] || name
reload_schema_from_cache
case cast_type

@ -186,11 +186,9 @@ def _enum(name, values, prefix: nil, suffix: nil, scopes: true, **options)
detect_enum_conflict!(name, name)
detect_enum_conflict!(name, "#{name}=")
attr = attribute_alias?(name) ? attribute_alias(name) : name
attribute(attr, **options) do |subtype|
attribute(name, **options) do |subtype|
subtype = subtype.subtype if EnumType === subtype
EnumType.new(attr, enum_values, subtype)
EnumType.new(name, enum_values, subtype)
end
value_method_names = []

@ -41,6 +41,21 @@ def test_serialized_attribute
assert_equal(myobj, topic.content)
end
def test_serialized_attribute_on_alias_attribute
klass = Class.new(ActiveRecord::Base) do
self.table_name = Topic.table_name
alias_attribute :object, :content
serialize :object, MyObject
end
myobj = MyObject.new("value1", "value2")
topic = klass.create!(object: myobj)
assert_equal(myobj, topic.object)
topic.reload
assert_equal(myobj, topic.object)
end
def test_serialized_attribute_with_default
klass = Class.new(ActiveRecord::Base) do
self.table_name = Topic.table_name