integrate arel query with datatable

This commit is contained in:
Ilion Beyst 2015-09-10 19:41:47 +02:00
parent 8ef41c9503
commit fdf4e27a27
2 changed files with 18 additions and 12 deletions

View file

@ -1,4 +1,3 @@
module DataTable
extend ActiveSupport::Concern
@ -47,6 +46,12 @@ module DataTable
end
selection_to_json(user, params[:draw], selection)
transactions = TransactionsQuery.new(@user)
query = transactions.query
selection = ActiveRecord::Base.connection.execute(query.to_sql).to_a
response_json(params[:draw], selection)
end
private
@ -78,18 +83,19 @@ module DataTable
return clean
end
def selection_to_json(user, draw, selection)
def response_json(draw, selection)
{
draw: draw,
recordsTotal: user.transactions.count,
recordsFiltered: selection.count,
data: selection.offset(params[:start]).take(params[:length]).map { |transaction| {
time: transaction.created_at,
amount: transaction.signed_amount_for(user),
peer: transaction.peer_of(user).try(:name),
issuer: transaction.issuer.name,
message: transaction.message,
}}
#data: selection.offset(params[:start]).take(params[:length]).map { |transaction| {
#time: transaction.created_at,
#amount: transaction.signed_amount_for(user),
#peer: transaction.peer_of(user).try(:name),
#issuer: transaction.issuer.name,
#message: transaction.message,
#}}
data: selection
}
end
end

View file

@ -31,7 +31,7 @@ class TransactionsQuery
.where(@perspectived[:issuer_type].eq(klass.name))
.project(
@perspectived[:amount],
@perspectived[:date],
@perspectived[:time],
@perspectived[:message],
@peers[:name].as('peer'),
issuers[:name].as('issuer')
@ -51,7 +51,7 @@ class TransactionsQuery
.project(
(@transactions[:amount]*Arel::Nodes::SqlLiteral.new("-1")).as('amount'),
@transactions[:creditor_id].as('peer_id'),
@transactions[:created_at].as('date'),
@transactions[:created_at].as('time'),
@transactions[:issuer_id],
@transactions[:issuer_type],
@transactions[:message]
@ -65,7 +65,7 @@ class TransactionsQuery
.project(
@transactions[:amount],
@transactions[:debtor_id].as('peer_id'),
@transactions[:created_at].as('date'),
@transactions[:created_at].as('time'),
@transactions[:issuer_id],
@transactions[:issuer_type],
@transactions[:message]