Module: Formtastic::Inputs::Base::GroupedCollections

Included in:
SelectInput
Defined in:
lib/formtastic/inputs/base/grouped_collections.rb

Instance Method Summary collapse

Instance Method Details

#group_associationObject



26
27
28
# File 'lib/formtastic/inputs/base/grouped_collections.rb', line 26

def group_association
  @group_association ||= (group_association_from_options || group_association_from_reflection)
end

#group_association_from_optionsObject



30
31
32
# File 'lib/formtastic/inputs/base/grouped_collections.rb', line 30

def group_association_from_options
  options[:group_association]
end

#group_association_from_reflectionObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/formtastic/inputs/base/grouped_collections.rb', line 38

def group_association_from_reflection
  method_to_group_association_by = reflection.klass.reflect_on_association(group_by)
  group_class = method_to_group_association_by.klass
      
  # This will return in the normal case
  return method.to_s.pluralize.to_sym if group_class.reflect_on_association(method.to_s.pluralize)
      
  # This is for belongs_to associations named differently than their class
  # form.input :parent, :group_by => :customer
  # eg.
  # class Project
  #   belongs_to :parent, :class_name => 'Project', :foreign_key => 'parent_id'
  #   belongs_to :customer
  # end
  # class Customer
  #   has_many :projects
  # end
  group_method = group_class.to_s.underscore.pluralize.to_sym
  return group_method if group_class.reflect_on_association(group_method) # :projects
      
  # This is for has_many associations named differently than their class
  # eg.
  # class Project
  #   belongs_to :parent, :class_name => 'Project', :foreign_key => 'parent_id'
  #   belongs_to :customer
  # end
  # class Customer
  #   has_many :tasks, :class_name => 'Project', :foreign_key => 'customer_id'
  # end
  possible_associations = group_class.reflect_on_all_associations(:has_many).find_all {|assoc| assoc.klass == reflection.klass }
  return possible_associations.first.name.to_sym if possible_associations.count == 1
      
  raise "Cannot infer group association for #{method} grouped by #{group_by}, there were #{possible_associations.empty? ? 'no' : possible_associations.size} possible associations. Please specify using :group_association"
end

#group_byObject



34
35
36
# File 'lib/formtastic/inputs/base/grouped_collections.rb', line 34

def group_by
  options[:group_by]
end

#group_label_methodObject



14
15
16
# File 'lib/formtastic/inputs/base/grouped_collections.rb', line 14

def group_label_method
  @group_label_method ||= (group_label_method_from_options || group_label_method_from_grouped_collection)
end

#group_label_method_from_grouped_collectionObject



22
23
24
# File 'lib/formtastic/inputs/base/grouped_collections.rb', line 22

def group_label_method_from_grouped_collection
  label_and_value_method(raw_grouped_collection, true).first
end

#group_label_method_from_optionsObject



18
19
20
# File 'lib/formtastic/inputs/base/grouped_collections.rb', line 18

def group_label_method_from_options
  options[:group_label_method]
end

#grouped_collectionObject



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

def grouped_collection
  @grouped_collection ||= raw_grouped_collection.sort_by { |group_item| group_item.send(group_label_method) }
end

#raw_grouped_collectionObject



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

def raw_grouped_collection
  @raw_grouped_collection ||= raw_collection.map { |option| option.send(options[:group_by]) }.uniq
end