aboutsummaryrefslogtreecommitdiffstats
path: root/actionview/test/template
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 /actionview/test/template
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 'actionview/test/template')
-rw-r--r--actionview/test/template/form_helper_test.rb17
1 files changed, 17 insertions, 0 deletions
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(
+ %{<textarea id="post_id" name="post[id]">\n0</textarea>},
+ 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(
+ %{<textarea id="post_id" name="post[id]">\nomg</textarea>},
+ text_area("post", "id")
+ )
+ end
+
def test_text_area_with_html_entities
@post.body = "The HTML Entity for & is &amp;"
assert_dom_equal(