Module: AjaxDatatablesRails::ORM::ActiveRecord

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

Instance Method Summary collapse

Instance Method Details

#build_conditionsObject

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



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

def build_conditions
  @build_conditions ||= begin
    criteria = [build_conditions_for_selected_columns]
    criteria << build_conditions_for_datatable if datatable.searchable?
    criteria.compact.reduce(:and)
  end
end

#build_conditions_for_datatableObject

rubocop:disable Metrics/AbcSize



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

def build_conditions_for_datatable
  columns  = searchable_columns.reject(&:searched?)
  criteria = search_for.inject([]) do |crit, atom|
    search = Datatable::SimpleSearch.new(value: atom, regex: datatable.search.regexp?)
    crit << columns.map do |simple_column|
      simple_column.search = search
      simple_column.search_query
    end.compact.reduce(:or)
  end.compact.reduce(:and)
  criteria
end

#build_conditions_for_selected_columnsObject

rubocop:enable Metrics/AbcSize



50
51
52
# File 'lib/ajax-datatables-rails/orm/active_record.rb', line 50

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

#filter_records(records) ⇒ Object



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

def filter_records(records)
  records.where(build_conditions)
end

#paginate_records(records) ⇒ Object

rubocop:enable Style/EachWithObject, Style/SafeNavigation



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

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

#search_forObject



54
55
56
# File 'lib/ajax-datatables-rails/orm/active_record.rb', line 54

def search_for
  datatable.search.value.split(global_search_delimiter)
end

#sort_records(records) ⇒ Object

rubocop:disable Style/EachWithObject, Style/SafeNavigation



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

def sort_records(records)
  sort_by = datatable.orders.inject([]) do |queries, order|
    column = order.column
    queries << order.query(column.sort_query) if column && column.orderable?
    queries
  end
  records.order(Arel.sql(sort_by.join(', ')))
end