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:
commit
8db3fefac0
3 changed files with 19 additions and 4 deletions
|
@ -207,6 +207,8 @@ module ActiveRecord
|
||||||
# methods in ActiveModel::Type::Value for more details.
|
# methods in ActiveModel::Type::Value for more details.
|
||||||
def attribute(name, cast_type = nil, default: NO_DEFAULT_PROVIDED, **options)
|
def attribute(name, cast_type = nil, default: NO_DEFAULT_PROVIDED, **options)
|
||||||
name = name.to_s
|
name = name.to_s
|
||||||
|
name = attribute_aliases[name] || name
|
||||||
|
|
||||||
reload_schema_from_cache
|
reload_schema_from_cache
|
||||||
|
|
||||||
case cast_type
|
case cast_type
|
||||||
|
|
|
@ -186,11 +186,9 @@ module ActiveRecord
|
||||||
detect_enum_conflict!(name, name)
|
detect_enum_conflict!(name, name)
|
||||||
detect_enum_conflict!(name, "#{name}=")
|
detect_enum_conflict!(name, "#{name}=")
|
||||||
|
|
||||||
attr = attribute_alias?(name) ? attribute_alias(name) : name
|
attribute(name, **options) do |subtype|
|
||||||
|
|
||||||
attribute(attr, **options) do |subtype|
|
|
||||||
subtype = subtype.subtype if EnumType === subtype
|
subtype = subtype.subtype if EnumType === subtype
|
||||||
EnumType.new(attr, enum_values, subtype)
|
EnumType.new(name, enum_values, subtype)
|
||||||
end
|
end
|
||||||
|
|
||||||
value_method_names = []
|
value_method_names = []
|
||||||
|
|
|
@ -41,6 +41,21 @@ class SerializedAttributeTest < ActiveRecord::TestCase
|
||||||
assert_equal(myobj, topic.content)
|
assert_equal(myobj, topic.content)
|
||||||
end
|
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
|
def test_serialized_attribute_with_default
|
||||||
klass = Class.new(ActiveRecord::Base) do
|
klass = Class.new(ActiveRecord::Base) do
|
||||||
self.table_name = Topic.table_name
|
self.table_name = Topic.table_name
|
||||||
|
|
Loading…
Reference in a new issue