class Tablets::Data
Responsible to fetch and prepare data for jquery-datatables
Attributes
controller[R]
params[R]
tablet[R]
Public Class Methods
new(tablet, params, controller)
click to toggle source
Initializes data with tablet and params
# File lib/tablets/data.rb, line 7 def initialize(tablet, params, controller) @tablet = tablet @params = params @controller = controller end
Public Instance Methods
as_json(_options = {})
click to toggle source
Prepares data to render as json
# File lib/tablets/data.rb, line 14 def as_json(_options = {}) { draw: params[:draw].to_i, recordsTotal: query.total, recordsFiltered: query.filtered, data: data, payload: tablet.payload } end
Private Instance Methods
cell(record, column)
click to toggle source
Returns single cell value, for specified record and for specified column
# File lib/tablets/data.rb, line 71 def cell(record, column) case column[:data] when Symbol then record.send(column[:data]) when Proc then column[:data].call(record, @controller) end end
concrete_relation()
click to toggle source
Relation with concretized parameters
# File lib/tablets/data.rb, line 36 def concrete_relation tablet.relation(params: relation_params, controller: controller) end
data()
click to toggle source
Fetching columns data for each row using column value:
Also appends details
# File lib/tablets/data.rb, line 54 def data records.map do |record| data = tablet.columns.map.with_index do |column, index| cell_data = cell(record, column) cell_data = CGI.escapeHTML(cell_data.to_s) unless column[:html_safe] [index, cell_data] end.to_h details = tablet.details(record) data[:details] = details unless details.nil? data end end
query()
click to toggle source
Initializes query with concrete relation
# File lib/tablets/data.rb, line 29 def query @query ||= Tablets::Data::Query.new(concrete_relation, params, tablet.columns) end
records()
click to toggle source
Fetching records and applies process tablet callback on it
# File lib/tablets/data.rb, line 46 def records @records ||= tablet.process(query.fetch) end
relation_params()
click to toggle source
Relation params. Empty hash by default
# File lib/tablets/data.rb, line 41 def relation_params params[:params] || {} end