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

Add back decorate_attribute_type

Originally #39882 is intended to remove `decorate_matching_attribute_type`
but leave `decorate_attribute_type` at that point, but in the
disscussion in #39882, to demonstrate removing `AttributeDecorators`
module, I've removed all decoration methods.

I wrote user-defined type decoration as inline, I wasn't sure that is
valid (meaningful) usage (that is a reason why I wasn't in a hurry to
remove that method), as I said at https://github.com/rails/rails/pull/39882#discussion_r457750898.

In #39897 and #39902, `enum` also need to support user-defined type
decoration, so I've decided to add back `decorate_attribute_type` as
before #39882.
This commit is contained in:
Ryuta Kamizono 2020-07-27 14:16:56 +09:00
parent 70c9f39039
commit 75c309c7ad
3 changed files with 14 additions and 14 deletions

View file

@ -69,14 +69,7 @@ module ActiveRecord
Coders::YAMLColumn.new(attr_name, class_name_or_coder)
end
attr_name = attr_name.to_s
type, options = attributes_to_define_after_schema_loads[attr_name]
attribute(attr_name) do |cast_type|
if type && !type.is_a?(Proc)
cast_type = _lookup_cast_type(attr_name, type, options)
end
decorate_attribute_type(attr_name.to_s) do |cast_type|
if type_incompatible_with_serialize?(cast_type, class_name_or_coder)
raise ColumnNotSerializableError.new(attr_name, cast_type)
end

View file

@ -270,6 +270,18 @@ module ActiveRecord
_default_attributes[name] = default_attribute
end
def decorate_attribute_type(attr_name, **default)
type, options = attributes_to_define_after_schema_loads[attr_name]
attribute(attr_name, **default) do |cast_type|
if type && !type.is_a?(Proc)
cast_type = _lookup_cast_type(attr_name, type, options)
end
yield cast_type
end
end
def _lookup_cast_type(name, type, options)
case type
when Symbol

View file

@ -183,13 +183,8 @@ module ActiveRecord
detect_enum_conflict!(name, "#{name}=")
attr = attribute_alias?(name) ? attribute_alias(name) : name
type, options = attributes_to_define_after_schema_loads[attr]
attribute(attr, **default) do |subtype|
if type && !type.is_a?(Proc)
subtype = _lookup_cast_type(attr, type, options)
end
decorate_attribute_type(attr, **default) do |subtype|
EnumType.new(attr, enum_values, subtype)
end