collection_helpers.rb 2.7 KB
Newer Older
1 2 3 4 5
module ActionView
  module Helpers
    module Tags
      module CollectionHelpers
        class Builder
6
          attr_reader :object, :text, :value
7

8
          def initialize(template_object, object_name, method_name, object,
9 10 11 12
                         sanitized_attribute_name, text, value, input_html_options)
            @template_object = template_object
            @object_name = object_name
            @method_name = method_name
13
            @object = object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
            @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

36 37
        def instantiate_builder(builder_class, item, value, text, html_options)
          builder_class.new(@template_object, @object_name, @method_name, item,
38 39 40 41 42 43 44 45 46
                            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|
47
            next unless current_value = @options[option]
48

49 50 51 52 53
            accept = if current_value.respond_to?(:call)
              current_value.call(item)
            else
              Array(current_value).include?(value)
            end
54 55 56 57 58 59 60 61

            if accept
              html_options[option] = true
            elsif option == :checked
              html_options[option] = false
            end
          end

62
          html_options[:object] = @object
63 64 65 66 67 68 69 70 71 72 73 74 75
          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)

76
            yield item, value, text, default_html_options
77 78 79 80 81 82
          end.join.html_safe
        end
      end
    end
  end
end