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_datatableObject



124
125
126
127
128
129
130
131
132
133
134
# File 'app/helpers/effective_datatables_helper.rb', line 124

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

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

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

  @_inline_datatable ||= datatable
end

#inline_datatable?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'app/helpers/effective_datatables_helper.rb', line 120

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

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



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 = pagination = search = sort = false
  end

  if short
    entries = pagination = 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 (: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

  # Build the datatables DOM option
  input_js[:dom] ||= [
    ("<'row'<'col-sm-12 dataTables_buttons'B>>" if buttons),
    "<'row'<'col-sm-12'tr>>",
    ("<'row'" if entries || pagination),
    ("<'col-sm-6 dataTables_entries'il>" if entries),
    ("<'col-sm-6'p>" if pagination),
    (">" if entries || pagination)
  ].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_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'), # effective_bootstrap
      '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

#render_inline_datatable(datatable) ⇒ Object



108
109
110
# File 'app/helpers/effective_datatables_helper.rb', line 108

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

#render_short_datatable(datatable) ⇒ Object



116
117
118
# File 'app/helpers/effective_datatables_helper.rb', line 116

def render_short_datatable(datatable)
  render_datatable(datatable, short: true)
end

#render_simple_datatable(datatable) ⇒ Object



112
113
114
# File 'app/helpers/effective_datatables_helper.rb', line 112

def render_simple_datatable(datatable)
  render_datatable(datatable, simple: true)
end