class FlexibleDatatables::Datatable

Attributes

collection[RW]
columns[RW]
draw[R]
length[R]
order[R]
paginator[R]
sorter[R]
start[R]

Public Class Methods

new(args = {}) click to toggle source
# File lib/flexible_datatables/datatable.rb, line 5
def initialize(args = {})
  @columns   = []
  @draw      = args.fetch(:draw).to_i
  @grid      = []
  @length    = args.fetch(:length, Configurator.settings.items_per_page).to_i
  @order     = args.fetch(:order, {})
  @paginator = args.fetch(:paginator, Configurator.settings.paginator)
  @sorter    = Configurator.settings.sorter
  @start     = args.fetch(:start, 0).to_i
end

Public Instance Methods

as_json(options = {}) click to toggle source
# File lib/flexible_datatables/datatable.rb, line 16
def as_json(options = {})
  {
    draw:            draw,
    recordsTotal:    collection.length,
    recordsFiltered: collection.length,
    data:            @grid
  }
end
format_grid(cols = []) { |record| ... } click to toggle source
# File lib/flexible_datatables/datatable.rb, line 25
def format_grid(cols = [])
  @columns = cols unless cols.empty?
  records = sort(records)
  @grid = paginate(records).map { |record| yield(record) }
end

Private Instance Methods

paginate(records) click to toggle source
# File lib/flexible_datatables/datatable.rb, line 39
def paginate(records)
  paginator.paginate(start: start, length: length, records: records)
end
sort(records) click to toggle source
# File lib/flexible_datatables/datatable.rb, line 43
def sort(records)
  sorter.sort(columns: @columns, records: collection, order: order)
end