Module: EffectiveDatatablesHelper

Defined in:
app/helpers/effective_datatables_helper.rb

Instance Method Summary collapse

Instance Method Details

#datatable_column_classes(datatable) ⇒ Object



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

def datatable_column_classes(datatable)
  [].tap do |classes|
    datatable.table_columns.values.each_with_index do |options, x|
      classes << {:className => options[:class], :targets => [x]} if options[:class].present?
    end
  end.to_json()
end

#datatable_default_order(datatable) ⇒ Object



52
53
54
55
56
57
58
59
# File 'app/helpers/effective_datatables_helper.rb', line 52

def datatable_default_order(datatable)
  [
    if datatable.default_order.present?
      index = (datatable.table_columns.values.find { |options| options[:name] == datatable.default_order.keys.first.to_s }[:index] rescue nil)
      [index, datatable.default_order.values.first] if index.present?
    end || [0, 'asc']
  ].to_json()
end

#datatable_filter(datatable, filterable = true) ⇒ Object



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

def datatable_filter(datatable, filterable = true)
  return false unless filterable

  filters = datatable.table_columns.values.map { |options, _| options[:filter] || {:type => 'null'} }

  # Process any Procs
  filters.each do |filter|
    if filter[:values].respond_to?(:call)
      filter[:values] = filter[:values].call()

      if filter[:values].kind_of?(ActiveRecord::Relation) || (filter[:values].kind_of?(Array) && filter[:values].first.kind_of?(ActiveRecord::Base))
        filter[:values] = filter[:values].map { |obj| [obj.id, obj.to_s] }
      end
    end
  end

  filters.to_json()
end

#datatable_non_sortable(datatable, sortable = true) ⇒ Object



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

def datatable_non_sortable(datatable, sortable = true)
  [].tap do |nonsortable|
    datatable.table_columns.values.each_with_index { |options, x| nonsortable << x if options[:sortable] == false || sortable == false }
  end.to_json()
end

#datatable_non_visible(datatable) ⇒ Object



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

def datatable_non_visible(datatable)
  [].tap do |nonvisible|
    datatable.table_columns.values.each_with_index do |options, x|
      visible = (options[:visible].respond_to?(:call) ? datatable.instance_exec(&options[:visible]) : options[:visible])
      nonvisible << x if visible == false
    end
  end.to_json()
end

#datatable_widths(datatable) ⇒ Object



61
62
63
# File 'app/helpers/effective_datatables_helper.rb', line 61

def datatable_widths(datatable)
  datatable.table_columns.values.map { |options| {'sWidth' => options[:width]} if options[:width] }.to_json()
end

#datatables_active_admin_path?Boolean

TODO: Improve on this

Returns:

  • (Boolean)


78
79
80
# File 'app/helpers/effective_datatables_helper.rb', line 78

def datatables_active_admin_path?
  attributes[:active_admin_path] rescue false
end

#datatables_admin_path?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'app/helpers/effective_datatables_helper.rb', line 73

def datatables_admin_path?
  (attributes[:admin_path] || request.referer.downcase.start_with?('/admin')) rescue false
end

#render_datatable(datatable, opts = {}) ⇒ Object



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

def render_datatable(datatable, opts = {})
  datatable.view = self
  locals = {:style => :full, :filterable => true, :sortable => true, :table_class => 'table-bordered table-striped'}.merge(opts)
  locals[:table_class] = 'sorting-hidden ' + locals[:table_class].to_s if locals[:sortable] == false

  render :partial => 'effective/datatables/datatable', :locals => locals.merge(:datatable => datatable)
end

#render_simple_datatable(datatable, opts = {}) ⇒ Object



10
11
12
13
14
15
16
# File 'app/helpers/effective_datatables_helper.rb', line 10

def render_simple_datatable(datatable, opts = {})
  datatable.view = self
  locals = {:style => :simple, :filterable => false, :sortable => false, :table_class => ''}.merge(opts)
  locals[:table_class] = 'sorting-hidden ' + locals[:table_class].to_s if locals[:sortable] == false

  render :partial => 'effective/datatables/datatable', :locals => locals.merge(:datatable => datatable)
end