Class: RailsTables::Datatable

Inherits:
Object
  • Object
show all
Includes:
Searching, Sorting
Defined in:
lib/rails-tables/datatable.rb

Instance Attribute Summary collapse

Attributes included from Searching

#searches

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, model) ⇒ Datatable

Called in has_datatable for model, or on an ActiveRecord::Relation in method_missing



12
13
14
15
# File 'lib/rails-tables/datatable.rb', line 12

def initialize(name, model)
  self.name = name
  self.root = self.model = model
end

Instance Attribute Details

#modelObject

Returns the value of attribute model.



9
10
11
# File 'lib/rails-tables/datatable.rb', line 9

def model
  @model
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/rails-tables/datatable.rb', line 9

def name
  @name
end

#rootObject

Returns the value of attribute root.



9
10
11
# File 'lib/rails-tables/datatable.rb', line 9

def root
  @root
end

#viewObject

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

.sourceObject



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(options={})
  {
    sEcho: params[:sEcho].to_i,
    iTotalRecords: objects.size,
    iTotalDisplayRecords: objects.total_entries,
    aaData: data
  }
end

#columnsObject

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_dataObject

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
  options = {}
  if self.class.source?
    options[:source] = self.class.source
  end
  if self.initial_order.present?
    options[:order_column] = self.columns.select{|c|c.name==self.class.initial_order.first[0].to_s}.first.order
    options[:order_direction] = self.class.initial_order.first[1]
  end
  options[:unsorted] = 'true'
  options
end

#joinsObject

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

#set_root(root) ⇒ Object



17
18
19
20
# File 'lib/rails-tables/datatable.rb', line 17

def set_root(root)
  self.root = root
  self
end