From d8e710410ea300ec4626250c0b35946cb52bc38c Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Wed, 14 Jan 2015 17:08:25 -0700 Subject: 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. --- actionview/test/template/form_helper_test.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'actionview/test/template') diff --git a/actionview/test/template/form_helper_test.rb b/actionview/test/template/form_helper_test.rb index fa8d11d08b..e4fd797924 100644 --- a/actionview/test/template/form_helper_test.rb +++ b/actionview/test/template/form_helper_test.rb @@ -101,6 +101,7 @@ class FormHelperTest < ActionView::TestCase def @post.to_key; [123]; end def @post.id; 0; end def @post.id_before_type_cast; "omg"; end + def @post.id_came_from_user?; true; end def @post.to_param; '123'; end @post.persisted = true @@ -908,6 +909,22 @@ class FormHelperTest < ActionView::TestCase ) end + def test_inputs_dont_use_before_type_cast_when_value_did_not_come_from_user + def @post.id_came_from_user?; false; end + assert_dom_equal( + %{}, + text_area("post", "id") + ) + end + + def test_inputs_use_before_typecast_when_object_doesnt_respond_to_came_from_user + class << @post; undef id_came_from_user?; end + assert_dom_equal( + %{}, + text_area("post", "id") + ) + end + def test_text_area_with_html_entities @post.body = "The HTML Entity for & is &" assert_dom_equal( -- cgit v1.2.3