Skip to content

Commit

Permalink
- The old hidden_field toggled by include_hidden_field is now always …
Browse files Browse the repository at this point in the history
…included

  (seems like is really needed in Rails 1.2).
- In some situations with the object-method versions of the helpers Rails will
  fail saying that content_tag expects 3 parameters instead of 2.
  • Loading branch information
drodriguez committed Apr 9, 2007
1 parent a6b70d8 commit a45bf62
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 26 deletions.
18 changes: 10 additions & 8 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,6 @@ All three posibilites supports the following options in the options hash:
disabled is an array only the checkboxes in the array will be
disabled. If disabled is +false+ no checkboxes will be disabled.
The default is +false+.
[+include_hidden_field+] Adds a hidden field input tag as the last element of
the list with a zero length string as value and the
same name of the checkboxes so you always have the
parameter returned in your controller. Keep in mind
that this options make that you always have a
<tt>""</tt> element in your parameter array. The
default value for this options is +false+.

Besides the options above described the two tree methods supports also the
following options:
Expand All @@ -105,6 +98,10 @@ following options:
[+initial_level+] Specifies the first level that will be used as preffix of
+level_class+. The default is 0.

There used to be an option called +include_hidden_field+ but now it is not
more an option and is always enforced +true+ so you always get a param value
in your controllers, even when the users check no checkbox.

Since 20060917 version you could establish default option for some of the most
used parameters (+outer_class+, +inner_class+, +level_class+, +alternate+,
+alternate_class+, and +position+). To set the new default values of this
Expand Down Expand Up @@ -142,7 +139,7 @@ you could use something like:
# In the view
<%=
collection_multiple_select(
'person', 'fruit_ids', Fruit.find(:all), :id, :name, :selected_items => @person.fruit_ids
'person', 'fruit_ids', Fruit.find(:all), :id, :name
)
%>

Expand All @@ -154,6 +151,11 @@ are editing.

== Version history

- 20070409
- The old hidden_field toggled by include_hidden_field is now always
included (seems like is really needed in Rails 1.2).
- In some situations with the object-method versions of the helpers
Rails will fail saying that content_tag expects 3 parameters instead of 2.
- 20070407
- Merge with the "Object Method Parameters" branch.
- Old versions of the methods are still accessible in the "_tag" versions.
Expand Down
16 changes: 10 additions & 6 deletions lib/multiple_select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ module FormMultipleSelectHelper
include ActionView::Helpers::FormTagHelper
include ActionView::Helpers::TagHelper
include ActionView::Helpers

# There is another content_tag in ActionView::Helpers::InstanceTag, but
# we want the one in ActionView::Helpers::TagHelper.
alias av_content_tag content_tag

# Returns a list of checkboxes using
# checkboxes_from_collection_for_multiple_select to generate the list of
Expand Down Expand Up @@ -217,7 +221,7 @@ def checkboxes_from_tree_for_multiple_select(
) do
children = node.send(child_method)
branch = if not (depth == 0 || children.size == 0)
"\n" + content_tag(
"\n" + av_content_tag(
FormMultipleSelectHelperConfiguration.list_tags[0],
checkboxes_from_tree_for_multiple_select(
name, children, value_method, text_method, selected_items,
Expand Down Expand Up @@ -301,11 +305,11 @@ def multiple_select_with_path(name, options, &block)

checkboxes = yield(selected_items)

content_tag(
av_content_tag(
FormMultipleSelectHelperConfiguration.list_tags[0],
checkboxes,
:class => outer_class
)
) << "\n" << hidden_field_tag("#{name}[]", '', :id => nil)
end

private
Expand Down Expand Up @@ -348,20 +352,20 @@ def checkbox_for_multiple_select(
is_disabled = is_disabled.include?(item.last) if is_disabled.respond_to?(:include?)
item_id = idfy("#{name}#{item.last}")
cbt = check_box_tag("#{name}[]", html_escape(item.last.to_s), is_selected, :id => item_id, :disabled => is_disabled)
lbt = content_tag('label', html_escape(item.first.to_s), :for => item_id)
lbt = av_content_tag('label', html_escape(item.first.to_s), :for => item_id)
else
is_selected = selected_items.include?(item)
is_disabled = is_disabled.include?(item) if is_disabled.respond_to?(:include?)
item_id = idfy("#{name}#{item.to_s}")
cbt = check_box_tag("#{name}[]", html_escape(item.to_s), is_selected, :id => item_id, :disabled => is_disabled)
lbt = content_tag('label', html_escape(item.to_s), :for => item_id)
lbt = av_content_tag('label', html_escape(item.to_s), :for => item_id)
end

item_class = is_alternate ? "#{inner_class} #{alternate_class}".strip : inner_class

extra = block_given? ? yield : ''

content_tag(
av_content_tag(
FormMultipleSelectHelperConfiguration.list_tags[1],
position == :left ? lbt + cbt + extra : cbt + lbt + extra,
:class => item_class
Expand Down
63 changes: 51 additions & 12 deletions test/multiple_select_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,57 +237,68 @@ def test_ms
@f = Father.new
assert_dom_equal <<END.strip, multiple_select('f', 'method_for_test', ['test'])
<ul><li><input id="f_method_for_test_test" name="f[method_for_test][]" type="checkbox" value="test" /><label for="f_method_for_test_test">test</label></li></ul>
<input name="f[method_for_test][]" type="hidden" value="" />
END
end

def test_ms_with_outer_class
@f = Father.new
assert_dom_equal <<END.strip, multiple_select('f', 'method_for_test', ['test'], :outer_class => 'test_class')
<ul class="test_class"><li><input id="f_method_for_test_test" name="f[method_for_test][]" type="checkbox" value="test" /><label for="f_method_for_test_test">test</label></li></ul>
<input name="f[method_for_test][]" type="hidden" value="" />
END
end

def test_ms_selected
@f = Father.new
assert_dom_equal <<END.strip, multiple_select('f', 'method_for_test_selected', ['test'])
<ul><li><input checked="checked" id="f_method_for_test_selected_test" name="f[method_for_test_selected][]" type="checkbox" value="test" /><label for="f_method_for_test_selected_test">test</label></li></ul>
<input name="f[method_for_test_selected][]" type="hidden" value="" />
END
end

def test_ms_empty
@f = Father.new
assert_dom_equal "<ul></ul>",
multiple_select('f', 'method_for_test', [])
assert_dom_equal <<END.strip, multiple_select('f', 'method_for_test', [])
<ul></ul>
<input name="f[method_for_test][]" type="hidden" value="" />
END
end

def test_mst
assert_dom_equal <<END.strip, multiple_select_tag('f', ['test'])
<ul><li><input id="ftest" name="f[]" type="checkbox" value="test" /><label for="ftest">test</label></li></ul>
<input name="f[]" type="hidden" value="" />
END
end

def test_mst_with_outer_class
assert_dom_equal <<END.strip, multiple_select_tag('f', ['test'], :outer_class => 'test_class')
<ul class="test_class"><li><input id="ftest" name="f[]" type="checkbox" value="test" /><label for="ftest">test</label></li></ul>
<input name="f[]" type="hidden" value="" />
END
end

def test_mst_with_selected_items
assert_dom_equal <<END.strip, multiple_select_tag('f', ['test'], :selected_items => ['test'])
<ul><li><input checked="checked" id="ftest" name="f[]" type="checkbox" value="test" /><label for="ftest">test</label></li></ul>
<input name="f[]" type="hidden" value="" />
END
end

def test_mst_empty
assert_dom_equal "<ul></ul>",
multiple_select_tag('f', [])
assert_dom_equal <<END.strip, multiple_select_tag('f', [])
<ul></ul>
<input name="f[]" type="hidden" value="" />
END
end

def test_ms_selected_items
@n = Node.new
assert_dom_equal <<END.strip, multiple_select('n', 'selected_some', [1, 2], :selected_items => [2] )
<ul><li><input id="n_selected_some_1" name="n[selected_some][]" type="checkbox" value="1" /><label for="n_selected_some_1">1</label></li>
<li><input id="n_selected_some_2" name="n[selected_some][]" type="checkbox" value="2" checked="checked" /><label for="n_selected_some_2">2</label></li></ul>
<input name="n[selected_some][]" type="hidden" value="" />
END
end

Expand All @@ -296,6 +307,7 @@ def test_ms_selected_items_nil
assert_dom_equal <<END.strip, multiple_select('n', 'selected_some', [1, 2], :selected_items => nil )
<ul><li><input id="n_selected_some_1" name="n[selected_some][]" type="checkbox" value="1" /><label for="n_selected_some_1">1</label></li>
<li><input id="n_selected_some_2" name="n[selected_some][]" type="checkbox" value="2" /><label for="n_selected_some_2">2</label></li></ul>
<input name="n[selected_some][]" type="hidden" value="" />
END
end

Expand All @@ -304,10 +316,12 @@ def test_ms_outer_class_variable
FightTheMelons::Helpers::FormMultipleSelectHelperConfiguration.outer_class = 'classtest'
assert_dom_equal <<END.strip, multiple_select('f', 'method_for_test', ['test'])
<ul class="classtest"><li><input id="f_method_for_test_test" name="f[method_for_test][]" type="checkbox" value="test" /><label for="f_method_for_test_test">test</label></li></ul>
<input name="f[method_for_test][]" type="hidden" value="" />
END

assert_dom_equal <<END.strip, multiple_select('f', 'method_for_test', ['test'], :outer_class => 'testclass')
<ul class="testclass"><li><input id="f_method_for_test_test" name="f[method_for_test][]" type="checkbox" value="test" /><label for="f_method_for_test_test">test</label></li></ul>
<input name="f[method_for_test][]" type="hidden" value="" />
END
end

Expand All @@ -316,6 +330,7 @@ def test_ms_nil_value
assert_dom_equal <<END.strip, multiple_select('n', 'selected_nil', {'item1' => 'value1', 'item2' => 'value2'} )
<ul><li><input id="n_selected_nil_value1" name="n[selected_nil][]" type="checkbox" value="value1" /><label for="n_selected_nil_value1">item1</label></li>
<li><input id="n_selected_nil_value2" name="n[selected_nil][]" type="checkbox" value="value2" /><label for="n_selected_nil_value2">item2</label></li></ul>
<input name="n[selected_nil][]" type="hidden" value="" />
END
end

Expand All @@ -334,6 +349,7 @@ def test_cms
<li><input id="f_son_ids_5" name="f[son_ids][]" type="checkbox" value="5" /><label for="f_son_ids_5">Son 5</label></li>
<li><input id="f_son_ids_6" name="f[son_ids][]" type="checkbox" value="6" /><label for="f_son_ids_6">Son 6</label></li>
<li><input id="f_son_ids_7" name="f[son_ids][]" type="checkbox" value="7" /><label for="f_son_ids_7">Son 7</label></li></ul>
<input name="f[son_ids][]" type="hidden" value="" />
END
end

Expand All @@ -352,6 +368,7 @@ def test_cms_with_outer_class
<li><input id="f_son_ids_5" name="f[son_ids][]" type="checkbox" value="5" /><label for="f_son_ids_5">Son 5</label></li>
<li><input id="f_son_ids_6" name="f[son_ids][]" type="checkbox" value="6" /><label for="f_son_ids_6">Son 6</label></li>
<li><input id="f_son_ids_7" name="f[son_ids][]" type="checkbox" value="7" /><label for="f_son_ids_7">Son 7</label></li></ul>
<input name="f[son_ids][]" type="hidden" value="" />
END
end

Expand All @@ -370,6 +387,7 @@ def test_cms_with_value
<li><input checked="checked" id="f_son_ids_5" name="f[son_ids][]" type="checkbox" value="5" /><label for="f_son_ids_5">Son 5</label></li>
<li><input checked="checked" id="f_son_ids_6" name="f[son_ids][]" type="checkbox" value="6" /><label for="f_son_ids_6">Son 6</label></li>
<li><input id="f_son_ids_7" name="f[son_ids][]" type="checkbox" value="7" /><label for="f_son_ids_7">Son 7</label></li></ul>
<input name="f[son_ids][]" type="hidden" value="" />
END
end

Expand All @@ -380,8 +398,10 @@ def test_cms_without_items
@f = Father.new
@f.son_ids = []

assert_dom_equal "<ul></ul>",
collection_multiple_select('f', 'son_ids', [], :id, :name)
assert_dom_equal <<END.strip, collection_multiple_select('f', 'son_ids', [], :id, :name)
<ul></ul>
<input name="f[son_ids][]" type="hidden" value="" />
END
end

def test_cmst
Expand All @@ -393,6 +413,7 @@ def test_cmst
<li><input id="sons5" name="sons[]" type="checkbox" value="5" /><label for="sons5">Son 5</label></li>
<li><input id="sons6" name="sons[]" type="checkbox" value="6" /><label for="sons6">Son 6</label></li>
<li><input id="sons7" name="sons[]" type="checkbox" value="7" /><label for="sons7">Son 7</label></li></ul>
<input name="sons[]" type="hidden" value="" />
END
end

Expand All @@ -405,6 +426,7 @@ def test_cmst_with_outer_class
<li><input id="sons5" name="sons[]" type="checkbox" value="5" /><label for="sons5">Son 5</label></li>
<li><input id="sons6" name="sons[]" type="checkbox" value="6" /><label for="sons6">Son 6</label></li>
<li><input id="sons7" name="sons[]" type="checkbox" value="7" /><label for="sons7">Son 7</label></li></ul>
<input name="sons[]" type="hidden" value="" />
END
end

Expand All @@ -417,12 +439,15 @@ def test_cmst_with_selected_items
<li><input checked="checked" id="sons5" name="sons[]" type="checkbox" value="5" /><label for="sons5">Son 5</label></li>
<li><input checked="checked" id="sons6" name="sons[]" type="checkbox" value="6" /><label for="sons6">Son 6</label></li>
<li><input id="sons7" name="sons[]" type="checkbox" value="7" /><label for="sons7">Son 7</label></li></ul>
<input name="sons[]" type="hidden" value="" />
END
end

def test_cmst_without_items
assert_dom_equal "<ul></ul>",
collection_multiple_select_tag('sons', [], :id, :name)
assert_dom_equal <<END.strip, collection_multiple_select_tag('sons', [], :id, :name)
<ul></ul>
<input name="sons[]" type="hidden" value="" />
END
end

def test_cms_selected_items
Expand All @@ -440,6 +465,7 @@ def test_cms_selected_items
<li><input id="f_son_ids_5" name="f[son_ids][]" type="checkbox" value="5" /><label for="f_son_ids_5">Son 5</label></li>
<li><input id="f_son_ids_6" name="f[son_ids][]" type="checkbox" value="6" /><label for="f_son_ids_6">Son 6</label></li>
<li><input id="f_son_ids_7" name="f[son_ids][]" type="checkbox" value="7" /><label for="f_son_ids_7">Son 7</label></li></ul>
<input name="f[son_ids][]" type="hidden" value="" />
END
end

Expand All @@ -458,6 +484,7 @@ def test_cms_selected_items_nil
<li><input id="f_son_ids_5" name="f[son_ids][]" type="checkbox" value="5" /><label for="f_son_ids_5">Son 5</label></li>
<li><input id="f_son_ids_6" name="f[son_ids][]" type="checkbox" value="6" /><label for="f_son_ids_6">Son 6</label></li>
<li><input id="f_son_ids_7" name="f[son_ids][]" type="checkbox" value="7" /><label for="f_son_ids_7">Son 7</label></li></ul>
<input name="f[son_ids][]" type="hidden" value="" />
END
end

Expand All @@ -466,6 +493,7 @@ def test_tms_selected_none
assert_dom_equal <<END.strip, tree_multiple_select('n', 'selected_none', nodes(:n32).children, :id, :name)
<ul><li><input id="n_selected_none_11" name="n[selected_none][]" type="checkbox" value="11" /><label for="n_selected_none_11">Node 3.2.1</label></li>
<li><input id="n_selected_none_12" name="n[selected_none][]" type="checkbox" value="12" /><label for="n_selected_none_12">Node 3.2.2</label></li></ul>
<input name="n[selected_none][]" type="hidden" value="" />
END
end

Expand All @@ -474,6 +502,7 @@ def test_tms_with_outer_class
assert_dom_equal <<END.strip, tree_multiple_select('n', 'selected_none', nodes(:n32).children, :id, :name, :outer_class => 'test_class')
<ul class="test_class"><li><input id="n_selected_none_11" name="n[selected_none][]" type="checkbox" value="11" /><label for="n_selected_none_11">Node 3.2.1</label></li>
<li><input id="n_selected_none_12" name="n[selected_none][]" type="checkbox" value="12" /><label for="n_selected_none_12">Node 3.2.2</label></li></ul>
<input name="n[selected_none][]" type="hidden" value="" />
END
end

Expand All @@ -482,46 +511,55 @@ def test_tms_selected_some
assert_dom_equal <<END.strip, tree_multiple_select('n', 'selected_some', nodes(:n32).children, :id, :name)
<ul><li><input id="n_selected_some_11" name="n[selected_some][]" type="checkbox" value="11" /><label for="n_selected_some_11">Node 3.2.1</label></li>
<li><input checked="checked" id="n_selected_some_12" name="n[selected_some][]" type="checkbox" value="12" /><label for="n_selected_some_12">Node 3.2.2</label></li></ul>
<input name="n[selected_some][]" type="hidden" value="" />
END
end

def test_tms_without_items
@n = Node.new
assert_dom_equal "<ul></ul>",
tree_multiple_select('n', 'selected_some', nodes(:n33).children, :id, :name)
assert_dom_equal <<END.strip, tree_multiple_select('n', 'selected_some', nodes(:n33).children, :id, :name)
<ul></ul>
<input name="n[selected_some][]" type="hidden" value="" />
END
end

def test_tmst
assert_dom_equal <<END.strip, tree_multiple_select_tag('n', nodes(:n32).children, :id, :name)
<ul><li><input id="n11" name="n[]" type="checkbox" value="11" /><label for="n11">Node 3.2.1</label></li>
<li><input id="n12" name="n[]" type="checkbox" value="12" /><label for="n12">Node 3.2.2</label></li></ul>
<input name="n[]" type="hidden" value="" />
END
end

def test_tmst_with_outer_class
assert_dom_equal <<END.strip, tree_multiple_select_tag('n', nodes(:n32).children, :id, :name, :outer_class => 'test_class')
<ul class="test_class"><li><input id="n11" name="n[]" type="checkbox" value="11" /><label for="n11">Node 3.2.1</label></li>
<li><input id="n12" name="n[]" type="checkbox" value="12" /><label for="n12">Node 3.2.2</label></li></ul>
<input name="n[]" type="hidden" value="" />
END
end

def test_tmst_with_selected_items
assert_dom_equal <<END.strip, tree_multiple_select_tag('n', nodes(:n32).children, :id, :name, :selected_items => [12])
<ul><li><input id="n11" name="n[]" type="checkbox" value="11" /><label for="n11">Node 3.2.1</label></li>
<li><input checked="checked" id="n12" name="n[]" type="checkbox" value="12" /><label for="n12">Node 3.2.2</label></li></ul>
<input name="n[]" type="hidden" value="" />
END
end

def test_tmst_without_items
assert_dom_equal "<ul></ul>",
tree_multiple_select_tag('n', nodes(:n33).children, :id, :name)
assert_dom_equal <<END.strip, tree_multiple_select_tag('n', nodes(:n33).children, :id, :name)
<ul></ul>
<input name="n[]" type="hidden" value="" />
END
end

def test_tms_selected_items
@n = Node.new
assert_dom_equal <<END.strip, tree_multiple_select('n', 'selected_none', nodes(:n32).children, :id, :name, :selected_items => [11])
<ul><li><input id="n_selected_none_11" name="n[selected_none][]" type="checkbox" value="11" checked="checked" /><label for="n_selected_none_11">Node 3.2.1</label></li>
<li><input id="n_selected_none_12" name="n[selected_none][]" type="checkbox" value="12" /><label for="n_selected_none_12">Node 3.2.2</label></li></ul>
<input name="n[selected_none][]" type="hidden" value="" />
END
end

Expand All @@ -530,6 +568,7 @@ def test_tms_selected_items_nil
assert_dom_equal <<END.strip, tree_multiple_select('n', 'selected_none', nodes(:n32).children, :id, :name, :selected_items => nil)
<ul><li><input id="n_selected_none_11" name="n[selected_none][]" type="checkbox" value="11" /><label for="n_selected_none_11">Node 3.2.1</label></li>
<li><input id="n_selected_none_12" name="n[selected_none][]" type="checkbox" value="12" /><label for="n_selected_none_12">Node 3.2.2</label></li></ul>
<input name="n[selected_none][]" type="hidden" value="" />
END
end

Expand Down

0 comments on commit a45bf62

Please sign in to comment.