diff options
author | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-01-25 20:06:46 -0200 |
---|---|---|
committer | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-01-31 14:48:25 -0200 |
commit | 6a5408b77598d04eea83c403841f861fda20f937 (patch) | |
tree | b1f477b98065cbce8a6776552b5fb19e17d662c6 /actionpack | |
parent | 40b218977ecf171852f6b00587b77602edf180de (diff) | |
download | rails-6a5408b77598d04eea83c403841f861fda20f937.tar.gz rails-6a5408b77598d04eea83c403841f861fda20f937.tar.bz2 rails-6a5408b77598d04eea83c403841f861fda20f937.zip |
Refactor select tag form helpers
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_view/helpers/tags/collection_select.rb | 9 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/tags/select.rb | 27 |
2 files changed, 25 insertions, 11 deletions
diff --git a/actionpack/lib/action_view/helpers/tags/collection_select.rb b/actionpack/lib/action_view/helpers/tags/collection_select.rb index f84140d8d0..ec78e6e5f9 100644 --- a/actionpack/lib/action_view/helpers/tags/collection_select.rb +++ b/actionpack/lib/action_view/helpers/tags/collection_select.rb @@ -12,9 +12,14 @@ module ActionView end def render - selected_value = @options.has_key?(:selected) ? @options[:selected] : value(@object) + option_tags_options = { + :selected => @options.fetch(:selected) { value(@object) }, + :disabled => @options[:disabled] + } + select_content_tag( - options_from_collection_for_select(@collection, @value_method, @text_method, :selected => selected_value, :disabled => @options[:disabled]), @options, @html_options + options_from_collection_for_select(@collection, @value_method, @text_method, option_tags_options), + @options, @html_options ) end end diff --git a/actionpack/lib/action_view/helpers/tags/select.rb b/actionpack/lib/action_view/helpers/tags/select.rb index 02b790db4e..53a108b7e6 100644 --- a/actionpack/lib/action_view/helpers/tags/select.rb +++ b/actionpack/lib/action_view/helpers/tags/select.rb @@ -11,21 +11,30 @@ module ActionView end def render - selected_value = @options.has_key?(:selected) ? @options[:selected] : value(@object) + option_tags_options = { + :selected => @options.fetch(:selected) { value(@object) }, + :disabled => @options[:disabled] + } - # Grouped choices look like this: - # - # [nil, []] - # { nil => [] } - # - if !@choices.empty? && @choices.first.respond_to?(:last) && Array === @choices.first.last - option_tags = grouped_options_for_select(@choices, :selected => selected_value, :disabled => @options[:disabled]) + option_tags = if grouped_choices? + grouped_options_for_select(@choices, option_tags_options) else - option_tags = options_for_select(@choices, :selected => selected_value, :disabled => @options[:disabled]) + options_for_select(@choices, option_tags_options) end select_content_tag(option_tags, @options, @html_options) end + + private + + # Grouped choices look like this: + # + # [nil, []] + # { nil => [] } + # + def grouped_choices? + !@choices.empty? && @choices.first.respond_to?(:last) && Array === @choices.first.last + end end end end |