aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2012-01-16 22:54:55 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2012-01-17 00:05:30 -0300
commit18f181db479630679bcc414b8c290b40d8b26781 (patch)
treeb5a880d0e8664555bf99ecb2b18868aaf80f8ffd /actionpack/lib
parent39dc7fc8aedf7b114064953c6ef166c648cd15a3 (diff)
downloadrails-18f181db479630679bcc414b8c290b40d8b26781.tar.gz
rails-18f181db479630679bcc414b8c290b40d8b26781.tar.bz2
rails-18f181db479630679bcc414b8c290b40d8b26781.zip
Extract Select
Diffstat (limited to 'actionpack/lib')
-rw-r--r--actionpack/lib/action_view/helpers/form_options_helper.rb19
-rw-r--r--actionpack/lib/action_view/helpers/tags.rb1
-rw-r--r--actionpack/lib/action_view/helpers/tags/select.rb57
3 files changed, 59 insertions, 18 deletions
diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb
index ba9ff1d5aa..8cce6d72cb 100644
--- a/actionpack/lib/action_view/helpers/form_options_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_options_helper.rb
@@ -154,7 +154,7 @@ module ActionView
# 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 @@ module ActionView
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(
diff --git a/actionpack/lib/action_view/helpers/tags.rb b/actionpack/lib/action_view/helpers/tags.rb
index 4dfd81841e..33e5425d48 100644
--- a/actionpack/lib/action_view/helpers/tags.rb
+++ b/actionpack/lib/action_view/helpers/tags.rb
@@ -16,6 +16,7 @@ module ActionView
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
diff --git a/actionpack/lib/action_view/helpers/tags/select.rb b/actionpack/lib/action_view/helpers/tags/select.rb
new file mode 100644
index 0000000000..56d1dbfd38
--- /dev/null
+++ b/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