Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

fix for #checks with string values #75

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions lib/meta_search/helpers/form_builder.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions test/test_view_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
<p>
Expand All @@ -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
<p>
<label for="search_name_in_mission_data">Mission Data</label>
<input id="search_name_in_mission_data" name="search[name_in][]"
type="checkbox" value="Mission Data" />
</p>
EXPECTED

assert_dom_equal expected,
render(:to => :string, :inline => <<-ERB)
<%= @f.checks(:name_in, [['Mission Data', 'Mission Data']]) do |c| -%>
<p>
<%= c.label %>
<%= c.box %>
</p>
<% end %>
ERB
end
end
end

context "A form using checks with three choices and a previous selection" do
Expand Down