Class: RailsTables::Datatable
- Inherits:
-
Object
- Object
- RailsTables::Datatable
- Defined in:
- lib/rails-tables/datatable.rb
Instance Attribute Summary collapse
-
#model ⇒ Object
Returns the value of attribute model.
-
#name ⇒ Object
Returns the value of attribute name.
-
#root ⇒ Object
Returns the value of attribute root.
-
#view ⇒ Object
Returns the value of attribute view.
Attributes included from Searching
Class Method Summary collapse
-
.column(name, *args) ⇒ Object
Allow user defined columns, lazily instanciate later after ‘self.root’ is defined.
-
.join(join) ⇒ Object
Allow user to explicitly join tables (not sure of use case).
- .source ⇒ Object
-
.source_path=(source) ⇒ Object
Set source url for this table.
Instance Method Summary collapse
-
#as_json(options = {}) ⇒ Object
Format this table for controller’s response.
-
#columns ⇒ Object
Lazily instanciates and caches columns.
-
#html_data ⇒ Object
Render data attributes for table for view.
-
#initialize(name, model) ⇒ Datatable
constructor
Called in has_datatable for model, or on an ActiveRecord::Relation in method_missing.
-
#joins ⇒ Object
Deduce joins based on columns and explicitly joined tables.
-
#render_with(view) ⇒ Object
Pass in view and scope table for controller.
- #set_root(root) ⇒ Object
Constructor Details
Instance Attribute Details
#model ⇒ Object
Returns the value of attribute model.
9 10 11 |
# File 'lib/rails-tables/datatable.rb', line 9 def model @model end |
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/rails-tables/datatable.rb', line 9 def name @name end |
#root ⇒ Object
Returns the value of attribute root.
9 10 11 |
# File 'lib/rails-tables/datatable.rb', line 9 def root @root end |
#view ⇒ Object
Returns the value of attribute view.
9 10 11 |
# File 'lib/rails-tables/datatable.rb', line 9 def view @view end |
Class Method Details
.column(name, *args) ⇒ Object
Allow user defined columns, lazily instanciate later after ‘self.root’ is defined
63 64 65 66 67 |
# File 'lib/rails-tables/datatable.rb', line 63 def self.column(name, *args) arguments = args.pop || {} self.column_factory = [] if self.column_factory.nil? self.column_factory << { name: name.to_s, args: arguments } end |
.join(join) ⇒ Object
Allow user to explicitly join tables (not sure of use case)
76 77 78 |
# File 'lib/rails-tables/datatable.rb', line 76 def self.join(join) self.joins += [join.to_s] end |
.source ⇒ Object
57 58 59 |
# File 'lib/rails-tables/datatable.rb', line 57 def self.source @source ||= Rails.application.routes.url_helpers.send(self.source_factory, format: "json") if self.source_factory.present? end |
.source_path=(source) ⇒ Object
Set source url for this table
54 55 56 |
# File 'lib/rails-tables/datatable.rb', line 54 def self.source_path=(source) self.source_factory = source if source.present? end |
Instance Method Details
#as_json(options = {}) ⇒ Object
Format this table for controller’s response
43 44 45 46 47 48 49 50 |
# File 'lib/rails-tables/datatable.rb', line 43 def as_json(={}) { sEcho: params[:sEcho].to_i, iTotalRecords: objects.size, iTotalDisplayRecords: objects.total_entries, aaData: data } end |
#columns ⇒ Object
Lazily instanciates and caches columns
69 70 71 |
# File 'lib/rails-tables/datatable.rb', line 69 def columns @columns ||= self.column_factory.map.with_index{ |new_column, index| RailsTables::Column.new(self.class.name, self.model, new_column[:name], index, new_column[:args]) } end |
#html_data ⇒ Object
Render data attributes for table for view
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rails-tables/datatable.rb', line 23 def html_data = {} if self.class.source? [:source] = self.class.source end if self.initial_order.present? [:order_column] = self.columns.select{|c|c.name==self.class.initial_order.first[0].to_s}.first.order [:order_direction] = self.class.initial_order.first[1] end [:unsorted] = 'true' end |
#joins ⇒ Object
Deduce joins based on columns and explicitly joined tables
80 81 82 |
# File 'lib/rails-tables/datatable.rb', line 80 def joins @joins ||= (self.columns.reject(&:virtual).map(&:column_source).reject(&:blank?) + self.class.joins).uniq end |
#render_with(view) ⇒ Object
Pass in view and scope table for controller
37 38 39 40 |
# File 'lib/rails-tables/datatable.rb', line 37 def render_with(view) self.view = view return self end |