Class: Effective::Datatable

Constant Summary

Constants included from EffectiveDatatable::Resource

EffectiveDatatable::Resource::AGGREGATE_SQL_FUNCTIONS

Constants included from EffectiveDatatable::Format

EffectiveDatatable::Format::BLANK, EffectiveDatatable::Format::NONVISIBLE, EffectiveDatatable::Format::SPACER, EffectiveDatatable::Format::SPACER_TEMPLATE

Constants included from EffectiveDatatable::Compute

EffectiveDatatable::Compute::BLANK

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EffectiveDatatable::Dsl

bulk_actions, charts, datatable, filters

Methods included from EffectiveDatatable::State

#display_length, #display_start, #filter, #offset, #order_direction, #order_index, #order_name, #page, #per_page, #scope, #search

Methods included from EffectiveDatatable::Resource

#admin_namespace?, #association_macros, #controller_namespace

Methods included from EffectiveDatatable::Hooks

#finalize

Methods included from EffectiveDatatable::Csv

#csv_content_type, #csv_file, #csv_filename, #csv_header, #csv_human_attribute_name, #csv_stream

Methods included from EffectiveDatatable::Cookie

#cookie

Methods included from EffectiveDatatable::Collection

#active_record_array_collection?, #active_record_collection?, #active_record_polymorphic_array_collection?, #array_collection?, #collection_class

Constructor Details

#initialize(view = nil, attributes = nil) ⇒ Datatable

Returns a new instance of Datatable.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/effective/datatable.rb', line 42

def initialize(view = nil, attributes = nil)
  (attributes = view; view = nil) if view.kind_of?(Hash)

  @attributes = initial_attributes(attributes)
  @state = initial_state

  @_aggregates = {}
  @_bulk_actions = []
  @_charts = {}
  @_columns = {}
  @_filters = {}
  @_form = {}
  @_scopes = {}

  raise 'expected a hash of arguments' unless @attributes.kind_of?(Hash)
  raise 'collection is defined as a method. Please use the collection do ... end syntax.' unless collection.nil?

  self.view = view if view
end

Instance Attribute Details

#_aggregatesObject (readonly)

Hashes of DSL options



10
11
12
# File 'app/models/effective/datatable.rb', line 10

def _aggregates
  @_aggregates
end

#_bulk_actionsObject (readonly)

Returns the value of attribute _bulk_actions.



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

def _bulk_actions
  @_bulk_actions
end

#_chartsObject (readonly)

Returns the value of attribute _charts.



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

def _charts
  @_charts
end

#_collectionObject

The collection itself. Only evaluated once.



19
20
21
# File 'app/models/effective/datatable.rb', line 19

def _collection
  @_collection
end

#_collection_apply_belongs_toObject

Returns the value of attribute _collection_apply_belongs_to.



20
21
22
# File 'app/models/effective/datatable.rb', line 20

def _collection_apply_belongs_to
  @_collection_apply_belongs_to
end

#_collection_apply_scopeObject

Returns the value of attribute _collection_apply_scope.



21
22
23
# File 'app/models/effective/datatable.rb', line 21

def _collection_apply_scope
  @_collection_apply_scope
end

#_columnsObject (readonly)

Returns the value of attribute _columns.



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

def _columns
  @_columns
end

#_filtersObject (readonly)

Returns the value of attribute _filters.



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

def _filters
  @_filters
end

#_formObject (readonly)

Returns the value of attribute _form.



15
16
17
# File 'app/models/effective/datatable.rb', line 15

def _form
  @_form
end

#_scopesObject (readonly)

Returns the value of attribute _scopes.



16
17
18
# File 'app/models/effective/datatable.rb', line 16

def _scopes
  @_scopes
end

#attributesObject (readonly)

Anything that we initialize our table with. That’s it. Can’t be changed by state.



5
6
7
# File 'app/models/effective/datatable.rb', line 5

def attributes
  @attributes
end

#effective_resourceObject

Returns the value of attribute effective_resource.



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

def effective_resource
  @effective_resource
end

#source_locationObject

Set by DSL so we can track where this datatable is coming from



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

def source_location
  @source_location
end

#stateObject (readonly)

Returns the value of attribute state.



6
7
8
# File 'app/models/effective/datatable.rb', line 6

def state
  @state
end

#viewObject

The view



24
25
26
# File 'app/models/effective/datatable.rb', line 24

def view
  @view
end

Class Method Details

.datatable_nameObject

Checks en.datatables.admin/my_datatable



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

def self.datatable_name
  value = I18n.t("datatables.#{name.underscore}").to_s
  value.start_with?('translation missing:') ? name.titleize.split('/').last.chomp(' Datatable') : value
end

Instance Method Details

#_filters_form_required?Boolean

Whether the filters must be rendered as a <form> or we can keep the normal <div> behaviour

Returns:

  • (Boolean)


200
201
202
# File 'app/models/effective/datatable.rb', line 200

def _filters_form_required?
  _form[:verb].present?
end

#blank?(view = nil) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
149
150
151
152
153
154
# File 'app/models/effective/datatable.rb', line 146

def blank?(view = nil)
  unless (@view || view)
    raise 'unable to call blank? without an assigned view. In your view, either call render_datatable(@datatable) first, or use @datatable.blank?(self)'
  end

  self.view ||= view

  to_json[:recordsTotal] == 0
end

#collectionObject



243
244
245
# File 'app/models/effective/datatable.rb', line 243

def collection
  @_collection
end

#columnsObject



239
240
241
# File 'app/models/effective/datatable.rb', line 239

def columns
  @_columns
end

#datatable_nameObject



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

def datatable_name
  self.class.datatable_name
end

#date_range(value = nil) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'app/models/effective/datatable.rb', line 212

def date_range(value = nil)
  now = Time.zone.now

  value ||= filters[:date_range]
  start_date ||= filters[:start_date]
  end_date ||= filters[:end_date]

  return (nil..nil) if value.blank?

  case value.to_sym
  when :current_month
    (now.beginning_of_month..now.end_of_day)
  when :current_year
    (now.beginning_of_year..now.end_of_day)
  when :month
    (start_date || now).all_month
  when :year
    (start_date || now).all_year
  when :custom
    (start_date&.beginning_of_day..end_date&.end_of_day)
  when :all
    (nil..nil)
  else
    raise('unexpected date range value')
  end
end

#default_visibilityObject



259
260
261
# File 'app/models/effective/datatable.rb', line 259

def default_visibility
  columns.values.inject({}) { |h, col| h[col[:index]] = col[:visible]; h }
end

#downloadable?Boolean

Returns:

  • (Boolean)


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

def downloadable?
  attributes[:downloadable] != false
end

#dsl_toolObject



247
248
249
# File 'app/models/effective/datatable.rb', line 247

def dsl_tool
  @dsl_tool ||= DatatableDslTool.new(self)
end

#fallback_effective_resourceObject



255
256
257
# File 'app/models/effective/datatable.rb', line 255

def fallback_effective_resource
  @fallback_effective_resource ||= Effective::Resource.new('', namespace: controller_namespace)
end

#filters_formObject



263
264
265
# File 'app/models/effective/datatable.rb', line 263

def filters_form
  DatatableFiltersForm.new(datatable: self)
end

#html_classObject



204
205
206
# File 'app/models/effective/datatable.rb', line 204

def html_class
  Array(attributes[:class] || EffectiveDatatables.html_class).join(' ').presence
end

#inline?Boolean

Inline crud

Returns:

  • (Boolean)


174
175
176
# File 'app/models/effective/datatable.rb', line 174

def inline?
  attributes[:inline] == true
end

#present?(view = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

def present?(view = nil)
  unless (@view || view)
    raise 'unable to call present? without an assigned view. In your view, either call render_datatable(@datatable) first, or use @datatable.present?(self)'
  end

  self.view ||= view

  to_json[:recordsTotal] > 0
end

#rendered(params = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/effective/datatable.rb', line 72

def rendered(params = {})
  raise('expected a hash of params') unless params.kind_of?(Hash)

  view = ApplicationController.renderer.controller.helpers

  view.class_eval do
    attr_accessor :rendered_params

    def current_user
      rendered_params[:current_user]
    end
  end

  if params[:current_user_id]
    params[:current_user] = User.find(params[:current_user_id])
  end

  view.rendered_params = params

  self.view = view
  self
end

#reorder?Boolean

Reordering

Returns:

  • (Boolean)


179
180
181
# File 'app/models/effective/datatable.rb', line 179

def reorder?
  columns.key?(:_reorder)
end

#resourceObject



251
252
253
# File 'app/models/effective/datatable.rb', line 251

def resource
  raise('depecated. Please use .effective_resource instead')
end

#searchable?Boolean

Returns:

  • (Boolean)


187
188
189
# File 'app/models/effective/datatable.rb', line 187

def searchable?
  attributes[:searchable] != false
end

#skip_save_state?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'app/models/effective/datatable.rb', line 195

def skip_save_state?
  attributes[:skip_save_state] == true
end

#sortable?Boolean

Returns:

  • (Boolean)


183
184
185
# File 'app/models/effective/datatable.rb', line 183

def sortable?
  !reorder? && attributes[:sortable] != false
end

#to_csvObject



156
157
158
# File 'app/models/effective/datatable.rb', line 156

def to_csv
  csv_file()
end

#to_jsonObject



160
161
162
163
164
165
166
167
168
169
170
171
# File 'app/models/effective/datatable.rb', line 160

def to_json
  @json ||= (
    {
      data: (compute || []),
      draw: (params[:draw] || 0),
      recordsTotal: (@total_records || 0),
      recordsFiltered: (@display_records || 0),
      aggregates: (@aggregates_data || []),
      charts: (@charts_data || {})
    }
  )
end

#to_paramObject



208
209
210
# File 'app/models/effective/datatable.rb', line 208

def to_param
  "#{self.class.name.underscore.parameterize}-#{[self.class, attributes].hash.abs.to_s.last(12)}"
end