From cc779d4f67ff73050fbb42869908614bf9b39e46 Mon Sep 17 00:00:00 2001 From: Erik Ostrom Date: Wed, 23 Nov 2011 13:11:42 -0600 Subject: [PATCH 1/2] Fixed bug in #checks when values contain spaces. but label_tag does, so the input's ID was invalid and also different from the label's FOR attribute. --- lib/meta_search/helpers/form_builder.rb | 10 +++++++--- test/test_view_helpers.rb | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/meta_search/helpers/form_builder.rb b/lib/meta_search/helpers/form_builder.rb index 944da5e..be38a71 100644 --- a/lib/meta_search/helpers/form_builder.rb +++ b/lib/meta_search/helpers/form_builder.rb @@ -124,14 +124,18 @@ def collection_checks(method, collection, value_method, text_method, options = { text = choice.send(text_method) value = choice.send(value_method) check = MetaSearch::Check.new + 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 c94bdbd..41245f6 100644 --- a/test/test_view_helpers.rb +++ b/test/test_view_helpers.rb @@ -154,6 +154,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 From 09714de56272cbbe358c4220868b6f4964c701c1 Mon Sep 17 00:00:00 2001 From: Erik Ostrom Date: Sat, 26 Nov 2011 14:58:23 -0600 Subject: [PATCH 2/2] Allow views to access checkbox text/value directly. Useful if you need to represent more information about each choice than just the label and the input. --- lib/meta_search/helpers/form_builder.rb | 4 +++- test/test_view_helpers.rb | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/meta_search/helpers/form_builder.rb b/lib/meta_search/helpers/form_builder.rb index be38a71..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,6 +124,8 @@ 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 diff --git a/test/test_view_helpers.rb b/test/test_view_helpers.rb index 41245f6..8e2d201 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