1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #41370 from kamipo/serialized_attribute_on_alias_attribute

Allow `serialize` attribute on `alias_attribute`
This commit is contained in:
Rafael França 2021-02-08 15:08:54 -05:00 committed by GitHub
commit 8db3fefac0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 4 deletions

View file

@ -207,6 +207,8 @@ module ActiveRecord
# 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

View file

@ -186,11 +186,9 @@ module ActiveRecord
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 = []

View file

@ -41,6 +41,21 @@ class SerializedAttributeTest < ActiveRecord::TestCase
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