Module: EffectiveDatatablesHelper

Defined in:
app/helpers/effective_datatables_helper.rb

Overview

These are expected to be called by a developer. They are part of the datatables DSL.

Instance Method Summary collapse

Instance Method Details

#inline_datatable ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'app/helpers/effective_datatables_helper.rb', line 93

def inline_datatable
  return nil unless inline_datatable?
  return @_inline_datatable if @_inline_datatable

  datatable = EffectiveDatatables.find(params[:_datatable_id])
  datatable.view = self

  EffectiveDatatables.authorize!(self, :index, datatable.collection_class)

  @_inline_datatable ||= datatable
end

#inline_datatable? ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'app/helpers/effective_datatables_helper.rb', line 89

def inline_datatable?
  params[:_datatable_id].present?
end

#render_datatable(datatable, input_js: {}, buttons: true, charts: true, filters: true, simple: false, inline: false) ⇒ Object



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

def render_datatable(datatable, input_js: {}, buttons: true, charts: true, filters: true, simple: false, inline: false)
  raise 'expected datatable to be present' unless datatable

  datatable.attributes[:simple] = true if simple
  datatable.attributes[:inline] = true if inline

  datatable.view ||= self

  unless EffectiveDatatables.authorized?(controller, :index, datatable.collection_class)
    return (: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?)

  input_js[:buttons] = false if simple || !buttons

  effective_datatable_params = {
    id: datatable.to_param,
    class: ('effective-datatable ' + Array(datatable.table_html_class).join(' ')),
    data: {
      'bulk-actions' => datatable_bulk_actions(datatable),
      'columns' => datatable_columns(datatable),
      'cookie' => datatable.cookie_key,
      'display-length' => datatable.display_length,
      'display-order' => [datatable.order_index, datatable.order_direction].to_json().html_safe,
      'display-records' => datatable.to_json[:recordsFiltered],
      'display-start' => datatable.display_start,
      'inline' => datatable.inline?.to_s,
      'options' => (input_js || {}).to_json.html_safe,
      'reset' => datatable_reset(datatable),
      'simple' => datatable.simple?.to_s,
      'spinner' => icon('spinner'), # effective_bootstrap
      'source' => effective_datatables.datatable_path(datatable, {format: 'json'}),
      'total-records' => datatable.to_json[:recordsTotal]
    }
  }

  if (charts || filters) && !simple
    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
end

#render_inline_datatable(datatable) ⇒ Object



65
66
67
# File 'app/helpers/effective_datatables_helper.rb', line 65

def render_inline_datatable(datatable)
  render_datatable(datatable, inline: true)
end

#render_simple_datatable(datatable) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/helpers/effective_datatables_helper.rb', line 69

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

  datatable.view ||= self

  unless EffectiveDatatables.authorized?(controller, :index, datatable.collection_class)
    return (:p, "You are not authorized to view this datatable. (cannot :index, #{datatable.collection_class})")
  end

  effective_datatable_params = {
    id: datatable.to_param,
    class: ('effective-datatable simple ' + Array(datatable.table_html_class).join(' ')),
    data: {}
  }

  render(partial: 'effective/datatables/datatable',
    locals: { datatable: datatable, effective_datatable_params: effective_datatable_params }
  )
end