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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'app/datatables/olivander/datatable.rb', line 18
def self.auto_datatable(klazz, collection: nil, link_path: nil, only: [], except: [], hide: [], show: [], order_by: [], scopes: [])
Rails.logger.debug "initializing datatable for #{klazz}"
instance = klazz.new
klazz_attributes = instance.attributes.collect { |x| x[0] }
column_attributes = klazz_attributes
column_attributes &&= only if only.size.positive?
column_attributes -= except if except.size.positive?
resources_sym = klazz.table_name.to_sym
bulk_action_list = Olivander::CurrentContext.build.application_context.route_builder.resources[resources_sym]&.datatable_bulk_actions || []
default_hidden = %w[
id created updated created_at updated_at
deleted_at current_user current_action
application_tenant_id created_by_id updated_by_id
]
filters do
scopes.each do |s|
scope s
end
end
collection do
dc = collection.nil? ? klazz.all : collection
attributes.each do |att|
dc = dc.where("#{att[0]} = ?", att[1]) if klazz_attributes.include?(att[0].to_s)
end
dc
end
if bulk_action_list.size.positive?
bulk_actions do
bulk_action_list.each do |ma|
label = resource_form_action_label(instance, ma.action)
if ma.confirm
bulk_action label, url_for(controller: ma.controller, action: "confirm_#{ma.action}")
else
bulk_action label, send(ma.path_helper), data: { turbo_frame: ma.turbo_frame }
end
end
end
end
datatable do
order(order_by[0], order_by[1]) if order_by.size == 2
column_attributes.each do |key|
label = field_label_for(klazz, key)
sym = key.gsub('_id', '')
visible = show.include?(key) || !(default_hidden.include?(key) || hide.include?(key))
already_implemented_method = "col_for_#{sym}"
if datatable.respond_to?(already_implemented_method)
datatable.send(already_implemented_method, visible: visible)
elsif %w[id name first_name last_name display_name title].include?(sym)
col sym, visible: visible, action: :show
elsif sym.include?('.')
col sym, visible: visible, label: label
elsif klazz.columns.select { |x| x.name == key }.first&.type == :boolean
col sym, visible: visible, label: label do |c|
val = c.send(sym)
icon_class = val ? 'fa-check text-success' : 'fa-times text-danger'
"<div class='text-center'><i class='fa fa-icon #{icon_class}'></div>".html_safe
end
elsif sym == 'avatar'
col sym, visible: visible, label: label, search: false, sort: false do |c|
"<div class='image'><img style='max-height: 25px; max-width: 25px' class='img-circle elevation-2' src='#{c.send(sym).expiring_url}'></div>".html_safe
end
else
reflection = instance.class.reflections[sym]
if reflection.present?
col sym, visible: visible, label: label, search: reflection.klass.all
else
col sym, visible: visible, label: label
end
end
end
actions_col
end
end
|