update transactions query to include peer

This commit is contained in:
Ilion Beyst 2015-09-10 15:05:46 +02:00
parent 65e6278b67
commit 2d753146f0

View file

@ -2,22 +2,25 @@ class TransactionsQuery
def initialize user def initialize user
@user = user @user = user
@transactions = Arel::Table.new(:transactions) @transactions = Arel::Table.new(:transactions)
@perspectived = Arel::Table.new(:perspectived_transactions)
@peers = Arel::Table.new(:users).alias('peers')
end end
def arel def arel
Arel::SelectManager.new(ActiveRecord::Base) Arel::SelectManager.new(ActiveRecord::Base)
.from(transactions) .from(transactions)
.join(@peers).on(@peers[:id].eq(@perspectived[:peer_id]))
.project( .project(
@transactions[:amount], @perspectived[:amount],
@transactions[:date], @perspectived[:date],
@transactions[:peer_id], @peers[:name].as('peer'),
@transactions[:issuer_id], @perspectived[:issuer_id],
@transactions[:message] @perspectived[:message]
) )
end end
def transactions def transactions
Arel::Nodes::UnionAll.new(outgoing, incoming) Arel::Nodes::TableAlias.new(incoming.union(outgoing), @perspectived.name)
end end
def outgoing def outgoing
@ -28,7 +31,8 @@ class TransactionsQuery
@transactions[:creditor_id].as('peer_id'), @transactions[:creditor_id].as('peer_id'),
@transactions[:created_at].as('date'), @transactions[:created_at].as('date'),
@transactions[:issuer_id], @transactions[:issuer_id],
@transactions[:issuer_type] @transactions[:issuer_type],
@transactions[:message]
) )
end end
@ -41,7 +45,8 @@ class TransactionsQuery
@transactions[:debtor_id].as('peer_id'), @transactions[:debtor_id].as('peer_id'),
@transactions[:created_at].as('date'), @transactions[:created_at].as('date'),
@transactions[:issuer_id], @transactions[:issuer_id],
@transactions[:issuer_type] @transactions[:issuer_type],
@transactions[:message]
) )
end end
end end