Module: EffectiveDatatablesHelper

Defined in:
app/helpers/effective_datatables_helper.rb

Instance Method Summary collapse

Instance Method Details

#datatable_column_classes(datatable) ⇒ Object



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

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_column_names(datatable) ⇒ Object



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

def datatable_column_names(datatable)
  datatable.table_columns.values.map { |options| {:name => options[:name], :targets => options[:index] } }.to_json()
end

#datatable_default_order(datatable) ⇒ Object



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

def datatable_default_order(datatable)
  [datatable.order_index, datatable.order_direction.downcase].to_json()
end

#datatable_non_sortable(datatable, sortable = true) ⇒ Object



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

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



88
89
90
91
92
93
94
95
# File 'app/helpers/effective_datatables_helper.rb', line 88

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



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

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)


126
127
128
# File 'app/helpers/effective_datatables_helper.rb', line 126

def datatables_active_admin_path?
  attributes[:active_admin_path] rescue false
end

#datatables_admin_path?Boolean

Returns:

  • (Boolean)


117
118
119
120
121
122
123
# File 'app/helpers/effective_datatables_helper.rb', line 117

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

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



2
3
4
5
6
7
# 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'}.merge(opts)

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

#render_datatable_header_cell(form, name, opts, filterable = true) ⇒ Object



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
# File 'app/helpers/effective_datatables_helper.rb', line 17

def render_datatable_header_cell(form, name, opts, filterable = true)
  return render(partial: opts[:header_partial], locals: {form: form, name: (opts[:label] || name), column: opts, filterable: filterable}) if opts[:header_partial].present?
  return (:p, opts[:label] || name) if filterable == false

  case opts[:filter][:type]
  when :string, :text, :number
    form.input name, label: false, required: false, as: :string, placeholder: (opts[:label] || name),
      input_html: { name: nil, autocomplete: 'off', data: {'column-name' => opts[:name], 'column-index' => opts[:index]} }
  when :date
    form.input name, label: false, required: false,
      as: (defined?(EffectiveFormInputs) ? :effective_date_picker : :string),
      placeholder: (opts[:label] || name),
      input_group: false,
      input_html: { name: nil, autocomplete: 'off', data: {'column-name' => opts[:name], 'column-index' => opts[:index]} }
  when :datetime
    form.input name, label: false, required: false,
      as: (defined?(EffectiveFormInputs) ? :effective_date_time_picker : :string),
      placeholder: (opts[:label] || name),
      input_group: false,
      input_html: { name: nil, autocomplete: 'off', data: {'column-name' => opts[:name], 'column-index' => opts[:index]} }

  when :select, :boolean
    if opts[:filter][:values].respond_to?(:call)
      opts[:filter][:values] = opts[:filter][:values].call()

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

    form.input name, label: false, required: false,
      as: (defined?(EffectiveFormInputs) ? :effective_select : :select),
      collection: opts[:filter][:values],
      multiple: opts[:filter][:multiple] == true,
      include_blank: (opts[:label] || name.titleize),
      input_html: { name: nil, autocomplete: 'off', data: {'column-name' => opts[:name], 'column-index' => opts[:index]} },
      input_js: { placeholder: (opts[:label] || name.titleize) }
  when :grouped_select
    raise "Expected :group_select filter to define its values as a Hash {'Posts' => Post.all, 'Events' => Event.all} or a Hash {'Posts' => [['Post A', 1], ['Post B', 2]], 'Events' => [['Event A', 1], ['Event B', 2]]}" unless opts[:filter][:values].kind_of?(Hash)

    opts[:filter][:values].each do |group, options|
      if options.kind_of?(ActiveRecord::Relation)
        if opts[:type] == :belongs_to_polymorphic
          opts[:filter][:values][group] = options.map { |obj| [obj.to_s, "#{options.model_name}_#{obj.to_param}"] }
        else
          opts[:filter][:values][group] = options.map { |obj| [obj.to_s, obj.to_param] }
        end
      end
    end

    form.input name, label: false, required: false,
      as: (defined?(EffectiveFormInputs) ? :effective_select : :grouped_select),
      collection: opts[:filter][:values],
      multiple: opts[:filter][:multiple] == true,
      include_blank: (opts[:label] || name.titleize),
      grouped: true,
      group_label_method: :first,
      group_method: :last,
      input_html: { name: nil, autocomplete: 'off', data: {'column-name' => opts[:name], 'column-index' => opts[:index]} },
      input_js: { placeholder: (opts[:label] || name.titleize) }
  else
    (:p, opts[:label] || name)
  end
end

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



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

def render_simple_datatable(datatable, opts = {})
  datatable.view = self
  datatable.per_page = :all
  locals = {style: :simple, filterable: false, sortable: false, table_class: 'table-bordered table-striped sorting-hidden'}.merge(opts)

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