Class: Effective::Datatable
- Inherits:
-
Object
- Object
- Effective::Datatable
- 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
-
#attributes ⇒ Object
Any attributes set on initialize will be echoed back and available to the class.
-
#display_records ⇒ Object
Returns the value of attribute display_records.
-
#simple ⇒ Object
These two options control the render behaviour of a datatable.
-
#table_html_class ⇒ Object
These two options control the render behaviour of a datatable.
-
#view ⇒ Object
Returns the value of attribute view.
Class Method Summary collapse
-
.model_name ⇒ Object
Searching & Filters.
Instance Method Summary collapse
- #collection ⇒ Object
- #collection_class ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(*args) ⇒ Datatable
constructor
A new instance of Datatable.
-
#model_name ⇒ Object
Instance method.
- #present? ⇒ Boolean
-
#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.
- #table_columns ⇒ Object
- #to_json ⇒ Object
-
#to_key ⇒ Object
Searching & Filters.
- #to_param ⇒ Object
- #total_records ⇒ Object
Methods included from EffectiveDatatable::Dsl::ClassMethods
Methods included from EffectiveDatatable::Rendering
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.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/models/effective/datatable.rb', line 17 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 # 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
#attributes ⇒ Object
Any attributes set on initialize will be echoed back and available to the class
43 44 45 |
# File 'app/models/effective/datatable.rb', line 43 def attributes @attributes end |
#display_records ⇒ Object
Returns the value of attribute display_records.
3 4 5 |
# File 'app/models/effective/datatable.rb', line 3 def display_records @display_records end |
#simple ⇒ Object
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_class ⇒ Object
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 |
#view ⇒ Object
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_name ⇒ Object
Searching & Filters
54 55 56 |
# File 'app/models/effective/datatable.rb', line 54 def self.model_name # Searching & Filters @model_name ||= ActiveModel::Name.new(self) end |
Instance Method Details
#collection ⇒ Object
62 63 64 |
# File 'app/models/effective/datatable.rb', line 62 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_class ⇒ Object
66 67 68 |
# File 'app/models/effective/datatable.rb', line 66 def collection_class @collection_class ||= (collection.respond_to?(:klass) ? collection.klass : self.class) end |
#empty? ⇒ Boolean
85 86 87 |
# File 'app/models/effective/datatable.rb', line 85 def empty? total_records.to_i == 0 end |
#model_name ⇒ Object
Instance method. In Rails 4.2 this needs to be defined on the instance, before it was on the class
50 51 52 |
# File 'app/models/effective/datatable.rb', line 50 def model_name # Searching & Filters @model_name ||= ActiveModel::Name.new(self.class) end |
#present? ⇒ Boolean
81 82 83 |
# File 'app/models/effective/datatable.rb', line 81 def present? total_records.to_i > 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
134 135 136 |
# File 'app/models/effective/datatable.rb', line 134 def simple? @simple == true end |
#table_columns ⇒ Object
38 39 40 |
# File 'app/models/effective/datatable.rb', line 38 def table_columns @table_columns end |
#to_json ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'app/models/effective/datatable.rb', line 70 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_key ⇒ Object
Searching & Filters
47 |
# File 'app/models/effective/datatable.rb', line 47 def to_key; []; end |
#to_param ⇒ Object
58 59 60 |
# File 'app/models/effective/datatable.rb', line 58 def to_param @to_param ||= self.class.name.underscore.sub('effective/datatables/', '') end |
#total_records ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'app/models/effective/datatable.rb', line 89 def total_records @total_records ||= ( if active_record_collection? # https://github.com/rails/rails/issues/15331 if collection_class.connection.respond_to?(:unprepared_statement) collection_sql = collection_class.connection.unprepared_statement { collection.to_sql } (collection_class.connection.execute("SELECT COUNT(*) FROM (#{collection_sql}) AS datatables_total_count").first.values.first rescue 1).to_i else (collection_class.connection.execute("SELECT COUNT(*) FROM (#{collection.to_sql}) AS datatables_total_count").first.values.first rescue 1).to_i end else collection.size end ) end |