aboutsummaryrefslogtreecommitdiffstats
path: root/actionview
diff options
context:
space:
mode:
authorJérémie Bonal <jeremie.bonal@gmail.com>2017-12-01 17:56:45 +0100
committerJérémie Bonal <jeremie.bonal@gmail.com>2017-12-27 17:49:07 +0100
commit0d73056436329c21aa0cca972964e8df49670391 (patch)
tree95abb27fd0e77e56d566d8c1d715aa9b4f3c34ce /actionview
parentb852ef2660dac36e348865b455fab7fbcc0d2a7f (diff)
downloadrails-0d73056436329c21aa0cca972964e8df49670391.tar.gz
rails-0d73056436329c21aa0cca972964e8df49670391.tar.bz2
rails-0d73056436329c21aa0cca972964e8df49670391.zip
Allow the use of callable objects as group methods for grouped selects.
Replaced the uses of `group.send(...)` in `option_groups_from_collection_for_select` by calls to `value_for_collection(group, ...)`, allowing the use of procs, lambdas and other callable objects as parameters.
Diffstat (limited to 'actionview')
-rw-r--r--actionview/CHANGELOG.md8
-rw-r--r--actionview/lib/action_view/helpers/form_options_helper.rb14
-rw-r--r--actionview/test/template/form_options_helper_test.rb16
3 files changed, 34 insertions, 4 deletions
diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md
index f42ada0baa..c38e11dc38 100644
--- a/actionview/CHANGELOG.md
+++ b/actionview/CHANGELOG.md
@@ -1,3 +1,11 @@
+* Allow the use of callable objects as group methods for grouped selects.
+
+ Until now, the `option_groups_from_collection_for_select` method was only able to
+ handle method names as `group_method` and `group_label_method` parameters,
+ it is now able to receive procs and other callable objects too.
+
+ *Jérémie Bonal*
+
* Add `preload_link_tag` helper
This helper that allows to the browser to initiate early fetch of resources
diff --git a/actionview/lib/action_view/helpers/form_options_helper.rb b/actionview/lib/action_view/helpers/form_options_helper.rb
index 02a44477c1..a46741f661 100644
--- a/actionview/lib/action_view/helpers/form_options_helper.rb
+++ b/actionview/lib/action_view/helpers/form_options_helper.rb
@@ -214,9 +214,15 @@ module ActionView
# * +method+ - The attribute of +object+ corresponding to the select tag
# * +collection+ - An array of objects representing the <tt><optgroup></tt> tags.
# * +group_method+ - The name of a method which, when called on a member of +collection+, returns an
- # array of child objects representing the <tt><option></tt> tags.
+ # array of child objects representing the <tt><option></tt> tags. It can also be any object that responds
+ # to +call+, such as a +proc+, that will be called for each member of the +collection+ to retrieve the
+ # value.
+
# * +group_label_method+ - The name of a method which, when called on a member of +collection+, returns a
- # string to be used as the +label+ attribute for its <tt><optgroup></tt> tag.
+ # string to be used as the +label+ attribute for its <tt><optgroup></tt> tag. It can also be any object
+ # that responds to +call+, such as a +proc+, that will be called for each member of the +collection+ to
+ # retrieve the label.
+
# * +option_key_method+ - The name of a method which, when called on a child object of a member of
# +collection+, returns a value to be used as the +value+ attribute for its <tt><option></tt> tag.
# * +option_value_method+ - The name of a method which, when called on a child object of a member of
@@ -457,9 +463,9 @@ module ActionView
def option_groups_from_collection_for_select(collection, group_method, group_label_method, option_key_method, option_value_method, selected_key = nil)
collection.map do |group|
option_tags = options_from_collection_for_select(
- group.send(group_method), option_key_method, option_value_method, selected_key)
+ value_for_collection(group, group_method), option_key_method, option_value_method, selected_key)
- content_tag("optgroup".freeze, option_tags, label: group.send(group_label_method))
+ content_tag("optgroup".freeze, option_tags, label: value_for_collection(group, group_label_method))
end.join.html_safe
end
diff --git a/actionview/test/template/form_options_helper_test.rb b/actionview/test/template/form_options_helper_test.rb
index f0eed1e290..7dc5755df1 100644
--- a/actionview/test/template/form_options_helper_test.rb
+++ b/actionview/test/template/form_options_helper_test.rb
@@ -337,6 +337,22 @@ class FormOptionsHelperTest < ActionView::TestCase
)
end
+ def test_option_groups_from_collection_for_select_with_callable_group_method
+ group_proc = Proc.new { |c| c.countries }
+ assert_dom_equal(
+ "<optgroup label=\"&lt;Africa&gt;\"><option value=\"&lt;sa&gt;\">&lt;South Africa&gt;</option>\n<option value=\"so\">Somalia</option></optgroup><optgroup label=\"Europe\"><option value=\"dk\" selected=\"selected\">Denmark</option>\n<option value=\"ie\">Ireland</option></optgroup>",
+ option_groups_from_collection_for_select(dummy_continents, group_proc, "continent_name", "country_id", "country_name", "dk")
+ )
+ end
+
+ def test_option_groups_from_collection_for_select_with_callable_group_label_method
+ label_proc = Proc.new { |c| c.continent_name }
+ assert_dom_equal(
+ "<optgroup label=\"&lt;Africa&gt;\"><option value=\"&lt;sa&gt;\">&lt;South Africa&gt;</option>\n<option value=\"so\">Somalia</option></optgroup><optgroup label=\"Europe\"><option value=\"dk\" selected=\"selected\">Denmark</option>\n<option value=\"ie\">Ireland</option></optgroup>",
+ option_groups_from_collection_for_select(dummy_continents, "countries", label_proc, "country_id", "country_name", "dk")
+ )
+ end
+
def test_option_groups_from_collection_for_select_returns_html_safe_string
assert option_groups_from_collection_for_select(dummy_continents, "countries", "continent_name", "country_id", "country_name", "dk").html_safe?
end