Skip to content

Commit

Permalink
update transactions query to include peer
Browse files Browse the repository at this point in the history
  • Loading branch information
iasoon committed Sep 10, 2015
1 parent b51e56e commit 7e251e1
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions app/grids/transactions_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@ class TransactionsQuery
def initialize user
@user = user
@transactions = Arel::Table.new(:transactions)
@perspectived = Arel::Table.new(:perspectived_transactions)
@peers = Arel::Table.new(:users).alias('peers')
end

def arel
Arel::SelectManager.new(ActiveRecord::Base)
.from(transactions)
.join(@peers).on(@peers[:id].eq(@perspectived[:peer_id]))
.project(
@transactions[:amount],
@transactions[:date],
@transactions[:peer_id],
@transactions[:issuer_id],
@transactions[:message]
@perspectived[:amount],
@perspectived[:date],
@peers[:name].as('peer'),
@perspectived[:issuer_id],
@perspectived[:message]
)
end

def transactions
Arel::Nodes::UnionAll.new(outgoing, incoming)
Arel::Nodes::TableAlias.new(incoming.union(outgoing), @perspectived.name)
end

def outgoing
Expand All @@ -28,7 +31,8 @@ def outgoing
@transactions[:creditor_id].as('peer_id'),
@transactions[:created_at].as('date'),
@transactions[:issuer_id],
@transactions[:issuer_type]
@transactions[:issuer_type],
@transactions[:message]
)
end

Expand All @@ -41,7 +45,8 @@ def incoming
@transactions[:debtor_id].as('peer_id'),
@transactions[:created_at].as('date'),
@transactions[:issuer_id],
@transactions[:issuer_type]
@transactions[:issuer_type],
@transactions[:message]
)
end
end

0 comments on commit 7e251e1

Please sign in to comment.