Skip to content

Commit

Permalink
Merge pull request #1748 from bogdan/select_form_helpers_refactor
Browse files Browse the repository at this point in the history
Select tag helpers: remove some code dups
  • Loading branch information
josevalim committed Jun 17, 2011
2 parents e510c2c + 69fdfab commit 0dd32e8
Showing 1 changed file with 10 additions and 25 deletions.
35 changes: 10 additions & 25 deletions actionpack/lib/action_view/helpers/form_options_helper.rb
Expand Up @@ -574,43 +574,26 @@ class InstanceTag #:nodoc:
include FormOptionsHelper include FormOptionsHelper


def to_select_tag(choices, options, html_options) def to_select_tag(choices, options, html_options)
html_options = html_options.stringify_keys selected_value = options.has_key?(:selected) ? options[:selected] : value(object)
add_default_name_and_id(html_options) select_content_tag(options_for_select(choices, :selected => selected_value, :disabled => options[:disabled]), options, html_options)
value = value(object)
selected_value = options.has_key?(:selected) ? options[:selected] : value
disabled_value = options.has_key?(:disabled) ? options[:disabled] : nil
select_content_tag(add_options(options_for_select(choices, :selected => selected_value, :disabled => disabled_value), options, selected_value), html_options)
end end


def to_collection_select_tag(collection, value_method, text_method, options, html_options) def to_collection_select_tag(collection, value_method, text_method, options, html_options)
html_options = html_options.stringify_keys selected_value = options.has_key?(:selected) ? options[:selected] : value(object)
add_default_name_and_id(html_options)
value = value(object)
disabled_value = options.has_key?(:disabled) ? options[:disabled] : nil
selected_value = options.has_key?(:selected) ? options[:selected] : value
select_content_tag( select_content_tag(
add_options(options_from_collection_for_select(collection, value_method, text_method, :selected => selected_value, :disabled => disabled_value), options, value), html_options options_from_collection_for_select(collection, value_method, text_method, :selected => selected_value, :disabled => options[:disabled]), options, html_options
) )
end end


def to_grouped_collection_select_tag(collection, group_method, group_label_method, option_key_method, option_value_method, options, html_options) def to_grouped_collection_select_tag(collection, group_method, group_label_method, option_key_method, option_value_method, options, html_options)
html_options = html_options.stringify_keys
add_default_name_and_id(html_options)
value = value(object)
select_content_tag( select_content_tag(
add_options(option_groups_from_collection_for_select(collection, group_method, group_label_method, option_key_method, option_value_method, value), options, value), html_options option_groups_from_collection_for_select(collection, group_method, group_label_method, option_key_method, option_value_method, value(object)), options, html_options
) )
end end


def to_time_zone_select_tag(priority_zones, options, html_options) def to_time_zone_select_tag(priority_zones, options, html_options)
html_options = html_options.stringify_keys
add_default_name_and_id(html_options)
value = value(object)
select_content_tag( select_content_tag(
add_options( time_zone_options_for_select(value(object) || options[:default], priority_zones, options[:model] || ActiveSupport::TimeZone), options, html_options
time_zone_options_for_select(value || options[:default], priority_zones, options[:model] || ActiveSupport::TimeZone),
options, value
), html_options
) )
end end


Expand All @@ -626,8 +609,10 @@ def add_options(option_tags, options, value = nil)
option_tags.html_safe option_tags.html_safe
end end


def select_content_tag(content, html_options) def select_content_tag(option_tags, options, html_options)
select = content_tag("select", content, html_options) html_options = html_options.stringify_keys
add_default_name_and_id(html_options)
select = content_tag("select", add_options(option_tags, options, value(object)), html_options)
if html_options["multiple"] if html_options["multiple"]
tag("input", :disabled => html_options["disabled"], :name => html_options["name"], :type => "hidden", :value => "") + select tag("input", :disabled => html_options["disabled"], :name => html_options["name"], :type => "hidden", :value => "") + select
else else
Expand Down

0 comments on commit 0dd32e8

Please sign in to comment.