Class: Effective::Datatable

Inherits:
Object
  • Object
show all
Extended by:
EffectiveDatatable::Dsl::ClassMethods
Includes:
EffectiveDatatable::Ajax, EffectiveDatatable::Dsl, EffectiveDatatable::Options, EffectiveDatatable::Rendering
Defined in:
app/models/effective/datatable.rb

Constant Summary

Constants included from EffectiveDatatable::Rendering

EffectiveDatatable::Rendering::BLANK

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EffectiveDatatable::Dsl::ClassMethods

datatable

Methods included from EffectiveDatatable::Rendering

#finalize

Methods included from EffectiveDatatable::Options

#initialize_options, #quote_sql

Methods included from EffectiveDatatable::Ajax

#display_entries, #display_table_columns, #order_direction, #order_index, #order_name, #page, #per_page, #per_page=, #search_column, #search_terms

Methods included from EffectiveDatatable::Dsl

#actions_column, #array_column, #default_entries, #default_order, #table_column

Constructor Details

#initialize(*args) ⇒ Datatable

Returns a new instance of Datatable.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/effective/datatable.rb', line 14

def initialize(*args)
  if args.present? && args.first != nil
    raise "#{self.class.name}.new() can only be initialized with a Hash like arguments" unless args.first.kind_of?(Hash)
    args.first.each { |k, v| self.attributes[k] = v }
  end

  initialize_datatable  # This creates @table_columns based on the DSL datatable do .. end block
  initialize_options    # This normalizes all the options

  unless active_record_collection? || array_collection?
    raise "Unsupported collection type. Should be ActiveRecord class, ActiveRecord relation, or an Array of Arrays [[1, 'something'], [2, 'something else']]"
  end

  if @default_order.present? && !table_columns.key?((@default_order.keys.first rescue nil))
    raise "default_order :#{(@default_order.keys.first rescue 'nil')} must exist as a table_column or array_column"
  end

  # Any pre-selected search terms should be assigned now
  search_terms.each { |column, term| self.send("#{column}=", term) }
end

Instance Attribute Details

#attributesObject

Any attributes set on initialize will be echoed back and available to the class



40
41
42
# File 'app/models/effective/datatable.rb', line 40

def attributes
  @attributes
end

#display_recordsObject

Returns the value of attribute display_records.



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

def display_records
  @display_records
end

#total_recordsObject

Returns the value of attribute total_records.



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

def total_records
  @total_records
end

#viewObject

Returns the value of attribute view.



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

def view
  @view
end

Class Method Details

.model_nameObject

Searching & Filters



51
52
53
# File 'app/models/effective/datatable.rb', line 51

def self.model_name # Searching & Filters
  @model_name ||= ActiveModel::Name.new(self)
end

Instance Method Details

#collectionObject



59
60
61
# File 'app/models/effective/datatable.rb', line 59

def collection
  raise "You must define a collection. Something like an ActiveRecord User.all or an Array of Arrays [[1, 'something'], [2, 'something else']]"
end

#collection_classObject



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

def collection_class
  @collection_class ||= (collection.respond_to?(:klass) ? collection.klass : self.class)
end

#empty?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'app/models/effective/datatable.rb', line 82

def empty?
  total_records.to_i == 0
end

#model_nameObject

Instance method. In Rails 4.2 this needs to be defined on the instance, before it was on the class



47
48
49
# File 'app/models/effective/datatable.rb', line 47

def model_name # Searching & Filters
  @model_name ||= ActiveModel::Name.new(self.class)
end

#present?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'app/models/effective/datatable.rb', line 78

def present?
  total_records.to_i > 0
end

#table_columnsObject



35
36
37
# File 'app/models/effective/datatable.rb', line 35

def table_columns
  @table_columns
end

#to_jsonObject



67
68
69
70
71
72
73
74
75
76
# File 'app/models/effective/datatable.rb', line 67

def to_json
  raise 'Effective::Datatable to_json called with a nil view.  Please call render_datatable(@datatable) or @datatable.view = view before this method' unless view.present?

  @json ||= {
    :draw => (params[:draw] || 0),
    :data => (table_data || []),
    :recordsTotal => (total_records || 0),
    :recordsFiltered => (display_records || 0)
  }
end

#to_keyObject

Searching & Filters



44
# File 'app/models/effective/datatable.rb', line 44

def to_key; []; end

#to_paramObject



55
56
57
# File 'app/models/effective/datatable.rb', line 55

def to_param
  @to_param ||= self.class.name.underscore.sub('effective/datatables/', '')
end