diff options
author | Sean Griffin <sean@thoughtbot.com> | 2015-01-14 17:08:25 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2015-01-14 17:08:25 -0700 |
commit | d8e710410ea300ec4626250c0b35946cb52bc38c (patch) | |
tree | a844532921c8a62aac2c04822cb419a0cb26506e /actionview/lib/action_view/helpers | |
parent | b67990cf342fb1d8f9ecab532db7e5b884981d7d (diff) | |
download | rails-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 'actionview/lib/action_view/helpers')
-rw-r--r-- | actionview/lib/action_view/helpers/tags/base.rb | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/actionview/lib/action_view/helpers/tags/base.rb b/actionview/lib/action_view/helpers/tags/base.rb index f8abb19698..7740c60eac 100644 --- a/actionview/lib/action_view/helpers/tags/base.rb +++ b/actionview/lib/action_view/helpers/tags/base.rb @@ -32,12 +32,19 @@ module ActionView unless object.nil? method_before_type_cast = @method_name + "_before_type_cast" - object.respond_to?(method_before_type_cast) ? - object.send(method_before_type_cast) : + if value_came_from_user?(object) && object.respond_to?(method_before_type_cast) + object.public_send(method_before_type_cast) + else value(object) + end end end + def value_came_from_user?(object) + method_name = "#{@method_name}_came_from_user?" + !object.respond_to?(method_name) || object.public_send(method_name) + end + def retrieve_object(object) if object object |