Skip to content

Commit

Permalink
Changed radio_input and check_boxes_input to no longer uses field_set…
Browse files Browse the repository at this point in the history
…_wrapping_for_method() [which was designed for use with labels that have a mapping to actual inputs]. Instead, I've hard-coded the desired markup into the method. Less DRY, but decoupled.

As a result, we now have control over things like the label(), which no longer contains a `for` attribute for an input that doesn't exist, resolving formtastic#253.
  • Loading branch information
justinfrench committed Jun 7, 2010
1 parent 0d69232 commit 9593e4b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
16 changes: 13 additions & 3 deletions lib/formtastic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -885,8 +885,13 @@ def radio_input(method, options)
li_options = value_as_class ? { :class => [method.to_s.singularize, value.to_s.downcase].join('_') } : {}
template.content_tag(:li, Formtastic::Util.html_safe(li_content), li_options)
end

field_set_and_list_wrapping_for_method(method, options, list_item_content)

template.content_tag(:fieldset,
template.content_tag(:legend,
template.label_tag(nil, localized_string(method, method, :label) || humanized_attribute_name(method), :for => nil), :class => :label
) <<
template.content_tag(:ol, list_item_content)
)
end
alias :boolean_radio_input :radio_input

Expand Down Expand Up @@ -1152,7 +1157,12 @@ def check_boxes_input(method, options)
template.content_tag(:li, Formtastic::Util.html_safe(li_content), li_options)
end

field_set_and_list_wrapping_for_method(method, options, list_item_content)
template.content_tag(:fieldset,
template.content_tag(:legend,
template.label_tag(nil, localized_string(method, method, :label) || humanized_attribute_name(method), :for => nil), :class => :label
) <<
template.content_tag(:ol, list_item_content)
)
end

# Outputs a country select input, wrapping around a regular country_select helper.
Expand Down
23 changes: 21 additions & 2 deletions spec/inputs/check_boxes_input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,29 @@
end

end

describe "with i18n of the legend label" do

before do
::I18n.backend.store_translations :en, :formtastic => { :labels => { :post => { :authors => "Translated!" }}}

@new_post.stub!(:author_ids).and_return(nil)
semantic_form_for(@new_post) do |builder|
concat(builder.input(:authors, :as => :check_boxes))
end
end

after do
::I18n.backend.reload!
end

it "should do foo" do
output_buffer.should have_tag("legend.label label", /Translated/)
end

end

end



end

23 changes: 22 additions & 1 deletion spec/inputs/radio_input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
end

it 'should not link the label within the legend to any input' do
output_buffer.should_not have_tag('form li fieldset legend label[@for^="post_author_id_"]')
output_buffer.should_not have_tag('form li fieldset legend label[@for]')
end

it 'should generate an ordered list with a list item for each choice' do
Expand Down Expand Up @@ -161,5 +161,26 @@
end

end

describe "with i18n of the legend label" do

before do
::I18n.backend.store_translations :en, :formtastic => { :labels => { :post => { :authors => "Translated!" }}}

@new_post.stub!(:author_ids).and_return(nil)
semantic_form_for(@new_post) do |builder|
concat(builder.input(:authors, :as => :radio))
end
end

after do
::I18n.backend.reload!
end

it "should do foo" do
output_buffer.should have_tag("legend.label label", /Translated/)
end

end

end

0 comments on commit 9593e4b

Please sign in to comment.