Class: Effective::Datatable

Constant Summary

Constants included from EffectiveDatatable::Resource

EffectiveDatatable::Resource::AGGREGATE_SQL_FUNCTIONS

Constants included from EffectiveDatatable::Format

EffectiveDatatable::Format::BLANK

Constants included from EffectiveDatatable::Cookie

EffectiveDatatable::Cookie::MAX_COOKIE_SIZE

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?, #controller_namespace

Methods included from EffectiveDatatable::Hooks

#finalize

Methods included from EffectiveDatatable::Cookie

#cookie, #cookie_key, #cookie_keys, #cookie_param

Methods included from EffectiveDatatable::Collection

#active_record_collection?, #array_collection?, #collection_class

Constructor Details

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

Returns a new instance of Datatable.



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

def initialize(view = nil, attributes = {})
  (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 '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

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

#resourceObject (readonly)

Returns the value of attribute resource.



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

def resource
  @resource
end

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

#viewObject

The view



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

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)


128
129
130
# File 'app/models/effective/datatable.rb', line 128

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

#blank?(view = nil) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
105
# File 'app/models/effective/datatable.rb', line 97

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



144
145
146
# File 'app/models/effective/datatable.rb', line 144

def collection
  @_collection
end

#columnsObject



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

def columns
  @_columns
end

#dsl_toolObject



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

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

#present?(view = nil) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
# File 'app/models/effective/datatable.rb', line 87

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

#simple?Boolean

When simple only a table will be rendered with no sorting, no filtering, no export buttons, no pagination, no per page, no colReorder default sorting only, default visibility only, all records returned, and responsive enabled

Returns:

  • (Boolean)


123
124
125
# File 'app/models/effective/datatable.rb', line 123

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

#table_html_classObject



132
133
134
# File 'app/models/effective/datatable.rb', line 132

def table_html_class
  attributes[:class] || EffectiveDatatables.html_class
end

#to_jsonObject



107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/models/effective/datatable.rb', line 107

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



136
137
138
# File 'app/models/effective/datatable.rb', line 136

def to_param
  @to_param ||= "#{self.class.name.underscore.parameterize}-#{cookie_param}"
end