Module: AjaxDatatablesRails::ORM::ActiveRecord

Defined in:
lib/ajax-datatables-rails/orm/active_record.rb

Instance Method Summary collapse

Instance Method Details

#build_conditionsObject

—————– SEARCH HELPER METHODS ——————–



27
28
29
30
31
32
33
# File 'lib/ajax-datatables-rails/orm/active_record.rb', line 27

def build_conditions
  if datatable.searchable?
    build_conditions_for_datatable
  else
    build_conditions_for_selected_columns
  end
end

#build_conditions_for_datatableObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ajax-datatables-rails/orm/active_record.rb', line 35

def build_conditions_for_datatable
  search_for = datatable.search.value.split(' ')
  criteria = search_for.inject([]) do |criteria, atom|
    search = Datatable::SimpleSearch.new({ value: atom, regex: datatable.search.regexp? })
    criteria << searchable_columns.map do |simple_column|
      simple_column.search = search
      simple_column.search_query
    end.reduce(:or)
  end.reduce(:and)
  criteria
end

#build_conditions_for_selected_columnsObject



47
48
49
# File 'lib/ajax-datatables-rails/orm/active_record.rb', line 47

def build_conditions_for_selected_columns
  search_columns.map(&:search_query).reduce(:and)
end

#fetch_recordsObject



5
6
7
# File 'lib/ajax-datatables-rails/orm/active_record.rb', line 5

def fetch_records
  get_raw_records
end

#filter_records(records) ⇒ Object



9
10
11
# File 'lib/ajax-datatables-rails/orm/active_record.rb', line 9

def filter_records records
  records.where(build_conditions)
end

#paginate_records(records) ⇒ Object



21
22
23
# File 'lib/ajax-datatables-rails/orm/active_record.rb', line 21

def paginate_records records
  records.offset(datatable.offset).limit(datatable.per_page)
end

#sort_records(records) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/ajax-datatables-rails/orm/active_record.rb', line 13

def sort_records records
  sort_by = datatable.orders.inject([]) do |queries, order|
    column = order.column
    queries << order.query(column.sort_query) if column
  end
  records.order(sort_by.join(", "))
end