tab/app/controllers/concerns/data_table.rb

44 lines
1.1 KiB
Ruby
Raw Normal View History

module DataTable
extend ActiveSupport::Concern
def apply_filter(user, params)
p AjaxRequest.new(params)
selection_to_json(user, user.transactions)
end
private
class AjaxRequest
def initialize(params)
# Parsing according to https://datatables.net/manual/server-side
@draw = params.require(:draw).to_i
@start = params.require(:start).to_i
@length = params.require(:length).to_i
@columns = Hash.new
params.require(:columns).each do |i, column|
@columns[column.require(:data).to_sym] = {
name: column[:name],
searchable: column[:searchable] == 'true',
orderable: column[:orderable] == 'true',
search_value: column.require(:search)[:value]
}
end
end
end
def selection_to_json(user, selection)
{ data: selection.map { |transaction| {
amount: transaction.signed_amount_for(user),
issuer: transaction.issuer.name,
message: transaction.message,
peer: transaction.peer_of(user).try(:name),
time: transaction.created_at
} }
}
end
end