Class: Effective::Datatable
- Inherits:
-
Object
- Object
- Effective::Datatable
- Extended by:
- EffectiveDatatable::Dsl
- Includes:
- EffectiveDatatable::Ajax, EffectiveDatatable::Charts, EffectiveDatatable::Dsl::BulkActions, EffectiveDatatable::Dsl::Charts, EffectiveDatatable::Dsl::Datatable, EffectiveDatatable::Dsl::Scopes, EffectiveDatatable::Helpers, EffectiveDatatable::Hooks, 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
- #aggregates ⇒ Object
- #charts ⇒ Object
- #collection ⇒ Object
-
#collection_class ⇒ Object
This is set by initialize_datatable_options().
-
#current_scope ⇒ Object
The currently selected (klass) scope.
- #empty? ⇒ Boolean
-
#initialize(*args) ⇒ Datatable
constructor
A new instance of Datatable.
- #klass_scopes ⇒ Object
-
#model_name ⇒ Object
Instance method.
- #permitted_params ⇒ Object
- #present? ⇒ Boolean
- #scopes ⇒ Object
-
#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
- #view_context ⇒ Object
Methods included from EffectiveDatatable::Dsl
Methods included from EffectiveDatatable::Options
#initialize_attributes, #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
Methods included from EffectiveDatatable::Charts
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
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
Methods included from EffectiveDatatable::Dsl::BulkActions
#bulk_action, #bulk_action_content, #bulk_action_divider, #bulk_download
Constructor Details
#initialize(*args) ⇒ Datatable
Returns a new instance of Datatable.
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 |
# File 'app/models/effective/datatable.rb', line 24 def initialize(*args) initialize_attributes(args) if respond_to?(:initialize_scopes) # There was at least one scope defined in the scopes do .. end block initialize_scopes end if respond_to?(:initialize_datatable) initialize_datatable # This creates @table_columns based on the DSL datatable do .. end block # This normalizes all the options end if respond_to?(:initialize_charts) initialize_charts 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
#attributes ⇒ Object
Any attributes set on initialize will be echoed back and available to the class
80 81 82 |
# File 'app/models/effective/datatable.rb', line 80 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
91 92 93 |
# File 'app/models/effective/datatable.rb', line 91 def self.model_name # Searching & Filters @model_name ||= ActiveModel::Name.new(self) end |
Instance Method Details
#aggregates ⇒ Object
75 76 77 |
# File 'app/models/effective/datatable.rb', line 75 def aggregates @aggregates end |
#charts ⇒ Object
71 72 73 |
# File 'app/models/effective/datatable.rb', line 71 def charts @charts end |
#collection ⇒ Object
99 100 101 |
# File 'app/models/effective/datatable.rb', line 99 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
This is set by initialize_datatable_options()
103 104 105 |
# File 'app/models/effective/datatable.rb', line 103 def collection_class # This is set by initialize_datatable_options() @collection_class # Will be either User/Post/etc or Array end |
#current_scope ⇒ Object
The currently selected (klass) scope
63 64 65 |
# File 'app/models/effective/datatable.rb', line 63 def current_scope # The currently selected (klass) scope attributes[:current_scope] end |
#empty? ⇒ Boolean
128 129 130 |
# File 'app/models/effective/datatable.rb', line 128 def empty? total_records == 0 && current_scope.blank? end |
#klass_scopes ⇒ Object
59 60 61 |
# File 'app/models/effective/datatable.rb', line 59 def klass_scopes scopes.select { |name, | [:klass_scope] } end |
#model_name ⇒ Object
Instance method. In Rails 4.2 this needs to be defined on the instance, before it was on the class
87 88 89 |
# File 'app/models/effective/datatable.rb', line 87 def model_name # Searching & Filters @model_name ||= ActiveModel::Name.new(self.class) end |
#permitted_params ⇒ Object
67 68 69 |
# File 'app/models/effective/datatable.rb', line 67 def permitted_params scopes.keys + [:current_scope, :referer] end |
#present? ⇒ Boolean
124 125 126 |
# File 'app/models/effective/datatable.rb', line 124 def present? total_records > 0 || current_scope.present? end |
#scopes ⇒ Object
55 56 57 |
# File 'app/models/effective/datatable.rb', line 55 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
177 178 179 |
# File 'app/models/effective/datatable.rb', line 177 def simple? @simple == true end |
#table_columns ⇒ Object
51 52 53 |
# File 'app/models/effective/datatable.rb', line 51 def table_columns @table_columns end |
#to_json ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'app/models/effective/datatable.rb', line 107 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_key ⇒ Object
Searching & Filters
84 |
# File 'app/models/effective/datatable.rb', line 84 def to_key; []; end |
#to_param ⇒ Object
95 96 97 |
# File 'app/models/effective/datatable.rb', line 95 def to_param @to_param ||= self.class.name.underscore end |
#total_records ⇒ Object
132 133 134 |
# File 'app/models/effective/datatable.rb', line 132 def total_records @total_records ||= (active_record_collection? ? active_record_collection_size(the_collection) : the_collection.size) end |
#view_context ⇒ Object
166 167 168 |
# File 'app/models/effective/datatable.rb', line 166 def view_context view end |