Class: TableSortable::Column::Filter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TableSortable::Concerns::Proc

#detect_method, #disabled?, #method

Constructor Details

#initialize(*args) ⇒ Filter

Returns a new instance of Filter.



8
9
10
11
12
13
# File 'lib/table_sortable/column/filter.rb', line 8

def initialize(*args)
  options = args.extract_options!
  @default_value = options[:filter_initial_value]
  @collection = options[:filter_collection]
  super :filter, options
end

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



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

def collection
  @collection
end

#default_valueObject

Returns the value of attribute default_value.



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

def default_value
  @default_value
end

#queryObject

Returns the value of attribute query.



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

def query
  @query
end

Instance Method Details

#active_record_procObject



19
20
21
# File 'lib/table_sortable/column/filter.rb', line 19

def active_record_proc
  -> (value, col=nil) { where("LOWER(#{col.name.to_s.underscore}) LIKE (?)", "%#{value.to_s.downcase}%") }
end

#array_procObject



15
16
17
# File 'lib/table_sortable/column/filter.rb', line 15

def array_proc
  -> (value, col=nil) { select{|record| col.value(record).to_s.downcase.include? value.downcase} }
end

#proc_wrapper(proc) ⇒ Object



23
24
25
# File 'lib/table_sortable/column/filter.rb', line 23

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

#run(records) ⇒ Object



27
28
29
# File 'lib/table_sortable/column/filter.rb', line 27

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

#used?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/table_sortable/column/filter.rb', line 31

def used?
  !query.nil?
end