Module: Zable::Sort::ActiveRecord::ClassMethods

Defined in:
lib/zable/sort.rb

Instance Method Summary collapse

Instance Method Details

#inherited(subclass) ⇒ Object



7
8
9
10
11
12
# File 'lib/zable/sort.rb', line 7

def inherited(subclass)
  subclass.class_eval do
    scope :for_sort_params, -> sort_params { inject_sort_scope(sort_params) }
  end
  super(subclass)
end

#sortable(*attr_names) ⇒ Object

Allows Model.for_sort_params to sort for the given attr_names for the common sorting case (i.e. “attr_name ASC” or “attr_name DESC”)

sortable :last_name, :created_at


18
19
20
21
22
# File 'lib/zable/sort.rb', line 18

def sortable(*attr_names)
  attr_names.each do |attr_name|
    scope "sort_#{attr_name}", -> options { order("#{table_name}.#{attr_name} #{options[:order]}") }
  end
end