mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Move collection radio buttons / check boxes back to FormOptionsHelper
[Carlos Antonio da Silva + Rafael Mendonça França]
This commit is contained in:
parent
36cb7150fd
commit
b161956061
4 changed files with 81 additions and 92 deletions
|
@ -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
|
||||
|
|
|
@ -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 <tt>:value_method</tt> and <tt>:text_method</tt> 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, <tt>@post</tt>):
|
||||
# collection_radio_buttons(:post, :author_id, Author.all, :id, :name_with_initial)
|
||||
#
|
||||
# If <tt>@post.author_id</tt> is already <tt>1</tt>, this would return:
|
||||
# <input id="post_author_id_1" name="post[author_id]" type="radio" value="1" checked="checked" />
|
||||
# <label class="collection_radio_buttons" for="post_author_id_1">D. Heinemeier Hansson</label>
|
||||
# <input id="post_author_id_2" name="post[author_id]" type="radio" value="2" />
|
||||
# <label class="collection_radio_buttons" for="post_author_id_2">D. Thomas</label>
|
||||
# <input id="post_author_id_3" name="post[author_id]" type="radio" value="3" />
|
||||
# <label class="collection_radio_buttons" for="post_author_id_3">M. Clark</label>
|
||||
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 <tt>:value_method</tt> and <tt>:text_method</tt> 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, <tt>@post</tt>):
|
||||
# collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial)
|
||||
#
|
||||
# If <tt>@post.author_ids</tt> is already <tt>[1]</tt>, this would return:
|
||||
# <input id="post_author_ids_1" name="post[author_ids][]" type="checkbox" value="1" checked="checked" />
|
||||
# <label class="collection_check_boxes" for="post_author_ids_1">D. Heinemeier Hansson</label>
|
||||
# <input id="post_author_ids_1" name="post[author_ids][]" type="checkbox" value="2" />
|
||||
# <label class="collection_check_boxes" for="post_author_ids_1">D. Thomas</label>
|
||||
# <input id="post_author_ids_3" name="post[author_ids][]" type="checkbox" value="3" />
|
||||
# <label class="collection_check_boxes" for="post_author_ids_3">M. Clark</label>
|
||||
# <input name="post[author_ids][]" type="hidden" value="" />
|
||||
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
|
|
@ -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 <tt>:value_method</tt> and <tt>:text_method</tt> 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, <tt>@post</tt>):
|
||||
# collection_radio_buttons(:post, :author_id, Author.all, :id, :name_with_initial)
|
||||
#
|
||||
# If <tt>@post.author_id</tt> is already <tt>1</tt>, this would return:
|
||||
# <input id="post_author_id_1" name="post[author_id]" type="radio" value="1" checked="checked" />
|
||||
# <label class="collection_radio_buttons" for="post_author_id_1">D. Heinemeier Hansson</label>
|
||||
# <input id="post_author_id_2" name="post[author_id]" type="radio" value="2" />
|
||||
# <label class="collection_radio_buttons" for="post_author_id_2">D. Thomas</label>
|
||||
# <input id="post_author_id_3" name="post[author_id]" type="radio" value="3" />
|
||||
# <label class="collection_radio_buttons" for="post_author_id_3">M. Clark</label>
|
||||
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 <tt>:value_method</tt> and <tt>:text_method</tt> 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, <tt>@post</tt>):
|
||||
# collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial)
|
||||
#
|
||||
# If <tt>@post.author_ids</tt> is already <tt>[1]</tt>, this would return:
|
||||
# <input id="post_author_ids_1" name="post[author_ids][]" type="checkbox" value="1" checked="checked" />
|
||||
# <label class="collection_check_boxes" for="post_author_ids_1">D. Heinemeier Hansson</label>
|
||||
# <input id="post_author_ids_1" name="post[author_ids][]" type="checkbox" value="2" />
|
||||
# <label class="collection_check_boxes" for="post_author_ids_1">D. Thomas</label>
|
||||
# <input id="post_author_ids_3" name="post[author_ids][]" type="checkbox" value="3" />
|
||||
# <label class="collection_check_boxes" for="post_author_ids_3">M. Clark</label>
|
||||
# <input name="post[author_ids][]" type="hidden" value="" />
|
||||
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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue