-
Notifications
You must be signed in to change notification settings - Fork 2
/
city_datatable.rb
59 lines (41 loc) · 1.39 KB
/
city_datatable.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# frozen_string_literal: true
class CityDatatable < ApplicationDatatable
def view_columns
@view_columns ||= {
check_box: { source: 'City.id', orderable: false, searchable: false },
name: { source: 'City.name' },
iata: { source: 'City.iata' },
country_name: { source: 'City.country_id', cond: filter_country_condition },
custom_column: { source: 'custom_column', cond: filter_custom_column_condition },
}
end
def data
records.map do |record|
{
check_box: record.decorate.to_checkbox(selected: selected.include?(record.id)),
name: record.name,
iata: record.iata,
country_name: record.country.try(:name),
custom_column: record[:custom_column],
}
end
end
def get_raw_records
City.select('cities.*, timezone AS custom_column').includes(:country)
end
def additional_data
super.merge({
dt_dropdown_data(:country_name) => select_options_for_country_name,
})
end
def records(opts = {})
super opts.merge(decorate: false)
end
private
def filter_country_condition
->(column, value) { column.table[column.field].eq(column.search.value.to_i + 1) }
end
def filter_custom_column_condition
->(column, value) { ::Arel::Nodes::SqlLiteral.new(column.field.to_s).matches("#{ column.search.value }%") }
end
end