Permalink
Browse files

Merge pull request #798 from mhuggins/indexed-labels

Pass current index as arguments to inputs block for nested inputs
  • Loading branch information...
2 parents 8e26afb + d5aa7fb commit 82b552d03b71a8c64e7d86033f6a958dd085d5fe @justinfrench committed Feb 14, 2012
Showing with 57 additions and 3 deletions.
  1. +15 −0 README.textile
  2. +4 −1 lib/formtastic/helpers/inputs_helper.rb
  3. +38 −2 spec/helpers/inputs_helper_spec.rb
View
@@ -238,6 +238,21 @@ When working in has many association, you can even supply @"%i"@ in your fieldse
<% end %>
</pre>
+Alternatively, the current index can be accessed via the `inputs` block's arguments for use anywhere:
+
+<pre>
+ <%= semantic_form_for @post do |f| %>
+ <%= f.inputs :for => :categories do |category, i| %>
+ <% if i <= 2 %>
+ <%= f.inputs :name => "Category ##{i}" %>
+ <% else %>
+ <%= f.inputs :name => "Category ##{i} (optional)" %>
+ <% end %>
+ <% end %>
+ <%= f.actions %>
+ <% end %>
+</pre>
+
If you have more than one form on the same page, it may lead to HTML invalidation because of the way HTML element id attributes are assigned. You can provide a namespace for your form to ensure uniqueness of id attributes on form elements. The namespace attribute will be prefixed with underscore on the generate HTML id. For example:
<pre>
@@ -370,7 +370,10 @@ def inputs_for_nested_attributes(*args, &block) #:nodoc:
raise ArgumentError, 'You gave :for option with a block to inputs method, ' <<
'but the block does not accept any argument.' if block.arity <= 0
lambda do |f|
- contents = f.inputs(*args){ block.call(f) }
+ contents = f.inputs(*args) do
+ index = parent_child_index(options[:parent]) if options[:parent]
+ block.call(f, index)
+ end
template.concat(contents)
end
else
@@ -122,6 +122,18 @@
output_buffer.should have_tag("form input[@name='post[authors_attributes][1][login]']")
output_buffer.should_not have_tag('form fieldset[@name]')
end
+
+ it 'should include an indexed :label input for each item' do
+ concat(semantic_form_for(@new_post) do |post|
+ post.inputs :for => :authors do |author, index|
+ concat(author.input(:login, :label => "#{index}", :required => false))
+ end
+ end)
+
+ output_buffer.should have_tag("form fieldset.inputs label", "1", :count => 1)
+ output_buffer.should have_tag("form fieldset.inputs label", "2", :count => 1)
+ output_buffer.should_not have_tag('form fieldset legend')
+ end
end
describe 'as an array containing the a symbole for the association name and the associated object' do
@@ -187,7 +199,7 @@
output_buffer.should_not have_tag('fieldset[@builder="Formtastic::Helpers::FormHelper"]')
end
- it 'should send parent_builder as an option to allow child index interpolation' do
+ it 'should send parent_builder as an option to allow child index interpolation for legends' do
concat(semantic_form_for(@new_post) do |builder|
builder.instance_variable_set('@nested_child_index', 0)
inputs = builder.inputs :for => [:author, @bob], :name => 'Author #%i' do |bob_builder|
@@ -199,7 +211,7 @@
output_buffer.should have_tag('fieldset legend', 'Author #1')
end
- it 'should also provide child index interpolation when nested child index is a hash' do
+ it 'should also provide child index interpolation for legends when nested child index is a hash' do
concat(semantic_form_for(@new_post) do |builder|
builder.instance_variable_set('@nested_child_index', :author => 10)
inputs = builder.inputs :for => [:author, @bob], :name => 'Author #%i' do |bob_builder|
@@ -210,6 +222,30 @@
output_buffer.should have_tag('fieldset legend', 'Author #11')
end
+
+ it 'should send parent_builder as an option to allow child index interpolation for labels' do
+ concat(semantic_form_for(@new_post) do |builder|
+ builder.instance_variable_set('@nested_child_index', 'post[author_attributes]' => 0)
+ inputs = builder.inputs :for => [:author, @bob] do |bob_builder, index|
+ concat(bob_builder.input(:name, :label => "Author ##{index}", :required => false))
+ end
+ concat(inputs)
+ end)
+
+ output_buffer.should have_tag('fieldset label', 'Author #1')
+ end
+
+ it 'should also provide child index interpolation for labels when nested child index is a hash' do
+ concat(semantic_form_for(@new_post) do |builder|
+ builder.instance_variable_set('@nested_child_index', 'post[author_attributes]' => 10)
+ inputs = builder.inputs :for => [:author, @bob] do |bob_builder, index|
+ concat(bob_builder.input(:name, :label => "Author ##{index}", :required => false))
+ end
+ concat(inputs)
+ end)
+
+ output_buffer.should have_tag('fieldset label', 'Author #11')
+ end
end
describe 'when a :name or :title option is provided' do

0 comments on commit 82b552d

Please sign in to comment.