Module: EffectiveDatatablesPrivateHelper

Defined in:
app/helpers/effective_datatables_private_helper.rb

Overview

These aren't expected to be called by a developer. They are internal methods.

Instance Method Summary collapse

Instance Method Details

#datatable_buttons(datatable, search: true, dom_id: datatable.to_param) ⇒ Object



28
29
30
# File 'app/helpers/effective_datatables_private_helper.rb', line 28

def datatable_buttons(datatable, search: true, dom_id: datatable.to_param)
  render('/effective/datatables/buttons', datatable: datatable, search: search, dom_id: dom_id).gsub("'", '"').html_safe
end

#datatable_columns(datatable) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/helpers/effective_datatables_private_helper.rb', line 7

def datatable_columns(datatable)
  sortable = datatable.sortable?

  datatable.columns.map do |name, opts|
    {
      className: opts[:col_class],
      name: name,
      responsivePriority: opts[:responsive],
      search: datatable.state[:search][name],
      searchHtml: datatable_search_tag(datatable, name, opts),
      sortable: (opts[:sort] && sortable),
      title: datatable_label_tag(datatable, name, opts),
      visible: datatable.state[:visible][name]
    }
  end.to_json.html_safe
end

#datatable_display_order(datatable) ⇒ Object



24
25
26
# File 'app/helpers/effective_datatables_private_helper.rb', line 24

def datatable_display_order(datatable)
  ((datatable.sortable? && datatable.order_index) ? [datatable.order_index, datatable.order_direction] : false).to_json.html_safe
end

#datatable_filter_tag(form, datatable, name, opts) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/helpers/effective_datatables_private_helper.rb', line 162

def datatable_filter_tag(form, datatable, name, opts)
  return if opts[:visible] == false

  as = opts[:as].to_s.chomp('_field').to_sym
  value = datatable.state[:filter][name]
  collection = opts[:collection]

  options = {
    autocomplete: 'off',
    feedback: false,
    label: false,
    placeholder: (opts[:label] || name.to_s.titleize),
    value: value,
    wrapper: { class: 'form-group col-auto'}
  }.merge(opts.except(:as, :collection, :parse, :value))

  options[:name] = '' unless datatable._filters_form_required?

  if [:select, :radios, :checks].include?(as)
    options.delete(:name) unless as == :select
    form.public_send(as, name, collection, options) # select, radios, checks
  elsif as == :boolean
    collection ||= [true, false].map { |value| [t("effective_datatables.boolean_#{value}"), value] }
    form.public_send(:select, name, collection, options) # boolean
  elsif as == :string
    form.public_send(:text_field, name, options)
  elsif form.respond_to?(as)
    form.public_send(as, name, options) # check_box, text_area
  else
    form.public_send("#{as}_field", name, options) # text_field, number_field, all the rest.
  end

end

#datatable_human_attribute_name(datatable, name, opts) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/helpers/effective_datatables_private_helper.rb', line 85

def datatable_human_attribute_name(datatable, name, opts)
  return (name.to_s.split('.').last || '').titleize unless datatable.active_record_collection?

  case opts[:as]
  when :belongs_to
    foreign_key = opts[:resource].initialized_name.try(:foreign_key).to_s.downcase
    klass_name = opts[:resource].initialized_name.try(:class_name).to_s.downcase

    if foreign_key.starts_with?(klass_name)
      opts[:resource].human_name
    else
      datatable.collection_class.human_attribute_name(name)
    end
  when :has_many
    opts[:resource].human_plural_name
  else
    datatable.collection_class.human_attribute_name(name)
  end
end

#datatable_label_tag(datatable, name, opts) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/helpers/effective_datatables_private_helper.rb', line 71

def datatable_label_tag(datatable, name, opts)
  case opts[:as]
  when :actions
    (:span, t('effective_datatables.actions'), style: 'display: none;')
  when :bulk_actions
    (:span, t('effective_datatables.bulk_actions'), style: 'display: none;')
  when :reorder
    (:span, t('effective_datatables.reorder'), style: 'display: none;')
  else
    label = opts[:label].presence || datatable_human_attribute_name(datatable, name, opts)
    (:span, label)
  end
end

#datatable_length_menu(datatable) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'app/helpers/effective_datatables_private_helper.rb', line 32

def datatable_length_menu(datatable)
  length_menu = datatable.length_menu

  if length_menu.present? 
    raise('expected datatable length_menu to be an Array of Integers') unless length_menu.kind_of?(Array) && length_menu.all? { |i| i.kind_of?(Integer) }
  end
  
  length_menu ||= [5, 10, 25, 50, 100, 250, 500]

  [length_menu, length_menu.map(&:to_s)]
end

#datatable_new_resource_button(datatable, name, column) ⇒ Object



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
# File 'app/helpers/effective_datatables_private_helper.rb', line 44

def datatable_new_resource_button(datatable, name, column)
  return unless datatable.inline? && (column[:actions][:new] != false)

  btn_class = EffectiveDatatables.new_action_button_class || 'btn-sm btn-success'
  action = { action: :new, class: "btn #{btn_class}", 'data-remote': true }

  # Pass actions_col(new: { label: 'New Thing' }) to override the button label on one datatable.
  label = t('effective_datatables.new')

  if column[:actions][:new].kind_of?(Hash) # This might be active_record_array_collection?
    new_opts = column[:actions][:new]
    label = new_opts[:label] if new_opts[:label].present?
    action = action.merge(new_opts.except(:label, :klass))

    effective_resource = (datatable.effective_resource || datatable.fallback_effective_resource)
    klass = (new_opts[:klass] || effective_resource&.klass || datatable.collection_class)
  elsif Array(datatable.effective_resource&.actions).include?(:new)
    effective_resource = datatable.effective_resource
    klass = effective_resource.klass
  else
    return
  end

  # Will only work if permitted
  render_resource_actions(klass, actions: { label => action }, effective_resource: effective_resource)
end

#datatable_scope_tag(form, datatable, opts = {}) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'app/helpers/effective_datatables_private_helper.rb', line 196

def datatable_scope_tag(form, datatable, opts = {})
  collection = datatable._scopes.map { |name, opts| [opts[:label], name] }
  value = datatable.state[:scope]

  options = {
    autocomplete: 'off',
    buttons: true,
    checked: value,
    feedback: false,
    label: (datatable._filters.present? ? t("effective_datatables.display") : false),
    required: false,
    wrapper: { class: 'form-group col-auto'}
  }.merge(opts.except(:checked, :value))

  form.radios :scope, collection, options
end

#datatable_search_tag(datatable, name, opts) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/helpers/effective_datatables_private_helper.rb', line 105

def datatable_search_tag(datatable, name, opts)
  return datatable_new_resource_button(datatable, name, opts) if name == :_actions

  return if opts[:search] == false

  # Build the search
  @_effective_datatables_form_builder || effective_form_with(scope: 'datatable_search', url: '#') { |f| @_effective_datatables_form_builder = f }
  form = @_effective_datatables_form_builder

  collection = opts[:search].delete(:collection)
  value = datatable.state[:search][name]

  options = opts[:search].merge(
    name: nil,
    feedback: false,
    label: false,
    value: value,
    data: { 'column-name': name, 'column-index': opts[:index] }
  )

  options.delete(:fuzzy)

  case options.delete(:as)
  when :string, :text, :number
    form.text_field name, options
  when :date, :datetime
    form.date_field name, options.reverse_merge(
      date_linked: false, prepend: false, input_js: { useStrict: true, keepInvalid: true }
    )
  when :time
    form.time_field name, options.reverse_merge(
      date_linked: false, prepend: false, input_js: { useStrict: false, keepInvalid: true }
    )
  when :select, :boolean
    options[:input_js] = (options[:input_js] || {}).reverse_merge(placeholder: '')

    form.select name, collection, options
  when :bulk_actions
    options[:data]['role'] = 'bulk-actions'
    form.check_box name, options.merge(label: ' ')
  end
end

#render_datatable_chart(datatable, name) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
# File 'app/helpers/effective_datatables_private_helper.rb', line 222

def render_datatable_chart(datatable, name)
  raise 'expected datatable to be present' unless datatable

  datatable.view ||= self
  return unless datatable._charts[name].present?

  chart = datatable._charts[name]
  chart_data = datatable.to_json[:charts][name][:data]

  render partial: chart[:partial], locals: { datatable: datatable, chart: chart, chart_data: chart_data }
end

#render_datatable_charts(datatable) ⇒ Object



213
214
215
216
217
218
219
220
# File 'app/helpers/effective_datatables_private_helper.rb', line 213

def render_datatable_charts(datatable)
  raise 'expected datatable to be present' unless datatable

  datatable.view ||= self
  return unless datatable._charts.present?

  datatable._charts.map { |name, _| render_datatable_chart(datatable, name) }.join.html_safe
end

#render_datatable_filters(datatable, dom_id: datatable.to_param) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/helpers/effective_datatables_private_helper.rb', line 148

def render_datatable_filters(datatable, dom_id: datatable.to_param)
  raise 'expected datatable to be present' unless datatable

  datatable.view ||= self
  return unless datatable._scopes.present? || datatable._filters.present?

  if datatable._filters_form_required?
    render('effective/datatables/filters', datatable: datatable, dom_id: dom_id)
  else
    render('effective/datatables/filters', datatable: datatable, dom_id: dom_id).gsub('<form', '<div').gsub('/form>', '/div>').html_safe
  end

end