aboutsummaryrefslogtreecommitdiffstats
path: root/actionview/test/template/form_helper
diff options
context:
space:
mode:
authorKasper Timm Hansen <kaspth@gmail.com>2016-12-18 20:01:54 +0100
committerKasper Timm Hansen <kaspth@gmail.com>2016-12-18 20:18:47 +0100
commit9f0f7ec2223ae60ee6e2eab5bc9f036a480e9f81 (patch)
treeadd9c1af0690fd0970d77978c21250c448bea7d6 /actionview/test/template/form_helper
parent2f197022e41f07f17301e11927b602a001c71192 (diff)
downloadrails-9f0f7ec2223ae60ee6e2eab5bc9f036a480e9f81.tar.gz
rails-9f0f7ec2223ae60ee6e2eab5bc9f036a480e9f81.tar.bz2
rails-9f0f7ec2223ae60ee6e2eab5bc9f036a480e9f81.zip
form_with: allow methods outside the model.
Has the handy effect of making the initial examples in the form_with docs work too. Had to do some finagling such that form_with's without a scope didn't wrap their names in braces ala `[title]`.
Diffstat (limited to 'actionview/test/template/form_helper')
-rw-r--r--actionview/test/template/form_helper/form_with_test.rb81
1 files changed, 69 insertions, 12 deletions
diff --git a/actionview/test/template/form_helper/form_with_test.rb b/actionview/test/template/form_helper/form_with_test.rb
index c078b47e14..96b797992f 100644
--- a/actionview/test/template/form_helper/form_with_test.rb
+++ b/actionview/test/template/form_helper/form_with_test.rb
@@ -323,6 +323,75 @@ class FormWithActsLikeFormForTest < FormWithTest
assert_dom_equal expected, output_buffer
end
+ def test_form_with_only_url_on_create
+ form_with(url: "/posts") do |f|
+ concat f.label :title, "Label me"
+ concat f.text_field :title
+ end
+
+ expected = whole_form("/posts") do
+ '<label for="title">Label me</label>' +
+ '<input type="text" name="title">'
+ end
+
+ assert_dom_equal expected, output_buffer
+ end
+
+ def test_form_with_only_url_on_update
+ form_with(url: "/posts/123") do |f|
+ concat f.label :title, 'Label me'
+ concat f.text_field :title
+ end
+
+ expected = whole_form("/posts/123") do
+ '<label for="title">Label me</label>' +
+ '<input type="text" name="title">'
+ end
+
+ assert_dom_equal expected, output_buffer
+ end
+
+ def test_form_with_general_attributes
+ form_with(url: "/posts/123") do |f|
+ concat f.text_field :no_model_to_back_this_badboy
+ end
+
+ expected = whole_form("/posts/123") do
+ '<input type="text" name="no_model_to_back_this_badboy">'
+ end
+
+ assert_dom_equal expected, output_buffer
+ end
+
+ def test_form_with_attribute_not_on_model
+ form_with(model: @post) do |f|
+ concat f.text_field :this_dont_exist_on_post
+ end
+
+ expected = whole_form("/posts/123", method: :patch) do
+ '<input type="text" name="post[this_dont_exist_on_post]">'
+ end
+
+ assert_dom_equal expected, output_buffer
+ end
+
+ def test_form_with_doesnt_call_private_or_protected_properties_on_form_object_skipping_value
+ obj = Class.new do
+ private def private_property
+ "That would be great."
+ end
+
+ protected def protected_property
+ "I believe you have my stapler."
+ end
+ end.new
+
+ form_with(model: obj, scope: "other_name", url: "/", id: "edit-other-name") do |f|
+ assert_dom_equal '<input type="hidden" name="other_name[private_property]">', f.hidden_field(:private_property)
+ assert_dom_equal '<input type="hidden" name="other_name[protected_property]">', f.hidden_field(:protected_property)
+ end
+ end
+
def test_form_with_with_collection_radio_buttons
post = Post.new
def post.active; false; end
@@ -599,18 +668,6 @@ class FormWithActsLikeFormForTest < FormWithTest
assert_dom_equal expected, output_buffer
end
- def test_form_tags_do_not_call_private_properties_on_form_object
- obj = Class.new do
- private def private_property
- raise "This method should not be called."
- end
- end.new
-
- form_with(model: obj, scope: "other_name", url: "/", id: "edit-other-name") do |f|
- assert_raise(NoMethodError) { f.hidden_field(:private_property) }
- end
- end
-
def test_form_with_with_method_as_part_of_html_options
form_with(model: @post, url: "/", id: "create-post", html: { method: :delete }) do |f|
concat f.text_field(:title)