Class: Tableficate::Filter::Collection

Inherits:
Array
  • Object
show all
Defined in:
lib/tableficate/filters/collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(container, options = {}) ⇒ Collection

Returns a new instance of Collection.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tableficate/filters/collection.rb', line 4

def initialize(container, options = {})
  container = container.to_a if container.is_a?(Range)

  selected = Array.wrap(options[:selected]).map{|item| item.to_s}
  disabled = Array.wrap(options[:disabled]).map{|item| item.to_s}

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

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

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