aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-02-01 16:44:35 -0200
committerCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-02-02 09:40:24 -0200
commit36cb7150fd4087c22b61f536431c149d4c545586 (patch)
tree645b01629ca81dda23aa13a7bd20f7c3f9694080 /actionpack
parent17d214a1d4c71db39d2a4cab4d18ccea9f5b8ab5 (diff)
downloadrails-36cb7150fd4087c22b61f536431c149d4c545586.tar.gz
rails-36cb7150fd4087c22b61f536431c149d4c545586.tar.bz2
rails-36cb7150fd4087c22b61f536431c149d4c545586.zip
Extract common collection helpers to a module to avoid too much inheritance
[Carlos Antonio da Silva + Rafael Mendonça França]
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_view/helpers/tags/collection_check_boxes.rb6
-rw-r--r--actionpack/lib/action_view/helpers/tags/collection_helpers.rb84
-rw-r--r--actionpack/lib/action_view/helpers/tags/collection_radio_buttons.rb72
3 files changed, 93 insertions, 69 deletions
diff --git a/actionpack/lib/action_view/helpers/tags/collection_check_boxes.rb b/actionpack/lib/action_view/helpers/tags/collection_check_boxes.rb
index 92312c5081..8f0f5c89fe 100644
--- a/actionpack/lib/action_view/helpers/tags/collection_check_boxes.rb
+++ b/actionpack/lib/action_view/helpers/tags/collection_check_boxes.rb
@@ -1,7 +1,11 @@
+require 'action_view/helpers/tags/collection_helpers'
+
module ActionView
module Helpers
module Tags
- class CollectionCheckBoxes < CollectionRadioButtons
+ class CollectionCheckBoxes < Base
+ include CollectionHelpers
+
class CheckBoxBuilder < Builder
def check_box(extra_html_options={})
html_options = extra_html_options.merge(@input_html_options)
diff --git a/actionpack/lib/action_view/helpers/tags/collection_helpers.rb b/actionpack/lib/action_view/helpers/tags/collection_helpers.rb
new file mode 100644
index 0000000000..1c9ba8598f
--- /dev/null
+++ b/actionpack/lib/action_view/helpers/tags/collection_helpers.rb
@@ -0,0 +1,84 @@
+module ActionView
+ module Helpers
+ module Tags
+ module CollectionHelpers
+ class Builder
+ def initialize(template_object, object_name, method_name,
+ sanitized_attribute_name, text, value, input_html_options)
+ @template_object = template_object
+ @object_name = object_name
+ @method_name = method_name
+ @sanitized_attribute_name = sanitized_attribute_name
+ @text = text
+ @value = value
+ @input_html_options = input_html_options
+ end
+
+ def label(label_html_options={}, &block)
+ @template_object.label(@object_name, @sanitized_attribute_name, @text, label_html_options, &block)
+ end
+ end
+
+ def initialize(object_name, method_name, template_object, collection, value_method, text_method, options, html_options)
+ @collection = collection
+ @value_method = value_method
+ @text_method = text_method
+ @html_options = html_options
+
+ super(object_name, method_name, template_object, options)
+ end
+
+ private
+
+ 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
+ # :disabled.
+ def default_html_options_for_collection(item, value) #: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(value) #:nodoc:
+ "#{sanitized_method_name}_#{sanitized_value(value)}"
+ end
+
+ 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)
+
+ yield value, text, default_html_options
+ end.join.html_safe
+ end
+
+ def value_for_collection(item, value) #:nodoc:
+ value.respond_to?(:call) ? value.call(item) : item.send(value)
+ end
+ end
+ 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
index ed64a3cfbd..d11f8632b9 100644
--- a/actionpack/lib/action_view/helpers/tags/collection_radio_buttons.rb
+++ b/actionpack/lib/action_view/helpers/tags/collection_radio_buttons.rb
@@ -1,23 +1,10 @@
+require 'action_view/helpers/tags/collection_helpers'
+
module ActionView
module Helpers
module Tags
- class CollectionRadioButtons < CollectionSelect
- class Builder
- def initialize(template_object, object_name, method_name,
- sanitized_attribute_name, text, value, input_html_options)
- @template_object = template_object
- @object_name = object_name
- @method_name = method_name
- @sanitized_attribute_name = sanitized_attribute_name
- @text = text
- @value = value
- @input_html_options = input_html_options
- end
-
- def label(label_html_options={}, &block)
- @template_object.label(@object_name, @sanitized_attribute_name, @text, label_html_options, &block)
- end
- end
+ class CollectionRadioButtons < Base
+ include CollectionHelpers
class RadioButtonBuilder < Builder
def radio_button(extra_html_options={})
@@ -37,57 +24,6 @@ module ActionView
end
end
end
-
- private
-
- 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
- # :disabled.
- def default_html_options_for_collection(item, value) #: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(value) #:nodoc:
- "#{sanitized_method_name}_#{sanitized_value(value)}"
- end
-
- 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)
-
- yield value, text, default_html_options
- end.join.html_safe
- end
-
- def value_for_collection(item, value) #:nodoc:
- value.respond_to?(:call) ? value.call(item) : item.send(value)
- end
end
end
end