Class: Effective::Datatable
- Inherits:
-
Object
- Object
- Effective::Datatable
- Extended by:
- EffectiveDatatable::Dsl
- Includes:
- EffectiveDatatable::Attributes, EffectiveDatatable::Collection, EffectiveDatatable::Compute, EffectiveDatatable::Cookie, EffectiveDatatable::Csv, EffectiveDatatable::Format, EffectiveDatatable::Hooks, EffectiveDatatable::Params, EffectiveDatatable::Resource, EffectiveDatatable::State
- Defined in:
- app/models/effective/datatable.rb
Constant Summary
Constants included from EffectiveDatatable::Resource
EffectiveDatatable::Resource::AGGREGATE_SQL_FUNCTIONS
Constants included from EffectiveDatatable::Format
EffectiveDatatable::Format::BLANK, EffectiveDatatable::Format::NONVISIBLE, EffectiveDatatable::Format::SPACER, EffectiveDatatable::Format::SPACER_TEMPLATE
Constants included from EffectiveDatatable::Compute
EffectiveDatatable::Compute::BLANK
Instance Attribute Summary collapse
-
#_aggregates ⇒ Object
readonly
Hashes of DSL options.
-
#_bulk_actions ⇒ Object
readonly
Returns the value of attribute _bulk_actions.
-
#_charts ⇒ Object
readonly
Returns the value of attribute _charts.
-
#_collection ⇒ Object
The collection itself.
-
#_collection_apply_belongs_to ⇒ Object
Returns the value of attribute _collection_apply_belongs_to.
-
#_collection_apply_scope ⇒ Object
Returns the value of attribute _collection_apply_scope.
-
#_columns ⇒ Object
readonly
Returns the value of attribute _columns.
-
#_filters ⇒ Object
readonly
Returns the value of attribute _filters.
-
#_form ⇒ Object
readonly
Returns the value of attribute _form.
-
#_scopes ⇒ Object
readonly
Returns the value of attribute _scopes.
-
#attributes ⇒ Object
readonly
Anything that we initialize our table with.
-
#effective_resource ⇒ Object
Returns the value of attribute effective_resource.
-
#source_location ⇒ Object
Set by DSL so we can track where this datatable is coming from.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#view ⇒ Object
The view.
Class Method Summary collapse
-
.datatable_name ⇒ Object
Checks en.datatables.admin/my_datatable.
Instance Method Summary collapse
-
#_filters_form_required? ⇒ Boolean
Whether the filters must be rendered as a <form> or we can keep the normal <div> behaviour.
- #blank?(view = nil) ⇒ Boolean
- #collection ⇒ Object
- #columns ⇒ Object
- #datatable_name ⇒ Object
- #date_range(value = nil) ⇒ Object
- #default_visibility ⇒ Object
- #downloadable? ⇒ Boolean
- #dsl_tool ⇒ Object
- #fallback_effective_resource ⇒ Object
- #filters_form ⇒ Object
- #html_class ⇒ Object
-
#initialize(view = nil, attributes = nil) ⇒ Datatable
constructor
A new instance of Datatable.
-
#inline? ⇒ Boolean
Inline crud.
-
#length_menu ⇒ Object
An array of integers [5, 10, 25, 50, 100, 250, 500].
- #present?(view = nil) ⇒ Boolean
- #rendered(params = {}) ⇒ Object
-
#reorder? ⇒ Boolean
Reordering.
- #resource ⇒ Object
- #searchable? ⇒ Boolean
- #skip_save_state? ⇒ Boolean
- #sortable? ⇒ Boolean
- #to_csv ⇒ Object
- #to_json ⇒ Object
- #to_param ⇒ Object
Methods included from EffectiveDatatable::Dsl
bulk_actions, charts, datatable, filters
Methods included from EffectiveDatatable::State
#display_length, #display_start, #filter, #offset, #order_direction, #order_index, #order_name, #page, #per_page, #scope, #search
Methods included from EffectiveDatatable::Resource
#admin_namespace?, #association_macros, #controller_namespace
Methods included from EffectiveDatatable::Hooks
Methods included from EffectiveDatatable::Csv
#csv_collection, #csv_content_type, #csv_file, #csv_filename, #csv_header, #csv_human_attribute_name, #csv_stream
Methods included from EffectiveDatatable::Cookie
Methods included from EffectiveDatatable::Collection
#active_record_array_collection?, #active_record_collection?, #active_record_polymorphic_array_collection?, #array_collection?, #collection_class
Constructor Details
#initialize(view = nil, attributes = nil) ⇒ Datatable
Returns a new instance of Datatable.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/models/effective/datatable.rb', line 42 def initialize(view = nil, attributes = nil) (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 'expected a hash of arguments' unless @attributes.kind_of?(Hash) 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
#_aggregates ⇒ Object (readonly)
Hashes of DSL options
10 11 12 |
# File 'app/models/effective/datatable.rb', line 10 def _aggregates @_aggregates end |
#_bulk_actions ⇒ Object (readonly)
Returns the value of attribute _bulk_actions.
11 12 13 |
# File 'app/models/effective/datatable.rb', line 11 def _bulk_actions @_bulk_actions end |
#_charts ⇒ Object (readonly)
Returns the value of attribute _charts.
12 13 14 |
# File 'app/models/effective/datatable.rb', line 12 def _charts @_charts end |
#_collection ⇒ Object
The collection itself. Only evaluated once.
19 20 21 |
# File 'app/models/effective/datatable.rb', line 19 def _collection @_collection end |
#_collection_apply_belongs_to ⇒ Object
Returns the value of attribute _collection_apply_belongs_to.
20 21 22 |
# File 'app/models/effective/datatable.rb', line 20 def _collection_apply_belongs_to @_collection_apply_belongs_to end |
#_collection_apply_scope ⇒ Object
Returns the value of attribute _collection_apply_scope.
21 22 23 |
# File 'app/models/effective/datatable.rb', line 21 def _collection_apply_scope @_collection_apply_scope end |
#_columns ⇒ Object (readonly)
Returns the value of attribute _columns.
13 14 15 |
# File 'app/models/effective/datatable.rb', line 13 def _columns @_columns end |
#_filters ⇒ Object (readonly)
Returns the value of attribute _filters.
14 15 16 |
# File 'app/models/effective/datatable.rb', line 14 def _filters @_filters end |
#_form ⇒ Object (readonly)
Returns the value of attribute _form.
15 16 17 |
# File 'app/models/effective/datatable.rb', line 15 def _form @_form end |
#_scopes ⇒ Object (readonly)
Returns the value of attribute _scopes.
16 17 18 |
# File 'app/models/effective/datatable.rb', line 16 def _scopes @_scopes end |
#attributes ⇒ Object (readonly)
Anything that we initialize our table with. That’s it. Can’t be changed by state.
5 6 7 |
# File 'app/models/effective/datatable.rb', line 5 def attributes @attributes end |
#effective_resource ⇒ Object
Returns the value of attribute effective_resource.
7 8 9 |
# File 'app/models/effective/datatable.rb', line 7 def effective_resource @effective_resource end |
#source_location ⇒ Object
Set by DSL so we can track where this datatable is coming from
27 28 29 |
# File 'app/models/effective/datatable.rb', line 27 def source_location @source_location end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
6 7 8 |
# File 'app/models/effective/datatable.rb', line 6 def state @state end |
#view ⇒ Object
The view
24 25 26 |
# File 'app/models/effective/datatable.rb', line 24 def view @view end |
Class Method Details
.datatable_name ⇒ Object
Checks en.datatables.admin/my_datatable
63 64 65 66 67 68 69 70 71 72 |
# File 'app/models/effective/datatable.rb', line 63 def self.datatable_name key = "datatables.#{name.underscore}" value = ::I18n.t(key) if value.include?(key) # missing translation name.titleize.split('/').last.chomp(' Datatable') else value end 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
206 207 208 |
# File 'app/models/effective/datatable.rb', line 206 def _filters_form_required? _form[:verb].present? end |
#blank?(view = nil) ⇒ Boolean
152 153 154 155 156 157 158 159 160 |
# File 'app/models/effective/datatable.rb', line 152 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 |
#collection ⇒ Object
254 255 256 |
# File 'app/models/effective/datatable.rb', line 254 def collection @_collection end |
#columns ⇒ Object
250 251 252 |
# File 'app/models/effective/datatable.rb', line 250 def columns @_columns end |
#datatable_name ⇒ Object
74 75 76 |
# File 'app/models/effective/datatable.rb', line 74 def datatable_name self.class.datatable_name end |
#date_range(value = nil) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'app/models/effective/datatable.rb', line 223 def date_range(value = nil) now = Time.zone.now value ||= filters[:date_range] start_date ||= filters[:start_date] end_date ||= filters[:end_date] return (nil..nil) if value.blank? case value.to_sym when :current_month (now.beginning_of_month..now.end_of_day) when :current_year (now.beginning_of_year..now.end_of_day) when :month (start_date || now).all_month when :year (start_date || now).all_year when :custom (start_date&.beginning_of_day..end_date&.end_of_day) when :all (nil..nil) else raise('unexpected date range value') end end |
#default_visibility ⇒ Object
270 271 272 |
# File 'app/models/effective/datatable.rb', line 270 def default_visibility columns.values.inject({}) { |h, col| h[col[:index]] = col[:visible]; h } end |
#downloadable? ⇒ Boolean
197 198 199 |
# File 'app/models/effective/datatable.rb', line 197 def downloadable? attributes[:downloadable] != false end |
#dsl_tool ⇒ Object
258 259 260 |
# File 'app/models/effective/datatable.rb', line 258 def dsl_tool @dsl_tool ||= DatatableDslTool.new(self) end |
#fallback_effective_resource ⇒ Object
266 267 268 |
# File 'app/models/effective/datatable.rb', line 266 def fallback_effective_resource @fallback_effective_resource ||= Effective::Resource.new('', namespace: controller_namespace) end |
#filters_form ⇒ Object
274 275 276 |
# File 'app/models/effective/datatable.rb', line 274 def filters_form DatatableFiltersForm.new(datatable: self) end |
#html_class ⇒ Object
210 211 212 |
# File 'app/models/effective/datatable.rb', line 210 def html_class Array(attributes[:class] || EffectiveDatatables.html_class).join(' ').presence end |
#inline? ⇒ Boolean
Inline crud
180 181 182 |
# File 'app/models/effective/datatable.rb', line 180 def inline? attributes[:inline] == true end |
#length_menu ⇒ Object
An array of integers [5, 10, 25, 50, 100, 250, 500]
215 216 217 |
# File 'app/models/effective/datatable.rb', line 215 def EffectiveDatatables. end |
#present?(view = nil) ⇒ Boolean
142 143 144 145 146 147 148 149 150 |
# File 'app/models/effective/datatable.rb', line 142 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 |
#rendered(params = {}) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'app/models/effective/datatable.rb', line 78 def rendered(params = {}) raise('expected a hash of params') unless params.kind_of?(Hash) view = ApplicationController.renderer.controller.helpers view.class_eval do attr_accessor :rendered_params def current_user rendered_params[:current_user] end end if params[:current_user_id] params[:current_user] = User.find(params[:current_user_id]) end view.rendered_params = params self.view = view self end |
#reorder? ⇒ Boolean
Reordering
185 186 187 |
# File 'app/models/effective/datatable.rb', line 185 def reorder? columns.key?(:_reorder) end |
#resource ⇒ Object
262 263 264 |
# File 'app/models/effective/datatable.rb', line 262 def resource raise('depecated. Please use .effective_resource instead') end |
#searchable? ⇒ Boolean
193 194 195 |
# File 'app/models/effective/datatable.rb', line 193 def searchable? attributes[:searchable] != false end |
#skip_save_state? ⇒ Boolean
201 202 203 |
# File 'app/models/effective/datatable.rb', line 201 def skip_save_state? attributes[:skip_save_state] == true end |
#sortable? ⇒ Boolean
189 190 191 |
# File 'app/models/effective/datatable.rb', line 189 def sortable? !reorder? && attributes[:sortable] != false end |
#to_csv ⇒ Object
162 163 164 |
# File 'app/models/effective/datatable.rb', line 162 def to_csv csv_file() end |
#to_json ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'app/models/effective/datatable.rb', line 166 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_param ⇒ Object
219 220 221 |
# File 'app/models/effective/datatable.rb', line 219 def to_param "#{self.class.name.underscore.parameterize}-#{[self.class, attributes].hash.abs.to_s.last(12)}" end |