aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2015-01-14 17:08:25 -0700
committerSean Griffin <sean@thoughtbot.com>2015-01-14 17:08:25 -0700
commitd8e710410ea300ec4626250c0b35946cb52bc38c (patch)
treea844532921c8a62aac2c04822cb419a0cb26506e /activerecord/lib
parentb67990cf342fb1d8f9ecab532db7e5b884981d7d (diff)
downloadrails-d8e710410ea300ec4626250c0b35946cb52bc38c.tar.gz
rails-d8e710410ea300ec4626250c0b35946cb52bc38c.tar.bz2
rails-d8e710410ea300ec4626250c0b35946cb52bc38c.zip
Only use the `_before_type_cast` in the form when from user input
While we don't want to change the form input when validations fail, blindly using `_before_type_cast` will cause the input to display the wrong data for any type which does additional work on database values.
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/attribute.rb8
-rw-r--r--activerecord/lib/active_record/attribute_methods/before_type_cast.rb5
2 files changed, 13 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/attribute.rb b/activerecord/lib/active_record/attribute.rb
index 88536eaac0..51e4fae62e 100644
--- a/activerecord/lib/active_record/attribute.rb
+++ b/activerecord/lib/active_record/attribute.rb
@@ -74,6 +74,10 @@ module ActiveRecord
true
end
+ def came_from_user?
+ false
+ end
+
def ==(other)
self.class == other.class &&
name == other.name &&
@@ -99,6 +103,10 @@ module ActiveRecord
def type_cast(value)
type.type_cast_from_user(value)
end
+
+ def came_from_user?
+ true
+ end
end
class WithCastValue < Attribute # :nodoc:
diff --git a/activerecord/lib/active_record/attribute_methods/before_type_cast.rb b/activerecord/lib/active_record/attribute_methods/before_type_cast.rb
index fd61febd57..56c1898551 100644
--- a/activerecord/lib/active_record/attribute_methods/before_type_cast.rb
+++ b/activerecord/lib/active_record/attribute_methods/before_type_cast.rb
@@ -28,6 +28,7 @@ module ActiveRecord
included do
attribute_method_suffix "_before_type_cast"
+ attribute_method_suffix "_came_from_user?"
end
# Returns the value of the attribute identified by +attr_name+ before
@@ -66,6 +67,10 @@ module ActiveRecord
def attribute_before_type_cast(attribute_name)
read_attribute_before_type_cast(attribute_name)
end
+
+ def attribute_came_from_user?(attribute_name)
+ @attributes[attribute_name].came_from_user?
+ end
end
end
end