Class: Effective::Datatable

Constant Summary

Constants included from EffectiveDatatable::Format

EffectiveDatatable::Format::BLANK

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, #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_name

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.



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

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

The view, and the ajax/cookie/default state



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

def cookie
  @cookie
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

Returns the value of attribute view.



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

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)


126
127
128
# File 'app/models/effective/datatable.rb', line 126

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

#blank?(view = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

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



142
143
144
# File 'app/models/effective/datatable.rb', line 142

def collection
  @_collection
end

#columnsObject



138
139
140
# File 'app/models/effective/datatable.rb', line 138

def columns
  @_columns
end

#dsl_toolObject



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

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

#present?(view = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

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)


121
122
123
# File 'app/models/effective/datatable.rb', line 121

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

#table_html_classObject



130
131
132
# File 'app/models/effective/datatable.rb', line 130

def table_html_class
  attributes[:class] || 'table table-bordered table-striped'
end

#to_jsonObject



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

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



134
135
136
# File 'app/models/effective/datatable.rb', line 134

def to_param
  @to_param ||= self.class.name.underscore.parameterize
end