2015-09-10 14:04:36 +02:00
|
|
|
class TransactionsQuery
|
2015-09-10 16:40:15 +02:00
|
|
|
attr_reader :arel_table
|
|
|
|
|
2015-09-10 14:04:36 +02:00
|
|
|
def initialize user
|
|
|
|
@user = user
|
|
|
|
@transactions = Arel::Table.new(:transactions)
|
2015-09-10 15:05:46 +02:00
|
|
|
@perspectived = Arel::Table.new(:perspectived_transactions)
|
|
|
|
@peers = Arel::Table.new(:users).alias('peers')
|
2015-09-10 16:40:15 +02:00
|
|
|
@arel_table = Arel::Table.new(@user.name.concat('_transactions'))
|
2015-09-10 14:04:36 +02:00
|
|
|
end
|
|
|
|
|
2015-09-10 16:40:15 +02:00
|
|
|
def query
|
2015-09-10 15:47:54 +02:00
|
|
|
Arel::SelectManager.new(ActiveRecord::Base)
|
2015-09-10 16:40:15 +02:00
|
|
|
.from(arel)
|
2015-09-11 15:50:55 +02:00
|
|
|
.order(@arel_table[:time].desc)
|
2015-09-10 15:47:54 +02:00
|
|
|
end
|
|
|
|
|
2015-09-10 16:40:15 +02:00
|
|
|
def arel
|
|
|
|
Arel::Nodes::TableAlias.new(
|
|
|
|
issued_by(User).union(:all, issued_by(Client)),
|
|
|
|
arel_table.name
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2015-09-10 15:47:54 +02:00
|
|
|
def issued_by klass
|
|
|
|
issuers = klass.arel_table.alias('issuer')
|
2015-09-10 14:04:36 +02:00
|
|
|
Arel::SelectManager.new(ActiveRecord::Base)
|
|
|
|
.from(transactions)
|
2015-09-10 15:05:46 +02:00
|
|
|
.join(@peers).on(@peers[:id].eq(@perspectived[:peer_id]))
|
2015-09-10 15:47:54 +02:00
|
|
|
.join(issuers).on(issuers[:id].eq(@perspectived[:issuer_id]))
|
|
|
|
.where(@perspectived[:issuer_type].eq(klass.name))
|
2015-09-10 14:04:36 +02:00
|
|
|
.project(
|
2015-09-10 15:05:46 +02:00
|
|
|
@perspectived[:amount],
|
2015-09-10 19:41:47 +02:00
|
|
|
@perspectived[:time],
|
2015-09-10 15:47:54 +02:00
|
|
|
@perspectived[:message],
|
2015-09-10 15:05:46 +02:00
|
|
|
@peers[:name].as('peer'),
|
2015-09-10 15:47:54 +02:00
|
|
|
issuers[:name].as('issuer')
|
2015-09-10 14:04:36 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def transactions
|
2015-09-10 15:47:54 +02:00
|
|
|
Arel::Nodes::TableAlias.new(
|
2015-09-10 15:59:22 +02:00
|
|
|
incoming.union(:all, outgoing),
|
2015-09-10 15:47:54 +02:00
|
|
|
@perspectived.name
|
|
|
|
)
|
2015-09-10 14:04:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def outgoing
|
|
|
|
@transactions
|
|
|
|
.where(@transactions[:debtor_id].eq(@user.id))
|
|
|
|
.project(
|
|
|
|
(@transactions[:amount]*Arel::Nodes::SqlLiteral.new("-1")).as('amount'),
|
|
|
|
@transactions[:creditor_id].as('peer_id'),
|
2015-09-10 19:41:47 +02:00
|
|
|
@transactions[:created_at].as('time'),
|
2015-09-10 14:04:36 +02:00
|
|
|
@transactions[:issuer_id],
|
2015-09-10 15:05:46 +02:00
|
|
|
@transactions[:issuer_type],
|
|
|
|
@transactions[:message]
|
2015-09-10 14:04:36 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def incoming
|
|
|
|
@user.incoming_transactions.arel
|
|
|
|
@transactions
|
2015-09-11 12:07:39 +02:00
|
|
|
.where(@transactions[:creditor_id].eq(@user.id))
|
2015-09-10 14:04:36 +02:00
|
|
|
.project(
|
|
|
|
@transactions[:amount],
|
2015-09-11 12:07:39 +02:00
|
|
|
@transactions[:debtor_id].as('peer_id'),
|
2015-09-10 19:41:47 +02:00
|
|
|
@transactions[:created_at].as('time'),
|
2015-09-10 14:04:36 +02:00
|
|
|
@transactions[:issuer_id],
|
2015-09-10 15:05:46 +02:00
|
|
|
@transactions[:issuer_type],
|
|
|
|
@transactions[:message]
|
2015-09-10 14:04:36 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|