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

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_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

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)


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

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

#blank?(view = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

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



202
203
204
# File 'app/models/effective/datatable.rb', line 202

def collection
  @_collection
end

#columnsObject



198
199
200
# File 'app/models/effective/datatable.rb', line 198

def columns
  @_columns
end

#default_visibilityObject



218
219
220
# File 'app/models/effective/datatable.rb', line 218

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

#downloadable?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'app/models/effective/datatable.rb', line 181

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

#dsl_toolObject



206
207
208
# File 'app/models/effective/datatable.rb', line 206

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

#fallback_effective_resourceObject



214
215
216
# File 'app/models/effective/datatable.rb', line 214

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

#html_classObject



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

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

#inline?Boolean

Inline crud

Returns:

  • (Boolean)


164
165
166
# File 'app/models/effective/datatable.rb', line 164

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

#present?(view = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/effective/datatable.rb', line 62

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)


169
170
171
# File 'app/models/effective/datatable.rb', line 169

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

#resourceObject



210
211
212
# File 'app/models/effective/datatable.rb', line 210

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

#searchable?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'app/models/effective/datatable.rb', line 177

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

#sortable?Boolean

Returns:

  • (Boolean)


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

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

#to_csvObject



146
147
148
# File 'app/models/effective/datatable.rb', line 146

def to_csv
  csv_file()
end

#to_jsonObject



150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/models/effective/datatable.rb', line 150

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



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

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