Skip to content

Commit

Permalink
Fix issue #331, :label=>false on checkboxes/radiobuttons
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyhaines authored and mjonuschat committed Aug 8, 2010
1 parent a53315a commit 214135d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lib/formtastic.rb
Expand Up @@ -907,10 +907,7 @@ def radio_input(method, options)
end

template.content_tag(:fieldset,
template.content_tag(:legend,
template.label_tag(nil, localized_string(method, options[:label], :label) || humanized_attribute_name(method), :for => nil), :class => :label
) <<
template.content_tag(:ol, Formtastic::Util.html_safe(list_item_content.join))
legend_tag(method, options) << template.content_tag(:ol, Formtastic::Util.html_safe(list_item_content.join))
)
end
alias :boolean_radio_input :radio_input
Expand Down Expand Up @@ -1187,9 +1184,7 @@ def check_boxes_input(method, options)
template.content_tag(:li, Formtastic::Util.html_safe(li_content), li_options)
end

fieldset_content = template.content_tag(:legend,
template.label_tag(nil, localized_string(method, options[:label], :label) || humanized_attribute_name(method), :for => nil), :class => :label
)
fieldset_content = legend_tag(method, options)
fieldset_content << self.create_hidden_field_for_check_boxes(input_name, value_as_class) unless hidden_fields
fieldset_content << template.content_tag(:ol, Formtastic::Util.html_safe(list_item_content.join))
template.content_tag(:fieldset, fieldset_content)
Expand Down Expand Up @@ -1409,6 +1404,14 @@ def field_set_and_list_wrapping_for_method(method, options, contents) #:nodoc:
)
end

# Generates the legend for radiobuttons and checkboxes
def legend_tag(method, options = {})
(options[:label] == false) ? "" : template.content_tag(:legend,
template.label_tag(nil, localized_string(method, options[:label], :label) || humanized_attribute_name(method), :for => nil), :class => :label
)
end


# For methods that have a database column, take a best guess as to what the input method
# should be. In most cases, it will just return the column type (eg :string), but for special
# cases it will simplify (like the case of :integer, :float & :decimal to :numeric), or do
Expand Down
15 changes: 15 additions & 0 deletions spec/inputs/check_boxes_input_spec.rb
Expand Up @@ -356,6 +356,21 @@
output_buffer.should have_tag("legend.label label", /The authors/)
end
end

describe "when :label option is false" do
before do
@output_buffer = ''
@new_post.stub!(:author_ids).and_return(nil)
@form = semantic_form_for(@new_post) do |builder|
concat(builder.input(:authors, :as => :check_boxes, :label => false))
end
end

it "should not output the legend" do
output_buffer.concat(@form) if Formtastic::Util.rails3?
output_buffer.should_not have_tag("legend.label")
end
end
end

describe 'for a has_and_belongs_to_many association' do
Expand Down
15 changes: 15 additions & 0 deletions spec/inputs/radio_input_spec.rb
Expand Up @@ -225,4 +225,19 @@
output_buffer.should have_tag("legend.label label", /The authors/)
end
end

describe "when :label option is false" do
before do
@output_buffer = ''
@new_post.stub!(:author_ids).and_return(nil)
@form = semantic_form_for(@new_post) do |builder|
concat(builder.input(:authors, :as => :radio, :label => false))
end
end

it "should not output the legend" do
output_buffer.concat(@form) if Formtastic::Util.rails3?
output_buffer.should_not have_tag("legend.label")
end
end
end

0 comments on commit 214135d

Please sign in to comment.