Class: Effective::Datatable

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

datatable

Methods included from EffectiveDatatable::Options

#initialize_chart_options, #initialize_datatable_options, #initialize_scope_options, #quote_sql

Methods included from EffectiveDatatable::Hooks

#finalize, #order_column, #search_column

Methods included from EffectiveDatatable::Helpers

#convert_to_column_type

Methods included from EffectiveDatatable::Charts

#charts_data

Methods included from EffectiveDatatable::Ajax

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

Methods included from EffectiveDatatable::Dsl::Scopes

#scope

Methods included from EffectiveDatatable::Dsl::Datatable

#actions_column, #aggregate, #array_column, #bulk_actions_column, #default_entries, #default_order, #table_column

Methods included from EffectiveDatatable::Dsl::Charts

#chart

Methods included from EffectiveDatatable::Dsl::BulkActions

#bulk_action, #bulk_action_content, #bulk_action_divider

Constructor Details

#initialize(*args) ⇒ Datatable

Returns a new instance of Datatable.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/models/effective/datatable.rb', line 23

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

  if respond_to?(:initialize_scopes)  # There was at least one scope defined in the scopes do .. end block
    initialize_scopes
    initialize_scope_options
  end

  if respond_to?(:initialize_datatable)
    initialize_datatable          # This creates @table_columns based on the DSL datatable do .. end block
    initialize_datatable_options  # This normalizes all the options
  end

  if respond_to?(:initialize_charts)
    initialize_charts
    initialize_chart_options
  end

  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
end

Instance Attribute Details

#attributesObject

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



70
71
72
# File 'app/models/effective/datatable.rb', line 70

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

#simpleObject

These two options control the render behaviour of a datatable



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

def simple
  @simple
end

#table_html_classObject

These two options control the render behaviour of a datatable



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

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



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

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

Instance Method Details

#aggregatesObject



65
66
67
# File 'app/models/effective/datatable.rb', line 65

def aggregates
  @aggregates
end

#chartsObject



61
62
63
# File 'app/models/effective/datatable.rb', line 61

def charts
  @charts
end

#collectionObject



89
90
91
# File 'app/models/effective/datatable.rb', line 89

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



93
94
95
# File 'app/models/effective/datatable.rb', line 93

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

#empty?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'app/models/effective/datatable.rb', line 118

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



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

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

#present?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'app/models/effective/datatable.rb', line 114

def present?
  total_records.to_i > 0
end

#scopesObject



57
58
59
# File 'app/models/effective/datatable.rb', line 57

def scopes
  @scopes
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)


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

def simple?
  @simple == true
end

#table_columnsObject



53
54
55
# File 'app/models/effective/datatable.rb', line 53

def table_columns
  @table_columns
end

#to_jsonObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/effective/datatable.rb', line 97

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 ||= begin
    data = table_data

    {
      :draw => (params[:draw] || 0),
      :data => (data || []),
      :recordsTotal => (total_records || 0),
      :recordsFiltered => (display_records || 0),
      :aggregates => (aggregate_data(data) || []),
      :charts => (charts_data || {})
    }
  end
end

#to_keyObject

Searching & Filters



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

def to_key; []; end

#to_paramObject



85
86
87
# File 'app/models/effective/datatable.rb', line 85

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

#total_recordsObject



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

def total_records
  @total_records ||= (
    if active_record_collection?
      if collection_class.connection.respond_to?(:unprepared_statement)
        collection_sql = collection_class.connection.unprepared_statement { collection.to_sql }
        (collection_class.connection.exec_query("SELECT COUNT(*) FROM (#{collection_sql}) AS datatables_total_count").rows[0][0] rescue 1).to_i
      else
        (collection_class.connection.exec_query("SELECT COUNT(*) FROM (#{collection.to_sql}) AS datatables_total_count").rows[0][0] rescue 1).to_i
      end
    else
      collection.size
    end
  )
end

#view_contextObject



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

def view_context
  view
end