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
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
101
102
103
104
105
106
|
# File 'app/helpers/effective_datatables_helper.rb', line 5
def render_datatable(datatable, input_js: {}, buttons: true, charts: true, download: nil, entries: true, filters: true, inline: false, namespace: nil, pagination: true, search: true, simple: false, short: false, sort: true)
raise 'expected datatable to be present' unless datatable
raise 'expected input_js to be a Hash' unless input_js.kind_of?(Hash)
if download.nil?
download = (buttons && EffectiveDatatables.download)
end
if simple
buttons = charts = download = entries = filters = = search = sort = false
end
if short
entries = = false
end
datatable.attributes[:inline] = true if inline
datatable.attributes[:sortable] = false unless sort
datatable.attributes[:searchable] = false unless search
datatable.attributes[:downloadable] = false unless download
datatable.attributes[:namespace] = namespace if namespace
datatable.view ||= self
datatable.state[:length] = 9999999 if simple
unless EffectiveDatatables.authorized?(controller, :index, datatable.collection_class)
return content_tag(:p, "You are not authorized to view this datatable. (cannot :index, #{datatable.collection_class})")
end
charts = charts && datatable._charts.present?
filters = filters && (datatable._scopes.present? || datatable._filters.present?)
html_class = ['effective-datatable', datatable.html_class, ('hide-sort' unless sort), ('hide-search' unless search), ('hide-buttons' unless buttons)].compact.join(' ')
if datatable.reorder? && !buttons
buttons = true; input_js[:buttons] = false
end
input_js[:dom] ||= [
("<'row'<'col-sm-12 dataTables_buttons'B>>" if buttons),
"<'row'<'col-sm-12'tr>>",
("<'row'" if entries || ),
("<'col-sm-6 dataTables_entries'il>" if entries),
("<'col-sm-6'p>" if ),
(">" if entries || )
].compact.join
effective_datatable_params = {
id: datatable.to_param,
class: html_class,
data: {
'all-label' => I18n.t('effective_datatables.all'),
'attributes' => EffectiveDatatables.encrypt(datatable.attributes),
'authenticity-token' => form_authenticity_token,
'buttons-html' => datatable_buttons(datatable),
'columns' => datatable_columns(datatable),
'default-visibility' => datatable.default_visibility.to_json,
'display-length' => datatable.display_length,
'display-order' => datatable_display_order(datatable),
'display-records' => datatable.to_json[:recordsFiltered],
'display-start' => datatable.display_start,
'inline' => inline.to_s,
'language' => EffectiveDatatables.language(I18n.locale),
'length-menu' => (datatable),
'options' => input_js.to_json,
'reorder' => datatable.reorder?.to_s,
'reorder-index' => (datatable.columns[:_reorder][:index] if datatable.reorder?).to_s,
'simple' => simple.to_s,
'spinner' => icon('spinner'), 'source' => effective_datatables.datatable_path(datatable, {format: 'json'}),
'total-records' => datatable.to_json[:recordsTotal]
}
}
retval = if (charts || filters)
output = ''.html_safe
if charts
datatable._charts.each { |name, _| output << render_datatable_chart(datatable, name) }
end
if filters
output << render_datatable_filters(datatable)
end
output << render(partial: 'effective/datatables/datatable',
locals: { datatable: datatable, effective_datatable_params: effective_datatable_params }
)
output
else
render(partial: 'effective/datatables/datatable',
locals: { datatable: datatable, effective_datatable_params: effective_datatable_params }
)
end
Rails.logger.info(" Rendered datatable #{datatable.class} #{datatable.source_location}")
retval
end
|