tab/app/models/request.rb

62 lines
1.6 KiB
Ruby
Raw Permalink Normal View History

2017-01-09 16:41:24 +01:00
# == Schema Information
#
# Table name: requests
#
# 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
# status :integer default(0)
# created_at :datetime not null
# updated_at :datetime not null
#
2017-01-09 15:46:43 +01:00
class Request < ActiveRecord::Base
2017-01-09 16:40:58 +01:00
include BaseTransaction
2017-01-09 15:46:43 +01:00
enum status: [:open, :confirmed, :declined, :cancelled]
2017-01-09 15:46:43 +01:00
def confirm!
return unless open?
2017-01-09 16:40:58 +01:00
Transaction.create info
Notification.create user: creditor,message: confirmed_message
2017-01-09 16:22:58 +01:00
2017-01-09 15:46:43 +01:00
update_attributes status: :confirmed
end
def decline!
return unless open?
2017-01-09 16:40:58 +01:00
Notification.create user: creditor, message: declined_message
2017-01-09 15:46:43 +01:00
update_attributes status: :declined
end
def cancel!
return unless open?
Notification.create user: creditor, message: cancelled_message unless issuer == creditor
Notification.create user: debtor, message: cancelled_message unless issuer == debtor
update_attributes status: :cancelled
end
2017-01-09 15:46:43 +01:00
private
2017-01-09 16:40:58 +01:00
def confirmed_message
"Your request for €#{amount/100.0} for \"#{message}\" has been accepted by #{debtor.name}."
end
def declined_message
"#{debtor.name} refuses to pay €#{amount/100.0} for \"#{message}\"."
2017-01-09 15:46:43 +01:00
end
def cancelled_message
"#{issuer.name} cancelled the request to pay #{debtor.name}#{amount/100.0} for \"#{message}\" to #{creditor.name}."
end
2017-01-09 15:46:43 +01:00
end