Merge remote branch 'Fingertips/master'
This commit is contained in:
commit
f8877d4b2a
@ -963,7 +963,7 @@ def initialize(object_name, object, template, options, proc)
|
||||
end
|
||||
end
|
||||
|
||||
(field_helpers - %w(label check_box radio_button fields_for)).each do |selector|
|
||||
(field_helpers - %w(label check_box radio_button fields_for hidden_field)).each do |selector|
|
||||
src = <<-end_src
|
||||
def #{selector}(method, options = {}) # def text_field(method, options = {})
|
||||
@template.send( # @template.send(
|
||||
@ -1022,6 +1022,11 @@ def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
|
||||
def radio_button(method, tag_value, options = {})
|
||||
@template.radio_button(@object_name, method, tag_value, objectify_options(options))
|
||||
end
|
||||
|
||||
def hidden_field(method, options = {})
|
||||
@emitted_hidden_id = true if method == :id
|
||||
@template.hidden_field(@object_name, method, objectify_options(options))
|
||||
end
|
||||
|
||||
def error_message_on(method, *args)
|
||||
@template.error_message_on(@object, method, *args)
|
||||
@ -1035,6 +1040,10 @@ def submit(value = "Save changes", options = {})
|
||||
@template.submit_tag(value, options.reverse_merge(:id => "#{object_name}_submit"))
|
||||
end
|
||||
|
||||
def emitted_hidden_id?
|
||||
@emitted_hidden_id
|
||||
end
|
||||
|
||||
private
|
||||
def objectify_options(options)
|
||||
@default_options.merge(options.merge(:object => @object))
|
||||
@ -1069,8 +1078,8 @@ def fields_for_nested_model(name, object, args, block)
|
||||
@template.fields_for(name, object, *args, &block)
|
||||
else
|
||||
@template.fields_for(name, object, *args) do |builder|
|
||||
@template.concat builder.hidden_field(:id)
|
||||
block.call(builder)
|
||||
@template.concat builder.hidden_field(:id) unless builder.emitted_hidden_id?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -611,6 +611,26 @@ def test_nested_fields_for_with_an_existing_record_on_a_nested_attributes_one_to
|
||||
end
|
||||
end
|
||||
|
||||
expected = '<form action="http://www.example.com" method="post">' +
|
||||
'<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
|
||||
'<input id="post_author_attributes_name" name="post[author_attributes][name]" size="30" type="text" value="author #321" />' +
|
||||
'<input id="post_author_attributes_id" name="post[author_attributes][id]" type="hidden" value="321" />' +
|
||||
'</form>'
|
||||
|
||||
assert_dom_equal expected, output_buffer
|
||||
end
|
||||
|
||||
def test_nested_fields_for_with_existing_records_on_a_nested_attributes_one_to_one_association_with_explicit_hidden_field_placement
|
||||
@post.author = Author.new(321)
|
||||
|
||||
form_for(:post, @post) do |f|
|
||||
concat f.text_field(:title)
|
||||
f.fields_for(:author) do |af|
|
||||
concat af.hidden_field(:id)
|
||||
concat af.text_field(:name)
|
||||
end
|
||||
end
|
||||
|
||||
expected = '<form action="http://www.example.com" method="post">' +
|
||||
'<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
|
||||
'<input id="post_author_attributes_id" name="post[author_attributes][id]" type="hidden" value="321" />' +
|
||||
@ -632,6 +652,30 @@ def test_nested_fields_for_with_existing_records_on_a_nested_attributes_collecti
|
||||
end
|
||||
end
|
||||
|
||||
expected = '<form action="http://www.example.com" method="post">' +
|
||||
'<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
|
||||
'<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #1" />' +
|
||||
'<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="1" />' +
|
||||
'<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="comment #2" />' +
|
||||
'<input id="post_comments_attributes_1_id" name="post[comments_attributes][1][id]" type="hidden" value="2" />' +
|
||||
'</form>'
|
||||
|
||||
assert_dom_equal expected, output_buffer
|
||||
end
|
||||
|
||||
def test_nested_fields_for_with_existing_records_on_a_nested_attributes_collection_association_with_explicit_hidden_field_placement
|
||||
@post.comments = Array.new(2) { |id| Comment.new(id + 1) }
|
||||
|
||||
form_for(:post, @post) do |f|
|
||||
concat f.text_field(:title)
|
||||
@post.comments.each do |comment|
|
||||
f.fields_for(:comments, comment) do |cf|
|
||||
concat cf.hidden_field(:id)
|
||||
concat cf.text_field(:name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
expected = '<form action="http://www.example.com" method="post">' +
|
||||
'<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
|
||||
'<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="1" />' +
|
||||
@ -678,8 +722,8 @@ def test_nested_fields_for_with_existing_and_new_records_on_a_nested_attributes_
|
||||
|
||||
expected = '<form action="http://www.example.com" method="post">' +
|
||||
'<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
|
||||
'<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="321" />' +
|
||||
'<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #321" />' +
|
||||
'<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="321" />' +
|
||||
'<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="new comment" />' +
|
||||
'</form>'
|
||||
|
||||
@ -713,10 +757,10 @@ def test_nested_fields_for_with_existing_records_on_a_supplied_nested_attributes
|
||||
|
||||
expected = '<form action="http://www.example.com" method="post">' +
|
||||
'<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
|
||||
'<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="1" />' +
|
||||
'<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #1" />' +
|
||||
'<input id="post_comments_attributes_1_id" name="post[comments_attributes][1][id]" type="hidden" value="2" />' +
|
||||
'<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="1" />' +
|
||||
'<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="comment #2" />' +
|
||||
'<input id="post_comments_attributes_1_id" name="post[comments_attributes][1][id]" type="hidden" value="2" />' +
|
||||
'</form>'
|
||||
|
||||
assert_dom_equal expected, output_buffer
|
||||
@ -736,8 +780,8 @@ def test_nested_fields_for_on_a_nested_attributes_collection_association_yields_
|
||||
|
||||
expected = '<form action="http://www.example.com" method="post">' +
|
||||
'<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
|
||||
'<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="321" />' +
|
||||
'<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #321" />' +
|
||||
'<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="321" />' +
|
||||
'<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="new comment" />' +
|
||||
'</form>'
|
||||
|
||||
@ -755,8 +799,8 @@ def test_nested_fields_for_with_child_index_option_override_on_a_nested_attribut
|
||||
end
|
||||
|
||||
expected = '<form action="http://www.example.com" method="post">' +
|
||||
'<input id="post_comments_attributes_abc_id" name="post[comments_attributes][abc][id]" type="hidden" value="321" />' +
|
||||
'<input id="post_comments_attributes_abc_name" name="post[comments_attributes][abc][name]" size="30" type="text" value="comment #321" />' +
|
||||
'<input id="post_comments_attributes_abc_id" name="post[comments_attributes][abc][id]" type="hidden" value="321" />' +
|
||||
'</form>'
|
||||
|
||||
assert_dom_equal expected, output_buffer
|
||||
@ -790,18 +834,18 @@ def test_nested_fields_uses_unique_indices_for_different_collection_associations
|
||||
end
|
||||
|
||||
expected = '<form action="http://www.example.com" method="post">' +
|
||||
'<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="321" />' +
|
||||
'<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #321" />' +
|
||||
'<input id="post_comments_attributes_0_relevances_attributes_0_id" name="post[comments_attributes][0][relevances_attributes][0][id]" type="hidden" value="314" />' +
|
||||
'<input id="post_comments_attributes_0_relevances_attributes_0_value" name="post[comments_attributes][0][relevances_attributes][0][value]" size="30" type="text" value="commentrelevance #314" />' +
|
||||
'<input id="post_tags_attributes_0_id" name="post[tags_attributes][0][id]" type="hidden" value="123" />' +
|
||||
'<input id="post_comments_attributes_0_relevances_attributes_0_id" name="post[comments_attributes][0][relevances_attributes][0][id]" type="hidden" value="314" />' +
|
||||
'<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="321" />' +
|
||||
'<input id="post_tags_attributes_0_value" name="post[tags_attributes][0][value]" size="30" type="text" value="tag #123" />' +
|
||||
'<input id="post_tags_attributes_0_relevances_attributes_0_id" name="post[tags_attributes][0][relevances_attributes][0][id]" type="hidden" value="3141" />' +
|
||||
'<input id="post_tags_attributes_0_relevances_attributes_0_value" name="post[tags_attributes][0][relevances_attributes][0][value]" size="30" type="text" value="tagrelevance #3141" />' +
|
||||
'<input id="post_tags_attributes_1_id" name="post[tags_attributes][1][id]" type="hidden" value="456" />' +
|
||||
'<input id="post_tags_attributes_0_relevances_attributes_0_id" name="post[tags_attributes][0][relevances_attributes][0][id]" type="hidden" value="3141" />' +
|
||||
'<input id="post_tags_attributes_0_id" name="post[tags_attributes][0][id]" type="hidden" value="123" />' +
|
||||
'<input id="post_tags_attributes_1_value" name="post[tags_attributes][1][value]" size="30" type="text" value="tag #456" />' +
|
||||
'<input id="post_tags_attributes_1_relevances_attributes_0_id" name="post[tags_attributes][1][relevances_attributes][0][id]" type="hidden" value="31415" />' +
|
||||
'<input id="post_tags_attributes_1_relevances_attributes_0_value" name="post[tags_attributes][1][relevances_attributes][0][value]" size="30" type="text" value="tagrelevance #31415" />' +
|
||||
'<input id="post_tags_attributes_1_relevances_attributes_0_id" name="post[tags_attributes][1][relevances_attributes][0][id]" type="hidden" value="31415" />' +
|
||||
'<input id="post_tags_attributes_1_id" name="post[tags_attributes][1][id]" type="hidden" value="456" />' +
|
||||
'</form>'
|
||||
|
||||
assert_dom_equal expected, output_buffer
|
||||
|
Loading…
Reference in New Issue
Block a user