tab/app/models/request.rb

38 lines
1,015 B
Ruby
Raw Normal View History

2017-01-09 15:46:43 +01:00
class Request < ActiveRecord::Base
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
enum status: [:open, :confirmed, :declined]
def confirm!
return unless open?
Transaction.create attributes.symbolize_keys.extract!(
:debtor_id, :creditor_id, :issuer_id, :issuer_type, :amount, :message
)
2017-01-09 16:22:58 +01:00
creditor.notifications.create message: "Your request for #{amount} for \"#{message}\" has been accepted by #{debtor.name}."
2017-01-09 15:46:43 +01:00
update_attributes status: :confirmed
end
def decline!
return unless open?
2017-01-09 16:22:58 +01:00
creditor.notifications.create message: "#{debtor.name} refuses to pay #{amount} for \"#{message}\"."
2017-01-09 15:46:43 +01:00
update_attributes status: :declined
end
private
def different_debtor_creditor
if self.debtor == self.creditor
self.errors.add :base, "Can't write money to yourself"
end
end
end