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.



49
50
51
52
53
54
55
56
57
58
# File 'app/models/effective/datatable.rb', line 49

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



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

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_entries(entries) ⇒ Object



44
45
46
# File 'app/models/effective/datatable.rb', line 44

def default_entries(entries)
  @default_entries = entries
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



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

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



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

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

#default_entriesObject



144
145
146
147
148
149
150
# File 'app/models/effective/datatable.rb', line 144

def default_entries
  @default_entries ||= begin
    entries = (self.class.instance_variable_get(:@default_entries).presence || EffectiveDatatables.default_entries)
    entries = -1 if entries.to_s.downcase == 'all'
    [10, 25, 50, 100, 250, 1000, -1].include?(entries) ? entries : 25
  end
end

#default_orderObject



140
141
142
# File 'app/models/effective/datatable.rb', line 140

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

#empty?(view = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

def empty?(view = nil)
  self.view = view unless view.nil?
  to_json[:iTotalDisplayRecords] == 0
end

#finalize(collection) ⇒ Object

Override me if you like



77
78
79
# File 'app/models/effective/datatable.rb', line 77

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

#order_column_indexObject

Wish these were protected



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

def order_column_index
  if params[:iSortCol_0].present?
    params[:iSortCol_0].to_i
  elsif default_order.present?
    table_columns[default_order.keys.first].fetch(:index, 0)
  else
    0
  end
end

#order_directionObject



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

def order_direction
  if params[:sSortDir_0].present?
    params[:sSortDir_0].try(:downcase) == 'desc' ? 'DESC' : 'ASC'
  elsif default_order.present?
    default_order.values.first.to_s.downcase == 'desc' ? 'DESC' : 'ASC'
  else
    'ASC'
  end
end

#pageObject



192
193
194
# File 'app/models/effective/datatable.rb', line 192

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

#per_pageObject



180
181
182
183
184
185
186
187
188
189
190
# File 'app/models/effective/datatable.rb', line 180

def per_page
  length = params[:iDisplayLength].to_i

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

#present?(view = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

def present?(view = nil)
  self.view = view unless view.nil?
  to_json[:iTotalDisplayRecords] > 0
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



172
173
174
175
176
177
178
# File 'app/models/effective/datatable.rb', line 172

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



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/models/effective/datatable.rb', line 152

def search_terms
  @search_terms ||= HashWithIndifferentAccess.new().tap do |terms|
    if params[:sEcho].present?
      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
    else
      # We are in the initial render and have to apply default search terms only
      table_columns.each do |name, values|
        if (values[:filter][:selected].present?) && (values[:visible] != false || values[:filter][:when_hidden] == true)
          terms[name] = values[:filter][:selected]
        end
      end
    end
  end
end

#table_columnsObject

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



82
83
84
85
86
# File 'app/models/effective/datatable.rb', line 82

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_jsonObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/effective/datatable.rb', line 88

def to_json
  raise 'Effective::Datatable to_json called with a nil view.  Please call render_datatable(@datatable) or @datatable.view = view before this method' unless view.present?

  @json ||= {
    :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



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

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