Sha256: fdbf4b4adc8684dc83c9d1d38ee97bee1dbf29dc87d2e7221f4b61edaae66d8f

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

module WithFilters
  module Filter
    # @private
    class Collection < Array
      def initialize(field_name, choices, options = {})
        choices = choices.to_a if choices.is_a?(Range)

        selected = Array.wrap(options[:selected]).map(&:to_s)

        choices.map do |element|
          text, value, choice_options = if element.is_a?(Array)
                                          html_attrs = element.detect {|e| Hash === e} || {}
                                          element = element.reject {|e| Hash === e}  
                                          [element.first.to_s, element.last, html_attrs]
                                        elsif !element.is_a?(String) && element.respond_to?(:first) && element.respond_to?(:last)
                                          [element.first.to_s, element.last, {}] 
                                        else
                                          [element.to_s, element, {}] 
                                        end

          choice_options[:selected] = 'selected' if selected.include?(value.to_s)

          self.push(Choice.new(field_name, text, value, choice_options))
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
with_filters-0.1.2 lib/with_filters/models/filter/collection.rb
with_filters-0.1.1 lib/with_filters/models/filter/collection.rb
with_filters-0.1.0 lib/with_filters/models/filter/collection.rb