Module: Formtastic::Inputs::Base::Collections

Included in:
CheckBoxesInput, RadioInput, SelectInput
Defined in:
lib/formtastic/inputs/base/collections.rb

Instance Method Summary collapse

Instance Method Details

#collectionObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/formtastic/inputs/base/collections.rb', line 37

def collection
  # Return if we have a plain string
  return raw_collection if raw_collection.instance_of?(String) || raw_collection.instance_of?(ActiveSupport::SafeBuffer)

  # Return if we have an Array of strings, fixnums or arrays
  return raw_collection if (raw_collection.instance_of?(Array) || raw_collection.instance_of?(Range)) &&
                       [Array, Fixnum, String, Symbol].include?(raw_collection.first.class) &&
                       !(options.include?(:label_method) || options.include?(:value_method))
               
  raw_collection.map { |o| [send_or_call(label_method, o), send_or_call(value_method, o)] }
end

#collection_for_booleanObject



74
75
76
77
78
79
80
81
# File 'lib/formtastic/inputs/base/collections.rb', line 74

def collection_for_boolean
  true_text = options[:true] || Formtastic::I18n.t(:yes)
  false_text = options[:false] || Formtastic::I18n.t(:no)

  # TODO options[:value_as_class] = true unless options.key?(:value_as_class)
      
  [ [true_text, true], [false_text, false] ]
end

#collection_from_associationObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/formtastic/inputs/base/collections.rb', line 55

def collection_from_association
  if reflection
    raise PolymorphicInputWithoutCollectionError.new("A collection must be supplied for #{method} input. Collections cannot be guessed for polymorphic associations.") if reflection.options[:polymorphic] == true

    find_options_from_options = options[:find_options] || {}
    conditions_from_options = find_options_from_options[:conditions] || {}
    conditions_from_reflection = reflection.options[:conditions] || {}
    
    if conditions_from_options.any?
      reflection.klass.where(
        conditions_from_reflection.merge(conditions_from_options)
      )
    else
      find_options_from_options.merge!(:include => group_by) if self.respond_to?(:group_by) && group_by
      reflection.klass.where(conditions_from_reflection.merge(find_options_from_options))
    end
  end
end

#collection_from_optionsObject



49
50
51
52
53
# File 'lib/formtastic/inputs/base/collections.rb', line 49

def collection_from_options
  items = options[:collection]
  items = items.to_a if items.is_a?(Hash)
  items
end

#label_and_value_method(_collection, grouped = false) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/formtastic/inputs/base/collections.rb', line 14

def label_and_value_method(_collection, grouped=false)
  sample = _collection.first || _collection.last
      
  case sample
  when Array
    label, value = :first, :last
  when Integer
    label, value = :to_s, :to_i
  when String, NilClass
    label, value = :to_s, :to_s
  end
      
  # Order of preference: user supplied method, class defaults, auto-detect
  label = (grouped ? options[:grouped_label_method] : options[:label_method]) || label || builder.collection_label_methods.find { |m| sample.respond_to?(m) }
  value = (grouped ? options[:grouped_value_method] : options[:value_method]) || value || builder.collection_value_methods.find { |m| sample.respond_to?(m) }
      
  [label, value]
end

#label_methodObject



6
7
8
# File 'lib/formtastic/inputs/base/collections.rb', line 6

def label_method
  label_and_value_method(raw_collection).first
end

#raw_collectionObject



33
34
35
# File 'lib/formtastic/inputs/base/collections.rb', line 33

def raw_collection
  @raw_collection ||= (collection_from_options || collection_from_association || collection_for_boolean)
end

#send_or_call(duck, object) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/formtastic/inputs/base/collections.rb', line 83

def send_or_call(duck, object)
  if duck.is_a?(Proc)
    duck.call(object)
  else
    object.send(duck)
  end
end

#value_methodObject



10
11
12
# File 'lib/formtastic/inputs/base/collections.rb', line 10

def value_method
  label_and_value_method(raw_collection).last
end