Class: TableSortable::Column::Sorter

Inherits:
Object
  • Object
show all
Includes:
TableSortable::Concerns::Proc
Defined in:
lib/table_sortable/column/sorter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TableSortable::Concerns::Proc

#detect_method, #disabled?, #method

Constructor Details

#initialize(*args) ⇒ Sorter

Returns a new instance of Sorter.



8
9
10
# File 'lib/table_sortable/column/sorter.rb', line 8

def initialize(*args)
  super :sort, *args
end

Instance Attribute Details

#sort_orderObject

Returns the value of attribute sort_order.



6
7
8
# File 'lib/table_sortable/column/sorter.rb', line 6

def sort_order
  @sort_order
end

Instance Method Details

#active_record_procObject



16
17
18
# File 'lib/table_sortable/column/sorter.rb', line 16

def active_record_proc
  -> (sort_order, col=nil) { order(col.name.to_s.underscore => sort_order) }
end

#array_procObject



12
13
14
# File 'lib/table_sortable/column/sorter.rb', line 12

def array_proc
  -> (sort_order, col=nil) { sort{ |a,b| col.value(sort_order == :asc ? a : b) <=> col.value(sort_order == :asc ? b : a) } }
end

#proc_wrapper(proc) ⇒ Object



20
21
22
# File 'lib/table_sortable/column/sorter.rb', line 20

def proc_wrapper(proc)
  -> (sort_order, col=nil) { instance_exec(sort_order , &proc) }
end

#run(records) ⇒ Object



24
25
26
# File 'lib/table_sortable/column/sorter.rb', line 24

def run(records)
  records.instance_exec(sort_order, column, &proc)
end

#used?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/table_sortable/column/sorter.rb', line 28

def used?
  !sort_order.nil?
end