diff --git a/lib/meta_search/helpers/form_builder.rb b/lib/meta_search/helpers/form_builder.rb index 944da5e..baaf13d 100644 --- a/lib/meta_search/helpers/form_builder.rb +++ b/lib/meta_search/helpers/form_builder.rb @@ -1,7 +1,7 @@ require 'action_view' require 'action_view/template' module MetaSearch - Check = Struct.new(:box, :label) + Check = Struct.new(:text, :value, :box, :label) module Helpers module FormBuilder @@ -124,14 +124,20 @@ def collection_checks(method, collection, value_method, text_method, options = { text = choice.send(text_method) value = choice.send(value_method) check = MetaSearch::Check.new + check.text = text + check.value = value + id = [ + @object_name, method.to_s, + # see FormTagHelper#sanitize_to_id + value.to_s.downcase.gsub(']','').gsub(/[^-a-zA-Z0-9:.]/, "_") + ].join('_') check.box = @template.check_box_tag( "#{@object_name}[#{method}][]", value, [@object.send(method)].flatten.include?(value), - options.merge(:id => [@object_name, method.to_s, value.to_s.underscore].join('_')) + options.merge(:id => id) ) - check.label = @template.label_tag([@object_name, method.to_s, value.to_s.underscore].join('_'), - text) + check.label = @template.label_tag(id, text) if block_given? yield check else diff --git a/test/test_view_helpers.rb b/test/test_view_helpers.rb index b9a1d72..3fdba3e 100644 --- a/test/test_view_helpers.rb +++ b/test/test_view_helpers.rb @@ -129,6 +129,12 @@ def setup assert @f.checks(:id_in, [['One', 1], ['Two', 2], ['Three', 3]]).all?{|c| c.is_a?(MetaSearch::Check)} end + should "provide direct access to the check box text and value" do + check = @f.checks(:id_in, [['One', 1]]).first + assert_equal 'One', check.text + assert_equal 1, check.value + end + should "generate the expected HTML with a block" do expected = <<-EXPECTED
@@ -154,6 +160,28 @@ def setup <% end -%> ERB end + + context "where the values are strings" do + should "generate matching labels and inputs" do + expected = <<-EXPECTED +
+ + +
+ EXPECTED + + assert_dom_equal expected, + render(:to => :string, :inline => <<-ERB) +<%= @f.checks(:name_in, [['Mission Data', 'Mission Data']]) do |c| -%> ++ <%= c.label %> + <%= c.box %> +
+<% end %> + ERB + end + end end context "A form using checks with three choices and a previous selection" do