diff options
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/attribute.rb | 18 | ||||
-rw-r--r-- | activerecord/lib/active_record/attribute_methods/write.rb | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/attribute_set.rb | 4 |
3 files changed, 23 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/attribute.rb b/activerecord/lib/active_record/attribute.rb index 8cc1904575..88536eaac0 100644 --- a/activerecord/lib/active_record/attribute.rb +++ b/activerecord/lib/active_record/attribute.rb @@ -9,6 +9,10 @@ module ActiveRecord FromUser.new(name, value, type) end + def with_cast_value(name, value, type) + WithCastValue.new(name, value, type) + end + def null(name) Null.new(name) end @@ -58,6 +62,10 @@ module ActiveRecord self.class.from_database(name, value, type) end + def with_cast_value(value) + self.class.with_cast_value(name, value, type) + end + def type_cast(*) raise NotImplementedError end @@ -93,6 +101,16 @@ module ActiveRecord end end + class WithCastValue < Attribute # :nodoc: + def type_cast(value) + value + end + + def changed_in_place_from?(old_value) + false + end + end + class Null < Attribute # :nodoc: def initialize(name) super(name, nil, Type::Value.new) diff --git a/activerecord/lib/active_record/attribute_methods/write.rb b/activerecord/lib/active_record/attribute_methods/write.rb index b3c8209a74..16804f86bf 100644 --- a/activerecord/lib/active_record/attribute_methods/write.rb +++ b/activerecord/lib/active_record/attribute_methods/write.rb @@ -73,7 +73,7 @@ module ActiveRecord if should_type_cast @attributes.write_from_user(attr_name, value) else - @attributes.write_from_database(attr_name, value) + @attributes.write_cast_value(attr_name, value) end value diff --git a/activerecord/lib/active_record/attribute_set.rb b/activerecord/lib/active_record/attribute_set.rb index 6b1d7ea79e..66fcaf6945 100644 --- a/activerecord/lib/active_record/attribute_set.rb +++ b/activerecord/lib/active_record/attribute_set.rb @@ -39,6 +39,10 @@ module ActiveRecord attributes[name] = self[name].with_value_from_user(value) end + def write_cast_value(name, value) + attributes[name] = self[name].with_cast_value(value) + end + def freeze @attributes.freeze super |