1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Refactor select tag form helpers

This commit is contained in:
Carlos Antonio da Silva 2012-01-25 20:06:46 -02:00
parent 40b218977e
commit 6a5408b775
2 changed files with 25 additions and 11 deletions

View file

@ -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

View file

@ -11,20 +11,29 @@ 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]
}
option_tags = if grouped_choices?
grouped_options_for_select(@choices, option_tags_options)
else
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 => [] }
#
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])
else
option_tags = options_for_select(@choices, :selected => selected_value, :disabled => @options[:disabled])
end
select_content_tag(option_tags, @options, @html_options)
def grouped_choices?
!@choices.empty? && @choices.first.respond_to?(:last) && Array === @choices.first.last
end
end
end