Class: Effective::Datatable

Inherits:
Object
  • Object
show all
Defined in:
app/models/effective/datatable.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Datatable

Returns a new instance of Datatable.



46
47
48
49
50
51
52
53
54
55
# File 'app/models/effective/datatable.rb', line 46

def initialize(*args)
  if args.present?
    raise 'Effective::Datatable.new() can only be called with a Hash like arguments' unless args.first.kind_of?(Hash)
    args.first.each { |k, v| self.attributes[k] = v }
  end

  unless active_record_collection? || (collection.kind_of?(Array) && collection.first.kind_of?(Array))
    raise "Unsupported collection type. Should be ActiveRecord class, ActiveRecord relation, or an Array of Arrays [[1, 'something'], [2, 'something else']]"
  end
end

Instance Attribute Details

#attributesObject

Any attributes set on initialize will be echoed back and available to the class



58
59
60
# File 'app/models/effective/datatable.rb', line 58

def attributes
  @attributes
end

#display_recordsObject

Returns the value of attribute display_records.



3
4
5
# File 'app/models/effective/datatable.rb', line 3

def display_records
  @display_records
end

#total_recordsObject

Returns the value of attribute total_records.



3
4
5
# File 'app/models/effective/datatable.rb', line 3

def total_records
  @total_records
end

#viewObject

Returns the value of attribute view.



3
4
5
# File 'app/models/effective/datatable.rb', line 3

def view
  @view
end

Class Method Details

.allObject



8
9
10
# File 'app/models/effective/datatable.rb', line 8

def all
  EffectiveDatatables.datatables.map { |klass| klass.new() }
end

.array_column(name, options = {}, proc = nil, &block) ⇒ Object



32
33
34
# File 'app/models/effective/datatable.rb', line 32

def array_column(name, options = {}, proc = nil, &block)
  table_column(name, options.merge({:array_column => true}), proc, &block)
end

.array_columns(*names) ⇒ Object



36
37
38
# File 'app/models/effective/datatable.rb', line 36

def array_columns(*names)
  names.each { |name| array_column(name) }
end

.default_order(name, direction = :asc) ⇒ Object



40
41
42
# File 'app/models/effective/datatable.rb', line 40

def default_order(name, direction = :asc)
  @default_order = {name => direction}
end

.find(obj, attributes = nil) ⇒ Object



12
13
14
15
# File 'app/models/effective/datatable.rb', line 12

def find(obj, attributes = nil)
  obj = obj.respond_to?(:to_param) ? obj.to_param : obj
  EffectiveDatatables.datatables.find { |klass| klass.name.underscore.parameterize == obj }.try(:new, attributes.presence || {})
end

.table_column(name, options = {}, proc = nil, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'app/models/effective/datatable.rb', line 17

def table_column(name, options = {}, proc = nil, &block)
  if block_given?
    raise "You cannot use :partial => '' with the block syntax" if options[:partial]
    raise "You cannot use :proc => ... with the block syntax" if options[:proc]
    options[:block] = block
  end
  raise "You cannot use both :partial => '' and proc => ..." if options[:partial] && options[:proc]

  (@table_columns ||= HashWithIndifferentAccess.new())[name] = options
end

.table_columns(*names) ⇒ Object



28
29
30
# File 'app/models/effective/datatable.rb', line 28

def table_columns(*names)
  names.each { |name| table_column(name) }
end

Instance Method Details

#collectionObject



66
67
68
# File 'app/models/effective/datatable.rb', line 66

def collection
  raise "You must define a collection. Something like an ActiveRecord User.all or an Array of Arrays [[1, 'something'], [2, 'something else']]"
end

#collection_classObject



70
71
72
# File 'app/models/effective/datatable.rb', line 70

def collection_class
  collection.respond_to?(:klass) ? collection.klass : self.class
end

#default_orderObject



114
115
116
# File 'app/models/effective/datatable.rb', line 114

def default_order
  self.class.instance_variable_get(:@default_order)
end

#finalize(collection) ⇒ Object

Override me if you like



74
75
76
# File 'app/models/effective/datatable.rb', line 74

def finalize(collection) # Override me if you like
  collection
end

#order_column_indexObject

Wish these were protected



106
107
108
# File 'app/models/effective/datatable.rb', line 106

def order_column_index
  params[:iSortCol_0].to_i
end

#order_directionObject



110
111
112
# File 'app/models/effective/datatable.rb', line 110

def order_direction
  params[:sSortDir_0].try(:downcase) == 'desc' ? 'DESC' : 'ASC'
end

#pageObject



149
150
151
# File 'app/models/effective/datatable.rb', line 149

def page
  params[:iDisplayStart].to_i / per_page + 1
end

#per_pageObject



137
138
139
140
141
142
143
144
145
146
147
# File 'app/models/effective/datatable.rb', line 137

def per_page
  length = params[:iDisplayLength].to_i

  if length == -1
    9999999
  elsif length > 0
    length
  else
    10
  end
end

#search_column(collection, table_column, search_term) ⇒ Object

This is here so classes that inherit from Datatables can can override the specific where clauses on a search column



129
130
131
132
133
134
135
# File 'app/models/effective/datatable.rb', line 129

def search_column(collection, table_column, search_term)
  if table_column[:array_column]
    array_tool.search_column_with_defaults(collection, table_column, search_term)
  else
    table_tool.search_column_with_defaults(collection, table_column, search_term)
  end
end

#search_termsObject



118
119
120
121
122
123
124
125
126
# File 'app/models/effective/datatable.rb', line 118

def search_terms
  @search_terms ||= HashWithIndifferentAccess.new().tap do |terms|
    table_columns.keys.each_with_index do |col, x|
      unless (params["sVisible_#{x}"] == 'false' && table_columns[col][:filter][:when_hidden] != true)
        terms[col] = params["sSearch_#{x}"] if params["sSearch_#{x}"].present?
      end
    end
  end
end

#table_columnsObject

Select only col == true columns, and then set the col accordingly



79
80
81
82
83
# File 'app/models/effective/datatable.rb', line 79

def table_columns
  @table_columns ||= table_columns_with_defaults().select do |_, col|
    col[:if] == nil || (col[:if].respond_to?(:call) ? (view || self).instance_exec(&col[:if]) : col[:if])
  end.each_with_index { |(_, col), index| col[:index] = index }
end

#to_json(options = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/effective/datatable.rb', line 85

def to_json(options = {})
  {
    :sEcho => params[:sEcho].to_i,
    :aaData => table_data || [],
    :iTotalRecords => (
      unless total_records.kind_of?(Hash)
        total_records.to_i
      else
        (total_records.keys.map(&:first).uniq.count rescue 1)
      end),
    :iTotalDisplayRecords => (
      unless display_records.kind_of?(Hash)
        display_records.to_i
      else
        (display_records.keys.map(&:first).uniq.count rescue 1)
      end)
  }
end

#to_paramObject



62
63
64
# File 'app/models/effective/datatable.rb', line 62

def to_param
  self.class.name.underscore.parameterize
end