2017-01-09 16:40:58 +01:00
|
|
|
module BaseTransaction
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
include ActionView::Helpers::NumberHelper
|
2017-01-14 16:22:44 +01:00
|
|
|
include ApplicationHelper
|
2017-01-09 16:40:58 +01:00
|
|
|
|
|
|
|
included do
|
|
|
|
belongs_to :debtor, class_name: 'User'
|
|
|
|
belongs_to :creditor, class_name: 'User'
|
|
|
|
belongs_to :issuer, polymorphic: true
|
|
|
|
|
2017-01-14 22:45:51 +01:00
|
|
|
validates :debtor, presence: true
|
|
|
|
validates :creditor, presence: true
|
|
|
|
validates :amount, numericality: { greater_than: 0 }
|
2017-01-09 16:40:58 +01:00
|
|
|
validate :different_debtor_creditor
|
|
|
|
end
|
|
|
|
|
|
|
|
def info
|
|
|
|
attributes.symbolize_keys.extract!(
|
|
|
|
:debtor_id, :creditor_id, :issuer_id, :issuer_type, :message, :amount
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def amount_f
|
2017-01-11 21:18:23 +01:00
|
|
|
euro_from_cents amount
|
2017-01-09 16:40:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def different_debtor_creditor
|
|
|
|
if self.debtor == self.creditor
|
|
|
|
self.errors.add :base, "Can't write money to yourself"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|