tab/app/models/concerns/base_transaction.rb

32 lines
700 B
Ruby
Raw Normal View History

2017-01-09 16:40:58 +01:00
module BaseTransaction
extend ActiveSupport::Concern
include ActionView::Helpers::NumberHelper
included do
belongs_to :debtor, class_name: 'User'
belongs_to :creditor, class_name: 'User'
belongs_to :issuer, polymorphic: true
validates :amount, numericality: { greater_than: 0 }
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