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::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.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/effective/datatable.rb', line 36

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

  @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



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

def _aggregates
  @_aggregates
end

#_bulk_actionsObject (readonly)

Returns the value of attribute _bulk_actions.



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

def _bulk_actions
  @_bulk_actions
end

#_chartsObject (readonly)

Returns the value of attribute _charts.



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

def _charts
  @_charts
end

#_collectionObject

The collection itself. Only evaluated once.



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

def _collection
  @_collection
end

#_collection_apply_belongs_toObject

Returns the value of attribute _collection_apply_belongs_to.



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

def _collection_apply_belongs_to
  @_collection_apply_belongs_to
end

#_collection_apply_scopeObject

Returns the value of attribute _collection_apply_scope.



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

def _collection_apply_scope
  @_collection_apply_scope
end

#_columnsObject (readonly)

Returns the value of attribute _columns.



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

def _columns
  @_columns
end

#_filtersObject (readonly)

Returns the value of attribute _filters.



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

def _filters
  @_filters
end

#_formObject (readonly)

Returns the value of attribute _form.



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

def _form
  @_form
end

#_scopesObject (readonly)

Returns the value of attribute _scopes.



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

def _scopes
  @_scopes
end

#attributesObject (readonly)

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



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

def attributes
  @attributes
end

#effective_resourceObject

Returns the value of attribute effective_resource.



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

def effective_resource
  @effective_resource
end

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

#viewObject

The view



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

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)


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

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

#blank?(view = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

def collection
  @_collection
end

#columnsObject



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

def columns
  @_columns
end

#default_visibilityObject



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

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

#dsl_toolObject



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

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

#fallback_effective_resourceObject



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

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

#html_classObject



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

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

#inline?Boolean

Inline crud

Returns:

  • (Boolean)


154
155
156
# File 'app/models/effective/datatable.rb', line 154

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

#present?(view = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/models/effective/datatable.rb', line 56

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)


159
160
161
# File 'app/models/effective/datatable.rb', line 159

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

#resourceObject



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

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

#sortable?Boolean

Returns:

  • (Boolean)


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

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

#to_jsonObject



140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/models/effective/datatable.rb', line 140

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



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

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