From b1619560614a49956828ec75aebaa4ad8b212684 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Wed, 1 Feb 2012 17:28:50 -0200 Subject: Move collection radio buttons / check boxes back to FormOptionsHelper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [Carlos Antonio da Silva + Rafael Mendonça França] --- actionpack/lib/action_view/helpers.rb | 2 - .../action_view/helpers/form_collections_helper.rb | 82 ---------------------- .../lib/action_view/helpers/form_options_helper.rb | 75 ++++++++++++++++++++ .../action_view/helpers/tags/collection_helpers.rb | 14 ++-- 4 files changed, 81 insertions(+), 92 deletions(-) delete mode 100644 actionpack/lib/action_view/helpers/form_collections_helper.rb diff --git a/actionpack/lib/action_view/helpers.rb b/actionpack/lib/action_view/helpers.rb index d47ec6eeb4..f2a3a494bc 100644 --- a/actionpack/lib/action_view/helpers.rb +++ b/actionpack/lib/action_view/helpers.rb @@ -12,7 +12,6 @@ module ActionView #:nodoc: autoload :CsrfHelper autoload :DateHelper autoload :DebugHelper - autoload :FormCollectionsHelper autoload :FormHelper autoload :FormOptionsHelper autoload :FormTagHelper @@ -43,7 +42,6 @@ module ActionView #:nodoc: include CsrfHelper include DateHelper include DebugHelper - include FormCollectionsHelper include FormHelper include FormOptionsHelper include FormTagHelper diff --git a/actionpack/lib/action_view/helpers/form_collections_helper.rb b/actionpack/lib/action_view/helpers/form_collections_helper.rb deleted file mode 100644 index d3d1ac18d3..0000000000 --- a/actionpack/lib/action_view/helpers/form_collections_helper.rb +++ /dev/null @@ -1,82 +0,0 @@ -module ActionView - module Helpers - module FormCollectionsHelper - # Returns radio button 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. - # - # The :value_method and :text_method parameters are methods to be called on each member - # of +collection+. The return values are used as the +value+ attribute and contents of each - # radio button tag, respectively. - # - # Example object structure for use with this method: - # class Post < ActiveRecord::Base - # belongs_to :author - # end - # class Author < ActiveRecord::Base - # has_many :posts - # def name_with_initial - # "#{first_name.first}. #{last_name}" - # end - # end - # - # Sample usage (selecting the associated Author for an instance of Post, @post): - # collection_radio_buttons(:post, :author_id, Author.all, :id, :name_with_initial) - # - # If @post.author_id is already 1, this would return: - # - # - # - # - # - # - 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 check box 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. - # - # The :value_method and :text_method parameters are methods to be called on each member - # of +collection+. The return values are used as the +value+ attribute and contents of each - # check box tag, respectively. - # - # Example object structure for use with this method: - # class Post < ActiveRecord::Base - # has_and_belongs_to :author - # end - # class Author < ActiveRecord::Base - # has_and_belongs_to :posts - # def name_with_initial - # "#{first_name.first}. #{last_name}" - # end - # end - # - # Sample usage (selecting the associated Author for an instance of Post, @post): - # collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial) - # - # If @post.author_ids is already [1], this would return: - # - # - # - # - # - # - # - 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 - end - - class FormBuilder - def collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}) - @template.collection_radio_buttons(@object_name, method, collection, value_method, text_method, objectify_options(options), @default_options.merge(html_options)) - end - - def collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}) - @template.collection_check_boxes(@object_name, method, collection, value_method, text_method, objectify_options(options), @default_options.merge(html_options)) - end - end - end -end diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index 6e0c0b6e2c..cf56bf3b9a 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -520,6 +520,73 @@ module ActionView zone_options.html_safe end + # Returns radio button 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. + # + # The :value_method and :text_method parameters are methods to be called on each member + # of +collection+. The return values are used as the +value+ attribute and contents of each + # radio button tag, respectively. + # + # Example object structure for use with this method: + # class Post < ActiveRecord::Base + # belongs_to :author + # end + # class Author < ActiveRecord::Base + # has_many :posts + # def name_with_initial + # "#{first_name.first}. #{last_name}" + # end + # end + # + # Sample usage (selecting the associated Author for an instance of Post, @post): + # collection_radio_buttons(:post, :author_id, Author.all, :id, :name_with_initial) + # + # If @post.author_id is already 1, this would return: + # + # + # + # + # + # + 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 check box 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. + # + # The :value_method and :text_method parameters are methods to be called on each member + # of +collection+. The return values are used as the +value+ attribute and contents of each + # check box tag, respectively. + # + # Example object structure for use with this method: + # class Post < ActiveRecord::Base + # has_and_belongs_to :author + # end + # class Author < ActiveRecord::Base + # has_and_belongs_to :posts + # def name_with_initial + # "#{first_name.first}. #{last_name}" + # end + # end + # + # Sample usage (selecting the associated Author for an instance of Post, @post): + # collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial) + # + # If @post.author_ids is already [1], this would return: + # + # + # + # + # + # + # + 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 + private def option_html_attributes(element) return "" unless Array === element @@ -588,6 +655,14 @@ module ActionView def time_zone_select(method, priority_zones = nil, options = {}, html_options = {}) @template.time_zone_select(@object_name, method, priority_zones, objectify_options(options), @default_options.merge(html_options)) end + + def collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}) + @template.collection_check_boxes(@object_name, method, collection, value_method, text_method, objectify_options(options), @default_options.merge(html_options)) + end + + def collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}) + @template.collection_radio_buttons(@object_name, method, collection, value_method, text_method, objectify_options(options), @default_options.merge(html_options)) + end end end end diff --git a/actionpack/lib/action_view/helpers/tags/collection_helpers.rb b/actionpack/lib/action_view/helpers/tags/collection_helpers.rb index 1c9ba8598f..4fc2293a8d 100644 --- a/actionpack/lib/action_view/helpers/tags/collection_helpers.rb +++ b/actionpack/lib/action_view/helpers/tags/collection_helpers.rb @@ -33,7 +33,6 @@ module ActionView def instantiate_builder(builder_class, value, text, html_options) builder_class.new(@template_object, @object_name, @method_name, sanitize_attribute_name(value), text, value, html_options) - end # Generate default options for collection helpers, such as :checked and @@ -42,14 +41,13 @@ module ActionView html_options = @html_options.dup [:checked, :selected, :disabled].each do |option| - next unless @options[option] - + next unless current_value = @options[option] - accept = if @options[option].respond_to?(:call) - @options[option].call(item) - else - Array(@options[option]).include?(value) - end + accept = if current_value.respond_to?(:call) + current_value.call(item) + else + Array(current_value).include?(value) + end if accept html_options[option] = true -- cgit v1.2.3