Module: FatFreeCRM::Sortable::ClassMethods

Defined in:
lib/fat_free_crm/sortable.rb

Instance Method Summary collapse

Instance Method Details

#sort_by_mapObject

Return hash that maps sort options to the actual :order strings, for example:

"first_name" => "leads.first_name ASC",
"last_name"  => "leads.last_name ASC"



34
35
36
37
38
39
40
# File 'lib/fat_free_crm/sortable.rb', line 34

def sort_by_map
  Hash[
    sort_by_fields.zip(sort_by_clauses).map do |field, clause|
      [field, name.tableize + "." + clause]
    end
  ]
end

#sortable(options = {}) ⇒ Object

Model class method to define sort options, for example:

sortable :by => "first_name ASC"
sortable :by => [ "first_name ASC", "last_name ASC" ]
sortable :by => [ "first_name ASC", "last_name ASC" ], :default => "last_name ASC"



20
21
22
23
24
25
26
27
28
# File 'lib/fat_free_crm/sortable.rb', line 20

def sortable(options = {})
  cattr_accessor :sort_by,            # Default sort order with prepended table name.
                 :sort_by_fields,     # Array of fields to sort by without ASC/DESC.
                 :sort_by_clauses     # A copy of sortable :by => ... stored as array.

  self.sort_by_clauses = [options[:by]].flatten
  self.sort_by_fields = sort_by_clauses.map(&:split).map(&:first)
  self.sort_by = name.tableize + "." + (options[:default] || options[:by].first)
end