class DatatablesServer::Base

Attributes

params[R]

Public Class Methods

new(params) click to toggle source
# File lib/datatables_server/base.rb, line 7
def initialize(params)
  @params = params
end

Public Instance Methods

as_json(options = {}) click to toggle source
# File lib/datatables_server/base.rb, line 11
def as_json(options = {})
  {
    sEcho: s_echo,
    iTotalRecords: total_records_count,
    iTotalDisplayRecords: filtered_records_count,
    aaData: aa_data
  }
end
columns() click to toggle source
# File lib/datatables_server/base.rb, line 24
def columns
  raise MethodNotImplementedError
end
data() click to toggle source
# File lib/datatables_server/base.rb, line 20
def data
  raise MethodNotImplementedError
end

Private Instance Methods

aa_data() click to toggle source
# File lib/datatables_server/base.rb, line 44
def aa_data
  repository.paginated_data.map do |datum|
    attributes.inject([]) do |array, column|
      raw_value = datum.public_send(column)
      if respond_to?(column)
        array << public_send(column, raw_value)
      else
        array << raw_value
      end
    end
  end
end
attributes() click to toggle source
# File lib/datatables_server/base.rb, line 57
def attributes
  @attributes ||= columns.map do |column|
    column.split('.').last
  end
end
filtered_records_count() click to toggle source
# File lib/datatables_server/base.rb, line 40
def filtered_records_count
  repository.count_filtered
end
options() click to toggle source
# File lib/datatables_server/base.rb, line 63
def options
  OpenStruct.new.tap do |o|
    o.page_start = params[:iDisplayStart].to_i
    o.page_size = params[:iDisplayLength].to_i
    o.sort_column = columns[params[:iSortCol_0].to_i]
    o.sort_direction = (params[:sSortDir_0] == 'desc' ? 'DESC' : 'ASC')
    o.search_term = params[:sSearch]
  end
end
repository() click to toggle source
# File lib/datatables_server/base.rb, line 73
def repository
  @repository ||= RepositoryFactory.create(data, columns, options)
end
s_echo() click to toggle source
# File lib/datatables_server/base.rb, line 32
def s_echo
  params[:sEcho].to_i
end
total_records_count() click to toggle source
# File lib/datatables_server/base.rb, line 36
def total_records_count
  repository.count_all
end