aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib
diff options
context:
space:
mode:
authorCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-01-31 15:45:59 -0200
committerCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-02-02 09:40:22 -0200
commit9323fb60572815fc63fa618a5bd17b68cf8c8e35 (patch)
tree70017e5388cc205a7b326684d839ae56d78e7c01 /actionpack/lib
parent3ccbc619e4e1614c94d19cc9d12bbe7f7826cbdf (diff)
downloadrails-9323fb60572815fc63fa618a5bd17b68cf8c8e35.tar.gz
rails-9323fb60572815fc63fa618a5bd17b68cf8c8e35.tar.bz2
rails-9323fb60572815fc63fa618a5bd17b68cf8c8e35.zip
Create collection_radio_buttons helper
[Carlos Antonio da Silva + Rafael Mendonça França]
Diffstat (limited to 'actionpack/lib')
-rw-r--r--actionpack/lib/action_view/helpers/form_options_helper.rb4
-rw-r--r--actionpack/lib/action_view/helpers/tags.rb31
-rw-r--r--actionpack/lib/action_view/helpers/tags/collection_radio_buttons.rb85
3 files changed, 105 insertions, 15 deletions
diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb
index c4cdfef4a2..5ca2f2abdc 100644
--- a/actionpack/lib/action_view/helpers/form_options_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_options_helper.rb
@@ -191,6 +191,10 @@ module ActionView
Tags::CollectionSelect.new(object, method, self, collection, value_method, text_method, options, html_options).render
end
+ def collection_radio_buttons(object, method, collection, value_method, text_method, options = {}, html_options = {}, &block)
+ Tags::CollectionRadioButtons.new(object, method, self, collection, value_method, text_method, options, html_options).render(&block)
+ 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>
diff --git a/actionpack/lib/action_view/helpers/tags.rb b/actionpack/lib/action_view/helpers/tags.rb
index e874d4ca42..fa19cf2dba 100644
--- a/actionpack/lib/action_view/helpers/tags.rb
+++ b/actionpack/lib/action_view/helpers/tags.rb
@@ -4,27 +4,28 @@ module ActionView
extend ActiveSupport::Autoload
autoload :Base
+ autoload :CheckBox
+ autoload :CollectionRadioButtons
+ autoload :CollectionSelect
+ autoload :DateSelect
+ autoload :DatetimeSelect
+ autoload :EmailField
+ autoload :FileField
+ autoload :GroupedCollectionSelect
+ autoload :HiddenField
autoload :Label
- autoload :TextField
+ autoload :NumberField
autoload :PasswordField
- autoload :HiddenField
- autoload :FileField
+ autoload :RadioButton
+ autoload :RangeField
autoload :SearchField
+ autoload :Select
autoload :TelField
- autoload :UrlField
- autoload :EmailField
- autoload :NumberField
- autoload :RangeField
autoload :TextArea
- autoload :CheckBox
- autoload :RadioButton
- autoload :Select
- autoload :CollectionSelect
- autoload :GroupedCollectionSelect
- autoload :TimeZoneSelect
- autoload :DateSelect
+ autoload :TextField
autoload :TimeSelect
- autoload :DatetimeSelect
+ autoload :TimeZoneSelect
+ autoload :UrlField
end
end
end
diff --git a/actionpack/lib/action_view/helpers/tags/collection_radio_buttons.rb b/actionpack/lib/action_view/helpers/tags/collection_radio_buttons.rb
new file mode 100644
index 0000000000..eea8c41dc0
--- /dev/null
+++ b/actionpack/lib/action_view/helpers/tags/collection_radio_buttons.rb
@@ -0,0 +1,85 @@
+module ActionView
+ module Helpers
+ module Tags
+ class CollectionRadioButtons < CollectionSelect
+ delegate :radio_button, :label, :to => :@template_object
+
+ def render
+ rendered_collection = render_collection(
+ @method_name, @collection, @value_method, @text_method, @options, @html_options
+ ) do |value, text, default_html_options|
+ if block_given?
+ yield sanitize_attribute_name(@method_name, value), text, value, default_html_options
+ else
+ radio_button(@object_name, @method_name, value, default_html_options) +
+ label(@object_name, sanitize_attribute_name(@method_name, value), text, :class => "collection_radio_buttons")
+ end
+ end
+
+ wrap_rendered_collection(rendered_collection, @options)
+ end
+
+ private
+
+ # Generate default options for collection helpers, such as :checked and
+ # :disabled.
+ def default_html_options_for_collection(item, value, options, html_options) #:nodoc:
+ html_options = html_options.dup
+
+ [:checked, :selected, :disabled].each do |option|
+ next unless options[option]
+
+
+ accept = if options[option].respond_to?(:call)
+ options[option].call(item)
+ else
+ Array(options[option]).include?(value)
+ end
+
+ if accept
+ html_options[option] = true
+ elsif option == :checked
+ html_options[option] = false
+ end
+ end
+
+ html_options
+ end
+
+ def sanitize_attribute_name(attribute, value) #:nodoc:
+ "#{attribute}_#{value.to_s.gsub(/\s/, "_").gsub(/[^-\w]/, "").downcase}"
+ end
+
+ def render_collection(attribute, collection, value_method, text_method, options={}, html_options={}) #:nodoc:
+ item_wrapper_tag = options.fetch(:item_wrapper_tag, :span)
+ item_wrapper_class = options[:item_wrapper_class]
+
+ collection.map do |item|
+ value = value_for_collection(item, value_method)
+ text = value_for_collection(item, text_method)
+ default_html_options = default_html_options_for_collection(item, value, options, html_options)
+
+ rendered_item = yield value, text, default_html_options
+
+ item_wrapper_tag ? @template_object.content_tag(item_wrapper_tag, rendered_item, :class => item_wrapper_class) : rendered_item
+ end.join.html_safe
+ end
+
+ def value_for_collection(item, value) #:nodoc:
+ value.respond_to?(:call) ? value.call(item) : item.send(value)
+ end
+
+ def wrap_rendered_collection(collection, options)
+ wrapper_tag = options[:collection_wrapper_tag]
+
+ if wrapper_tag
+ wrapper_class = options[:collection_wrapper_class]
+ @template_object.content_tag(wrapper_tag, collection, :class => wrapper_class)
+ else
+ collection
+ end
+ end
+ end
+ end
+ end
+end