tab/app/models/transaction.rb

43 lines
1.1 KiB
Ruby
Raw Permalink Normal View History

2015-09-08 11:44:40 +02:00
# == Schema Information
#
# Table name: transactions
#
# id :integer not null, primary key
# debtor_id :integer not null
# creditor_id :integer not null
# issuer_id :integer not null
# issuer_type :string not null
# amount :integer default(0), not null
# message :string
# created_at :datetime not null
# updated_at :datetime not null
# id_at_client :integer
2015-09-08 11:44:40 +02:00
#
2015-09-08 11:30:11 +02:00
class Transaction < ActiveRecord::Base
2017-01-09 16:40:58 +01:00
include BaseTransaction
include TransactionHelpers
2015-09-08 12:11:48 +02:00
2017-01-09 16:40:58 +01:00
after_save :recalculate_balances!
after_destroy :recalculate_balances!
2015-09-08 13:09:34 +02:00
validates :id_at_client, presence: true, uniqueness: { scope: :issuer_id }, if: :is_client_transaction?
def signed_amount_for(user)
return -amount if user == debtor
return amount if user == creditor
end
def reverse
self.creditor, self.debtor = self.debtor, self.creditor
self.amount *= -1
end
2015-09-08 13:09:34 +02:00
private
2015-09-08 15:51:23 +02:00
2017-01-09 16:40:58 +01:00
def recalculate_balances!
2015-09-08 13:09:34 +02:00
creditor.calculate_balance!
debtor.calculate_balance!
end
2015-09-08 11:30:11 +02:00
end