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

#approve_icon_to(path, options = {}) ⇒ Object



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

def approve_icon_to(path, options = {})
  glyphicon_to('ok', path, {title: 'Approve'}.merge(options))
end

#archive_icon_to(path, options = {}) ⇒ Object



94
95
96
97
# File 'app/helpers/effective_datatables_helper.rb', line 94

def archive_icon_to(path, options = {})
  defaults = {title: 'Archive', data: {method: :delete, confirm: 'Archive this item?'}}
  glyphicon_to('trash', path, defaults.merge(options))
end

#datatables_active_admin_path?Boolean

TODO: Improve on this

Returns:

  • (Boolean)


76
77
78
# File 'app/helpers/effective_datatables_helper.rb', line 76

def datatables_active_admin_path?
  attributes[:active_admin_path] rescue false
end

#datatables_admin_path?Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
# File 'app/helpers/effective_datatables_helper.rb', line 67

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

#destroy_icon_to(path, options = {}) ⇒ Object



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

def destroy_icon_to(path, options = {})
  defaults = {title: 'Destroy', data: {method: :delete, confirm: 'Delete this item?'}}
  glyphicon_to('trash', path, defaults.merge(options))
end

#edit_icon_to(path, options = {}) ⇒ Object



85
86
87
# File 'app/helpers/effective_datatables_helper.rb', line 85

def edit_icon_to(path, options = {})
  glyphicon_to('edit', path, {title: 'Edit'}.merge(options))
end

#glyphicon_to(icon, path, options = {}) ⇒ Object Also known as: bootstrap_icon_to, glyph_icon_to



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

def glyphicon_to(icon, path, options = {})
  (:a, options.merge(href: path)) do
    if icon.start_with?('glyphicon-')
      (:span, '', class: "glyphicon #{icon}")
    else
      (:span, '', class: "glyphicon glyphicon-#{icon}")
    end
  end
end

#ok_icon_to(path, options = {}) ⇒ Object



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

def ok_icon_to(path, options = {})
  glyphicon_to('ok', path, {title: 'OK'}.merge(options))
end

#remove_icon_to(path, options = {}) ⇒ Object



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

def remove_icon_to(path, options = {})
  glyphicon_to('remove', path, {title: 'Remove'}.merge(options))
end

#render_datatable(datatable, input_js_options = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/helpers/effective_datatables_helper.rb', line 4

def render_datatable(datatable, input_js_options = nil)
  raise 'expected datatable to be present' unless datatable

  datatable.view ||= self

  begin
    EffectiveDatatables.authorized?(controller, :index, datatable.collection_class) || raise(Effective::AccessDenied)
  rescue Effective::AccessDenied => e
    return (:p, "You are not authorized to view this datatable. (cannot :index, #{datatable.collection_class})")
  end

  render partial: 'effective/datatables/datatable',
    locals: { datatable: datatable, input_js_options: input_js_options.try(:to_json) }
end

#render_datatable_chart(datatable, name) ⇒ Object



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

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

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

  options = datatable.charts[name]
  chart = datatable.to_json[:charts][name]

  render partial: (options[:partial] || 'effective/datatables/chart'),
    locals: { datatable: datatable, chart: chart }
end

#render_datatable_charts(datatable) ⇒ Object



44
45
46
47
48
49
50
51
# File 'app/helpers/effective_datatables_helper.rb', line 44

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

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

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

#render_datatable_scopes(datatable) ⇒ Object



35
36
37
38
39
40
41
42
# File 'app/helpers/effective_datatables_helper.rb', line 35

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

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

  render partial: 'effective/datatables/scopes', locals: { datatable: datatable }
end

#render_simple_datatable(datatable, input_js_options = nil) ⇒ Object



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

def render_simple_datatable(datatable, input_js_options = nil)
  raise 'expected datatable to be present' unless datatable

  datatable.view ||= self
  datatable.simple = true

  begin
    EffectiveDatatables.authorized?(controller, :index, datatable.collection_class) || raise(Effective::AccessDenied)
  rescue Effective::AccessDenied => e
    return (:p, "You are not authorized to view this datatable. (cannot :index, #{datatable.collection_class})})")
  end

  render partial: 'effective/datatables/datatable',
    locals: {datatable: datatable, input_js_options: input_js_options.try(:to_json) }
end

#settings_icon_to(path, options = {}) ⇒ Object



104
105
106
# File 'app/helpers/effective_datatables_helper.rb', line 104

def settings_icon_to(path, options = {})
  glyphicon_to('cog', path, {title: 'Settings'}.merge(options))
end

#show_icon_to(path, options = {}) ⇒ Object

Icon Helpers for actions_column or elsewhere



81
82
83
# File 'app/helpers/effective_datatables_helper.rb', line 81

def show_icon_to(path, options = {})
  glyphicon_to('eye-open', path, {title: 'Show'}.merge(options))
end

#unarchive_icon_to(path, options = {}) ⇒ Object



99
100
101
102
# File 'app/helpers/effective_datatables_helper.rb', line 99

def unarchive_icon_to(path, options = {})
  defaults = {title: 'Unarchive', data: {confirm: 'Unarchive this item?'}}
  glyphicon_to('retweet', path, defaults.merge(options))
end