Module: EffectiveDatatablesHelper

Defined in:
app/helpers/effective_datatables_helper.rb

Instance Method Summary collapse

Instance Method Details

#bulk_action(*args) ⇒ Object

Bulk Actions DSL Methods



134
135
136
# File 'app/helpers/effective_datatables_helper.rb', line 134

def bulk_action(*args)
  content_for(:effective_datatables_bulk_actions) { (:li, link_to(*args)) }
end

#bulk_action_content(&block) ⇒ Object



142
143
144
# File 'app/helpers/effective_datatables_helper.rb', line 142

def bulk_action_content(&block)
  content_for(:effective_datatables_bulk_actions) { block.call }
end

#bulk_action_dividerObject



138
139
140
# File 'app/helpers/effective_datatables_helper.rb', line 138

def bulk_action_divider
  content_for(:effective_datatables_bulk_actions) { (:li, '', class: 'divider', role: 'separator') }
end

#datatable_bulk_actions(datatable) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/helpers/effective_datatables_helper.rb', line 39

def datatable_bulk_actions(datatable)
  bulk_actions_column = datatable.table_columns.find { |_, options| options[:bulk_actions_column] }.try(:second)
  return false unless bulk_actions_column

  # This sets content_for(:effective_datatables_bulk_actions) as per the 3 bulk_action methods below
  instance_exec(&bulk_actions_column[:dropdown_block]) if bulk_actions_column[:dropdown_block].respond_to?(:call)

  {
    dropdownHtml: render(
      partial: bulk_actions_column[:dropdown_partial],
      locals: HashWithIndifferentAccess.new(datatable: datatable).merge(bulk_actions_column[:partial_locals])
    )
  }.to_json()
end

#datatable_columns(datatable) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/helpers/effective_datatables_helper.rb', line 20

def datatable_columns(datatable)
  form = nil
  simple_form_for(:datatable_filter, url: '#', html: {id: "#{datatable.to_param}-form"}) { |f| form = f }

  datatable.table_columns.map do |name, options|
    {
      name: options[:name],
      title: (:span, options[:label], class: 'filter-label'),
      className: options[:class],
      width: options[:width],
      responsivePriority: (options[:responsivePriority] || 10000),  # 10,000 is datatables default
      sortable: (options[:sortable] && !datatable.simple?),
      visible: (options[:visible].respond_to?(:call) ? datatable.instance_exec(&options[:visible]) : options[:visible]),
      filterHtml: (datatable_header_filter(form, name, datatable.search_terms[name], options) unless datatable.simple?),
      filterSelectedValue: options[:filter][:selected]
    }
  end.to_json()
end

#datatable_default_order(datatable) ⇒ Object



15
16
17
# File 'app/helpers/effective_datatables_helper.rb', line 15

def datatable_default_order(datatable)
  [datatable.order_index, datatable.order_direction.downcase].to_json()
end

#datatable_header_filter(form, name, value, opts) ⇒ Object



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
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/helpers/effective_datatables_helper.rb', line 65

def datatable_header_filter(form, name, value, opts)
  return render(partial: opts[:header_partial], locals: {form: form, name: (opts[:label] || name), column: opts}) if opts[:header_partial].present?

  include_blank = opts[:filter].key?(:include_blank) ? opts[:filter][:include_blank] : (opts[:label] || name.titleize)
  placeholder = opts[:filter].key?(:placeholder) ? opts[:filter][:placeholder] : (opts[:label] || name.titleize)

  case opts[:filter][:as]
  when :string, :text, :number
    form.input name, label: false, required: false, value: value,
      as: :string,
      placeholder: placeholder,
      input_html: { name: nil, value: value, autocomplete: 'off', data: {'column-name' => opts[:name], 'column-index' => opts[:index]} }
  when :date
    form.input name, label: false, required: false, value: value,
      as: (ActionView::Helpers::FormBuilder.instance_methods.include?(:effective_date_picker) ? :effective_date_picker : :string),
      placeholder: placeholder,
      input_group: false,
      input_html: { name: nil, autocomplete: 'off', data: {'column-name' => opts[:name], 'column-index' => opts[:index]} },
      input_js: { useStrict: true, keepInvalid: true }
  when :datetime
    form.input name, label: false, required: false, value: value,
      as: (ActionView::Helpers::FormBuilder.instance_methods.include?(:effective_date_time_picker) ? :effective_date_time_picker : :string),
      placeholder: placeholder,
      input_group: false,
      input_html: { name: nil, value: value, autocomplete: 'off', data: {'column-name' => opts[:name], 'column-index' => opts[:index]} },
      input_js: { useStrict: true, keepInvalid: true } # Keep invalid format like "2015-11" so we can still filter by year, month or day
  when :select, :boolean
    form.input name, label: false, required: false, value: value,
      as: (ActionView::Helpers::FormBuilder.instance_methods.include?(:effective_select) ? :effective_select : :select),
      collection: opts[:filter][:collection],
      selected: opts[:filter][:selected],
      multiple: opts[:filter][:multiple] == true,
      include_blank: include_blank,
      input_html: { name: nil, value: value, autocomplete: 'off', data: {'column-name' => opts[:name], 'column-index' => opts[:index]} },
      input_js: { placeholder: placeholder }
  when :grouped_select
    form.input name, label: false, required: false, value: value,
      as: (ActionView::Helpers::FormBuilder.instance_methods.include?(:effective_select) ? :effective_select : :grouped_select),
      collection: opts[:filter][:collection],
      selected: opts[:filter][:selected],
      multiple: opts[:filter][:multiple] == true,
      include_blank: include_blank,
      grouped: true,
      polymorphic: opts[:filter][:polymorphic] == true,
      group_label_method: opts[:filter][:group_label_method] || :first,
      group_method: opts[:filter][:group_method] || :last,
      input_html: { name: nil, value: value, autocomplete: 'off', data: {'column-name' => opts[:name], 'column-index' => opts[:index]} },
      input_js: { placeholder: placeholder }
  when :bulk_actions_column
    form.input name, label: false, required: false, value: nil,
      as: :boolean,
      input_html: { name: nil, value: nil, autocomplete: 'off', data: {'column-name' => opts[:name], 'column-index' => opts[:index], 'role' => 'bulk-actions-all'} }
  end
end

#datatable_scopes(datatable) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'app/helpers/effective_datatables_helper.rb', line 54

def datatable_scopes(datatable)
  return false unless datatable.scopes.present?

  {
    scopeHtml: render(
      partial: 'effective/datatables/scopes',
      locals: HashWithIndifferentAccess.new(datatable: datatable)
    )
  }.to_json()
end

#datatables_active_admin_path?Boolean

TODO: Improve on this

Returns:

  • (Boolean)


129
130
131
# File 'app/helpers/effective_datatables_helper.rb', line 129

def datatables_active_admin_path?
  attributes[:active_admin_path] rescue false
end

#datatables_admin_path?Boolean

Returns:

  • (Boolean)


120
121
122
123
124
125
126
# File 'app/helpers/effective_datatables_helper.rb', line 120

def datatables_admin_path?
  @datatables_admin_path ||= (
    path = request.path.to_s.downcase.chomp('/') + '/'
    referer = request.referer.to_s.downcase.chomp('/') + '/'
    (attributes[:admin_path] || referer.include?('/admin/') || path.include?('/admin/')) rescue false
  )
end

#render_datatable(datatable, input_js_options = nil) ⇒ Object



2
3
4
5
6
# File 'app/helpers/effective_datatables_helper.rb', line 2

def render_datatable(datatable, input_js_options = nil)
  datatable.view = self
  render partial: 'effective/datatables/datatable',
    locals: { datatable: datatable, input_js_options: input_js_options.try(:to_json) }
end

#render_simple_datatable(datatable, input_js_options = nil) ⇒ Object



8
9
10
11
12
13
# File 'app/helpers/effective_datatables_helper.rb', line 8

def render_simple_datatable(datatable, input_js_options = nil)
  datatable.view = self
  datatable.simple = true
  render partial: 'effective/datatables/datatable',
    locals: {datatable: datatable, input_js_options: input_js_options.try(:to_json) }
end