diff options
author | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-01-31 16:31:17 -0200 |
---|---|---|
committer | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-02-02 09:40:22 -0200 |
commit | 7af0ec75d0d933d75f9a63a63bbbde554f206721 (patch) | |
tree | 481d56735c6c1a5d0efb6faa2fb30b435789587f /actionpack/lib/action_view | |
parent | 9323fb60572815fc63fa618a5bd17b68cf8c8e35 (diff) | |
download | rails-7af0ec75d0d933d75f9a63a63bbbde554f206721.tar.gz rails-7af0ec75d0d933d75f9a63a63bbbde554f206721.tar.bz2 rails-7af0ec75d0d933d75f9a63a63bbbde554f206721.zip |
Add collection_check_boxes helper
[Carlos Antonio da Silva + Rafael Mendonça França]
Diffstat (limited to 'actionpack/lib/action_view')
3 files changed, 35 insertions, 0 deletions
diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index 5ca2f2abdc..96519a7428 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -195,6 +195,10 @@ module ActionView Tags::CollectionRadioButtons.new(object, method, self, collection, value_method, text_method, options, html_options).render(&block) end + def collection_check_boxes(object, method, collection, value_method, text_method, options = {}, html_options = {}, &block) + Tags::CollectionCheckBoxes.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 fa19cf2dba..c480799fe3 100644 --- a/actionpack/lib/action_view/helpers/tags.rb +++ b/actionpack/lib/action_view/helpers/tags.rb @@ -5,6 +5,7 @@ module ActionView autoload :Base autoload :CheckBox + autoload :CollectionCheckBoxes autoload :CollectionRadioButtons autoload :CollectionSelect autoload :DateSelect diff --git a/actionpack/lib/action_view/helpers/tags/collection_check_boxes.rb b/actionpack/lib/action_view/helpers/tags/collection_check_boxes.rb new file mode 100644 index 0000000000..d8f7a1fff6 --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/collection_check_boxes.rb @@ -0,0 +1,30 @@ +module ActionView + module Helpers + module Tags + class CollectionCheckBoxes < CollectionRadioButtons + delegate :check_box, :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| + default_html_options[:multiple] = true + + if block_given? + yield sanitize_attribute_name(@method_name, value), text, value, default_html_options + else + check_box(@object_name, @method_name, default_html_options, value, nil) + + label(@object_name, sanitize_attribute_name(@method_name, value), text, :class => "collection_check_boxes") + end + end + + # Prepend a hidden field to make sure something will be sent back to the + # server if all checkboxes are unchecked. + hidden = @template_object.hidden_field_tag("#{@object_name}[#{@method_name}][]", "", :id => nil) + + wrap_rendered_collection(rendered_collection + hidden, @options) + end + end + end + end +end |