Permalink
Browse files

Extract CollectionSelect

  • Loading branch information...
1 parent 18f181d commit 9eb6cd6373cf8c1142e1f3cd9e3af55d47828bd7 @rafaelfranca rafaelfranca committed Jan 17, 2012
@@ -188,10 +188,9 @@ def select(object, method, choices, options = {}, html_options = {})
# <option value="3">M. Clark</option>
# </select>
def collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
- InstanceTag.new(object, method, self, options.delete(:object)).to_collection_select_tag(collection, value_method, text_method, options, html_options)
+ ActionView::Helpers::Tags::CollectionSelect.new(object, method, self, collection, value_method, text_method, options, html_options).render
end
-
# Returns <tt><select></tt>, <tt><optgroup></tt> and <tt><option></tt> tags for the collection of existing return values of
# +method+ for +object+'s class. The value returned from calling +method+ on the instance +object+ will
# be selected. If calling +method+ returns +nil+, no selection is made without including <tt>:prompt</tt>
@@ -576,13 +575,6 @@ def extract_values_from_collection(collection, value_method, selected)
class InstanceTag #:nodoc:
include FormOptionsHelper
- 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(
- options_from_collection_for_select(collection, value_method, text_method, :selected => selected_value, :disabled => options[:disabled]), options, html_options
- )
- end
-
def to_grouped_collection_select_tag(collection, group_method, group_label_method, option_key_method, option_value_method, options, html_options)
select_content_tag(
option_groups_from_collection_for_select(collection, group_method, group_label_method, option_key_method, option_value_method, value(object)), options, html_options
@@ -1,22 +1,23 @@
module ActionView
module Helpers
module Tags
- autoload :Base, 'action_view/helpers/tags/base'
- autoload :Label, 'action_view/helpers/tags/label'
- autoload :TextField, 'action_view/helpers/tags/text_field'
- autoload :PasswordField, 'action_view/helpers/tags/password_field'
- autoload :HiddenField, 'action_view/helpers/tags/hidden_field'
- autoload :FileField, 'action_view/helpers/tags/file_field'
- autoload :SearchField, 'action_view/helpers/tags/search_field'
- autoload :TelField, 'action_view/helpers/tags/tel_field'
- autoload :UrlField, 'action_view/helpers/tags/url_field'
- autoload :EmailField, 'action_view/helpers/tags/email_field'
- autoload :NumberField, 'action_view/helpers/tags/number_field'
- autoload :RangeField, 'action_view/helpers/tags/range_field'
- 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'
+ autoload :Base, 'action_view/helpers/tags/base'
+ autoload :Label, 'action_view/helpers/tags/label'
+ autoload :TextField, 'action_view/helpers/tags/text_field'
+ autoload :PasswordField, 'action_view/helpers/tags/password_field'
+ autoload :HiddenField, 'action_view/helpers/tags/hidden_field'
+ autoload :FileField, 'action_view/helpers/tags/file_field'
+ autoload :SearchField, 'action_view/helpers/tags/search_field'
+ autoload :TelField, 'action_view/helpers/tags/tel_field'
+ autoload :UrlField, 'action_view/helpers/tags/url_field'
+ autoload :EmailField, 'action_view/helpers/tags/email_field'
+ autoload :NumberField, 'action_view/helpers/tags/number_field'
+ autoload :RangeField, 'action_view/helpers/tags/range_field'
+ 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'
+ autoload :CollectionSelect, 'action_view/helpers/tags/collection_select'
end
end
end
@@ -3,6 +3,7 @@ module Helpers
module Tags
class Base #:nodoc:
include Helpers::ActiveModelInstanceTag, Helpers::TagHelper, Helpers::FormTagHelper
+ include FormOptionsHelper
DEFAULT_FIELD_OPTIONS = { "size" => 30 }
@@ -114,6 +115,28 @@ def sanitized_object_name
def sanitized_method_name
@sanitized_method_name ||= @method_name.sub(/\?$/,"")
end
+
+ 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
@@ -0,0 +1,23 @@
+module ActionView
+ module Helpers
+ module Tags
+ class CollectionSelect < Base #:nodoc:
+ def initialize(object_name, method_name, template_object, collection, value_method, text_method, options, html_options)
+ @collection = collection
+ @value_method = value_method
+ @text_method = text_method
+ @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)
+ select_content_tag(
+ options_from_collection_for_select(@collection, @value_method, @text_method, :selected => selected_value, :disabled => @options[:disabled]), @options, @html_options
+ )
+ end
+ end
+ end
+ end
+end
@@ -2,8 +2,6 @@ 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
@@ -27,30 +25,6 @@ def render
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

0 comments on commit 9eb6cd6

Please sign in to comment.