Module: EffectiveDatatablesHelper

Defined in:
app/helpers/effective_datatables_helper.rb

Instance Method Summary collapse

Instance Method Details

#datatable_column_classes(datatable) ⇒ Object



78
79
80
81
82
83
84
# File 'app/helpers/effective_datatables_helper.rb', line 78

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



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

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/helpers/effective_datatables_helper.rb', line 31

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



50
51
52
53
54
# File 'app/helpers/effective_datatables_helper.rb', line 50

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



56
57
58
59
60
61
62
63
# File 'app/helpers/effective_datatables_helper.rb', line 56

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



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

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)


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

def datatables_active_admin_path?
  attributes[:active_admin_path] rescue false
end

#datatables_admin_path?Boolean

Returns:

  • (Boolean)


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

def datatables_admin_path?
  @datatables_admin_path ||= (
    referer = request.referer.to_s.downcase.chomp('/') + '/'
    (attributes[:admin_path] || referer.include?('/admin/')) rescue false
  )
end

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



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

def render_datatable(datatable, opts = {}, &block)
  datatable.view = self

  locals = {:style => :full, :filterable => true, :sortable => true, :table_class => 'table-bordered table-striped'}
  locals = locals.merge(opts) if opts.kind_of?(Hash)
  locals[:table_class] = 'sorting-hidden ' + locals[:table_class].to_s if locals[:sortable] == false

  # Do we have to look at empty? behaviour
  if (block_given? || opts.kind_of?(String) || (opts.kind_of?(Hash) && opts[:empty].present?)) && datatable.empty?
    if block_given?
      yield; nil
    elsif opts.kind_of?(String)
      opts
    elsif opts.kind_of?(Hash) && opts[:empty].present?
      opts[:empty]
    end
  else
    render :partial => 'effective/datatables/datatable', :locals => locals.merge(:datatable => datatable)
  end
end

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



23
24
25
26
27
28
29
# File 'app/helpers/effective_datatables_helper.rb', line 23

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