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 module DataTable
extend ActiveSupport::Concern extend ActiveSupport::Concern
@ -47,6 +46,12 @@ module DataTable
end end
selection_to_json(user, params[:draw], selection) 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 end
private private
@ -78,18 +83,19 @@ module DataTable
return clean return clean
end end
def selection_to_json(user, draw, selection) def response_json(draw, selection)
{ {
draw: draw, draw: draw,
recordsTotal: user.transactions.count, recordsTotal: user.transactions.count,
recordsFiltered: selection.count, recordsFiltered: selection.count,
data: selection.offset(params[:start]).take(params[:length]).map { |transaction| { #data: selection.offset(params[:start]).take(params[:length]).map { |transaction| {
time: transaction.created_at, #time: transaction.created_at,
amount: transaction.signed_amount_for(user), #amount: transaction.signed_amount_for(user),
peer: transaction.peer_of(user).try(:name), #peer: transaction.peer_of(user).try(:name),
issuer: transaction.issuer.name, #issuer: transaction.issuer.name,
message: transaction.message, #message: transaction.message,
}} #}}
data: selection
} }
end end
end end

View file

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