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_bulk_actions(datatable) ⇒ Object



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

def datatable_bulk_actions(datatable)
  if datatable._bulk_actions.present?
    render(partial: '/effective/datatables/bulk_actions_dropdown', locals: { datatable: datatable }).gsub("'", '"').html_safe
  end
end

#datatable_columns(datatable) ⇒ Object



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

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



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

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

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



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 116

def datatable_filter_tag(form, datatable, name, opts)
  placeholder = opts.delete(:label)

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

  options = opts.except(:parse).merge(
    autocomplete: 'off',
    feedback: false,
    label: false,
    placeholder: placeholder,
    value: value,
    wrapper: { class: 'form-group col-auto'}
  )

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

  case options.delete(:as)
  when :date
    form.date_field name, options
  when :datetime
    form.datetime_field name, options
  when :time
    form.time_field name, options
  when :select, :boolean
    options[:input_js] = (options[:input_js] || {}).reverse_merge(placeholder: placeholder)
    form.select name, collection, options
  else
    form.text_field name, options
  end
end

#datatable_label_tag(datatable, name, opts) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/helpers/effective_datatables_private_helper.rb', line 48

def datatable_label_tag(datatable, name, opts)
  case opts[:as]
  when :actions
    (:span, 'Actions', style: 'display: none;')
  when :bulk_actions
    (:span, 'Bulk Actions', style: 'display: none;')
  when :reorder
    (:span, 'Reorder', style: 'display: none;')
  else
    (:span, opts[:label].presence)
  end
end

#datatable_new_resource_button(datatable, name, column) ⇒ Object



41
42
43
44
45
46
# File 'app/helpers/effective_datatables_private_helper.rb', line 41

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

  actions = {'New' => { action: :new, class: ['btn', column[:btn_class].presence].compact.join(' '), 'data-remote': true } }
  render_resource_actions(datatable.resource.klass, actions: actions, effective_resource: datatable.resource) # Will only work if permitted
end

#datatable_reorder(datatable) ⇒ Object



36
37
38
39
# File 'app/helpers/effective_datatables_private_helper.rb', line 36

def datatable_reorder(datatable)
  return unless datatable.reorder? && EffectiveDatatables.authorized?(self, :update, datatable.collection_class)
  link_to((:span, 'Reorder'), '#', class: 'btn btn-link btn-sm buttons-reorder', disabled: true)
end

#datatable_reset(datatable) ⇒ Object



32
33
34
# File 'app/helpers/effective_datatables_private_helper.rb', line 32

def datatable_reset(datatable)
  link_to((:span, 'Reset'), '#', class: 'btn btn-link btn-sm buttons-reset-search')
end

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



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

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

  options = {
    autocomplete: 'off',
    buttons: true,
    checked: datatable.state[:scope],
    feedback: false,
    label: false,
    required: false,
    wrapper: { class: 'form-group col-auto'}
  }.merge(opts)

  form.radios :scope, collection, options
end

#datatable_search_tag(datatable, name, opts) ⇒ Object



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/helpers/effective_datatables_private_helper.rb', line 61

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].except(:fuzzy).merge!(
    name: nil,
    feedback: false,
    label: false,
    value: value,
    data: { 'column-name': name, 'column-index': opts[:index] }
  )

  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



173
174
175
176
177
178
179
180
181
182
183
# File 'app/helpers/effective_datatables_private_helper.rb', line 173

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



164
165
166
167
168
169
170
171
# File 'app/helpers/effective_datatables_private_helper.rb', line 164

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) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/helpers/effective_datatables_private_helper.rb', line 102

def render_datatable_filters(datatable)
  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 partial: 'effective/datatables/filters', locals: { datatable: datatable }
  else
    render(partial: 'effective/datatables/filters', locals: { datatable: datatable }).gsub('<form', '<div').gsub('/form>', '/div>').html_safe
  end

end