Refactor the methods to use instance variables

[Carlos Antonio da Silva + Rafael Mendonça França]
This commit is contained in:
Rafael Mendonça França 2012-01-31 17:26:52 -02:00 committed by Carlos Antonio da Silva
parent 3312cdb0bb
commit 15206885eb
3 changed files with 26 additions and 22 deletions

@ -78,7 +78,7 @@ def add_default_name_and_id(options)
options["name"] ||= tag_name_with_index(@auto_index)
options["id"] = options.fetch("id"){ tag_id_with_index(@auto_index) }
else
options["name"] ||= tag_name + (options['multiple'] ? '[]' : '')
options["name"] ||= options['multiple'] ? tag_name_multiple : tag_name
options["id"] = options.fetch("id"){ tag_id }
end
options["id"] = [options.delete('namespace'), options["id"]].compact.join("_").presence
@ -88,6 +88,10 @@ def tag_name
"#{@object_name}[#{sanitized_method_name}]"
end
def tag_name_multiple
"#{tag_name}[]"
end
def tag_name_with_index(index)
"#{@object_name}[#{index}][#{sanitized_method_name}]"
end

@ -9,18 +9,18 @@ def render
default_html_options[:multiple] = true
if block_given?
yield sanitize_attribute_name(@method_name, value), text, value, default_html_options
yield sanitize_attribute_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")
label(@object_name, sanitize_attribute_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)
# Append a hidden field to make sure something will be sent back to the
# server if all check boxes are unchecked.
hidden = @template_object.hidden_field_tag(tag_name_multiple, "", :id => nil)
wrap_rendered_collection(rendered_collection + hidden, @options)
wrap_rendered_collection(rendered_collection + hidden)
end
end
end

@ -7,31 +7,31 @@ class CollectionRadioButtons < CollectionSelect
def render
rendered_collection = render_collection do |value, text, default_html_options|
if block_given?
yield sanitize_attribute_name(@method_name, value), text, value, default_html_options
yield sanitize_attribute_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")
label(@object_name, sanitize_attribute_name(value), text, :class => "collection_radio_buttons")
end
end
wrap_rendered_collection(rendered_collection, @options)
wrap_rendered_collection(rendered_collection)
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
def default_html_options_for_collection(item, value) #:nodoc:
html_options = @html_options.dup
[:checked, :selected, :disabled].each do |option|
next unless options[option]
next unless @options[option]
accept = if options[option].respond_to?(:call)
options[option].call(item)
accept = if @options[option].respond_to?(:call)
@options[option].call(item)
else
Array(options[option]).include?(value)
Array(@options[option]).include?(value)
end
if accept
@ -44,8 +44,8 @@ def default_html_options_for_collection(item, value, options, html_options) #:no
html_options
end
def sanitize_attribute_name(attribute, value) #:nodoc:
"#{attribute}_#{value.to_s.gsub(/\s/, "_").gsub(/[^-\w]/, "").downcase}"
def sanitize_attribute_name(value) #:nodoc:
"#{sanitized_method_name}_#{value.to_s.gsub(/\s/, "_").gsub(/[^-\w]/, "").downcase}"
end
def render_collection #:nodoc:
@ -55,7 +55,7 @@ def render_collection #:nodoc:
@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)
default_html_options = default_html_options_for_collection(item, value)
rendered_item = yield value, text, default_html_options
@ -67,11 +67,11 @@ 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]
def wrap_rendered_collection(collection)
wrapper_tag = @options[:collection_wrapper_tag]
if wrapper_tag
wrapper_class = options[:collection_wrapper_class]
wrapper_class = @options[:collection_wrapper_class]
@template_object.content_tag(wrapper_tag, collection, :class => wrapper_class)
else
collection