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



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

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

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



101
102
103
104
# File 'app/helpers/effective_datatables_helper.rb', line 101

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)


83
84
85
# File 'app/helpers/effective_datatables_helper.rb', line 83

def datatables_active_admin_path?
  attributes[:active_admin_path] rescue false
end

#datatables_admin_path?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
# File 'app/helpers/effective_datatables_helper.rb', line 74

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



96
97
98
99
# File 'app/helpers/effective_datatables_helper.rb', line 96

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



92
93
94
# File 'app/helpers/effective_datatables_helper.rb', line 92

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



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

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



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

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

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



123
124
125
# File 'app/helpers/effective_datatables_helper.rb', line 123

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.try(:collection_class) || datatable.try(:class)) || raise('unauthorized')
  rescue => e
    return (:p, "You are not authorized to view this datatable. (cannot :index, #{datatable.try(:collection_class) || datatable.try(: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
66
67
68
69
70
71
72
# 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

  unless @effective_datatables_chart_javascript_rendered
    concat javascript_include_tag('https://www.google.com/jsapi')
    concat javascript_tag("if(google && google.visualization === undefined) { google.load('visualization', '1', {packages:#{EffectiveDatatables.google_chart_packages}}); }")

    @effective_datatables_chart_javascript_rendered = true
  end

  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.try(:collection_class) || datatable.try(:class)) || raise('unauthorized')
  rescue => e
    return (:p, "You are not authorized to view this datatable. (cannot :index, #{datatable.try(:collection_class) || datatable.try(: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



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

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



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

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

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



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

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