Module: Formtastic::Inputs::Base::Collections
- Included in:
- CheckBoxesInput, RadioInput, SelectInput
- Defined in:
- lib/formtastic/inputs/base/collections.rb
Instance Method Summary collapse
- #collection ⇒ Object
- #collection_for_boolean ⇒ Object
- #collection_from_association ⇒ Object
- #collection_from_options ⇒ Object
- #label_and_value_method(_collection, grouped = false) ⇒ Object
- #label_method ⇒ Object
- #raw_collection ⇒ Object
- #send_or_call(duck, object) ⇒ Object
- #value_method ⇒ Object
Instance Method Details
#collection ⇒ Object
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) && !(.include?(:label_method) || .include?(:value_method)) raw_collection.map { |o| [send_or_call(label_method, o), send_or_call(value_method, o)] } end |
#collection_for_boolean ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/formtastic/inputs/base/collections.rb', line 74 def collection_for_boolean true_text = [:true] || Formtastic::I18n.t(:yes) false_text = [: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_association ⇒ Object
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.[:polymorphic] == true = [:find_options] || {} = [:conditions] || {} conditions_from_reflection = reflection.[:conditions] || {} if .any? reflection.klass.where( conditions_from_reflection.merge() ) else .merge!(:include => group_by) if self.respond_to?(:group_by) && group_by reflection.klass.where(conditions_from_reflection.merge()) end end end |
#collection_from_options ⇒ Object
49 50 51 52 53 |
# File 'lib/formtastic/inputs/base/collections.rb', line 49 def items = [: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 ? [:grouped_label_method] : [:label_method]) || label || builder.collection_label_methods.find { |m| sample.respond_to?(m) } value = (grouped ? [:grouped_value_method] : [:value_method]) || value || builder.collection_value_methods.find { |m| sample.respond_to?(m) } [label, value] end |
#label_method ⇒ Object
6 7 8 |
# File 'lib/formtastic/inputs/base/collections.rb', line 6 def label_method label_and_value_method(raw_collection).first end |
#raw_collection ⇒ Object
33 34 35 |
# File 'lib/formtastic/inputs/base/collections.rb', line 33 def raw_collection @raw_collection ||= ( || 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_method ⇒ Object
10 11 12 |
# File 'lib/formtastic/inputs/base/collections.rb', line 10 def value_method label_and_value_method(raw_collection).last end |