Skip to content
Browse files

Extract Select

  • Loading branch information...
1 parent 39dc7fc commit 18f181db479630679bcc414b8c290b40d8b26781 @rafaelfranca rafaelfranca committed Jan 16, 2012
View
19 actionpack/lib/action_view/helpers/form_options_helper.rb
@@ -154,7 +154,7 @@ module FormOptionsHelper
# key in the query string, that works for ordinary forms.
#
def select(object, method, choices, options = {}, html_options = {})
- InstanceTag.new(object, method, self, options.delete(:object)).to_select_tag(choices, options, html_options)
+ ActionView::Helpers::Tags::Select.new(object, method, self, choices, options, html_options).render
end
# Returns <tt><select></tt> and <tt><option></tt> tags for the collection of existing return values of
@@ -576,23 +576,6 @@ def extract_values_from_collection(collection, value_method, selected)
class InstanceTag #:nodoc:
include FormOptionsHelper
- def to_select_tag(choices, options, html_options)
- selected_value = options.has_key?(:selected) ? options[:selected] : value(object)
-
- # 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)
- end
-
def to_collection_select_tag(collection, value_method, text_method, options, html_options)
selected_value = options.has_key?(:selected) ? options[:selected] : value(object)
select_content_tag(
View
1 actionpack/lib/action_view/helpers/tags.rb
@@ -16,6 +16,7 @@ module Tags
autoload :TextArea, 'action_view/helpers/tags/text_area'
autoload :CheckBox, 'action_view/helpers/tags/check_box'
autoload :RadioButton, 'action_view/helpers/tags/radio_button'
+ autoload :Select, 'action_view/helpers/tags/select'
end
end
end
View
57 actionpack/lib/action_view/helpers/tags/select.rb
@@ -0,0 +1,57 @@
+module ActionView
+ module Helpers
+ module Tags
+ class Select < Base #:nodoc:
+ include FormOptionsHelper
+
+ def initialize(object_name, method_name, template_object, choices, options, html_options)
+ @choices = choices
+ @html_options = html_options
+
+ super(object_name, method_name, template_object, options)
+ end
+
+ def render
+ selected_value = @options.has_key?(:selected) ? @options[:selected] : value(@object)
+
+ # 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)
+ end
+
+ private
+
+ def select_content_tag(option_tags, options, 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"]
+ tag("input", :disabled => html_options["disabled"], :name => html_options["name"], :type => "hidden", :value => "") + select
+ else
+ select
+ end
+ end
+
+ def add_options(option_tags, options, value = nil)
+ if options[:include_blank]
+ option_tags = "<option value=\"\">#{ERB::Util.html_escape(options[:include_blank]) if options[:include_blank].kind_of?(String)}</option>\n" + option_tags
+ end
+ if value.blank? && options[:prompt]
+ prompt = options[:prompt].kind_of?(String) ? options[:prompt] : I18n.translate('helpers.select.prompt', :default => 'Please select')
+ option_tags = "<option value=\"\">#{ERB::Util.html_escape(prompt)}</option>\n" + option_tags
+ end
+ option_tags.html_safe
+ end
+ end
+ end
+ end
+end

0 comments on commit 18f181d

Please sign in to comment.
Something went wrong with that request. Please try again.