Browse files

Merge pull request #773 from mhuggins/indexed-labels

Allow labels to include an index via %i
  • Loading branch information...
2 parents 1df5245 + a78955f commit 5641f43d3ca2494c36b84635b48b78f316103d7c @justinfrench committed Jan 5, 2012
Showing with 55 additions and 3 deletions.
  1. +17 −1 lib/formtastic/inputs/base/labelling.rb
  2. +38 −2 spec/helpers/inputs_helper_spec.rb
View
18 lib/formtastic/inputs/base/labelling.rb
@@ -20,7 +20,9 @@ def label_html_options
end
def label_text
- ((localized_label || humanized_method_name) << requirement_text).html_safe
+ text = ((localized_label || humanized_method_name) << requirement_text)
+ text %= input_index
+ text.html_safe
end
# TODO: why does this need to be memoized in order to make the inputs_spec tests pass?
@@ -49,6 +51,20 @@ def render_label?
true
end
+ def input_index
+ # Try to get parent builder's @nested_child_index Hash, which contains the current
+ # index of the form element we want to look up. Fall back on empty Hash otherwise.
+ parent = builder.parent_builder
+ duck = parent ? parent.instance_variable_get('@nested_child_index') : {}
+
+ # Strip the index from the @nested_child_index, e.g.:
+ # `post[comment_attributes][0]` becomes `post[comment_attributes]`
+ key = builder.object_name
+ key = key.gsub(/\[[0-9]+\]$/, '') if key.is_a?(String)
+
+ duck[key].to_i + 1
+ end
+
end
end
end
View
40 spec/helpers/inputs_helper_spec.rb
@@ -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|
+ concat(author.input(:login, :label => '%i', :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|
+ concat(bob_builder.input(:name, :label => 'Author #%i', :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|
+ concat(bob_builder.input(:name, :label => 'Author #%i', :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 5641f43

Please sign in to comment.