Clean redundant code

This commit is contained in:
benji 2017-01-09 16:40:58 +01:00
parent 220a1e6684
commit 1e88bdd63a
5 changed files with 60 additions and 45 deletions

View file

@ -0,0 +1,31 @@
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
number_to_currency amount/100.0, unit: '€'
end
private
def different_debtor_creditor
if self.debtor == self.creditor
self.errors.add :base, "Can't write money to yourself"
end
end
end

View file

@ -0,0 +1,12 @@
module TransactionHelpers
include ActiveSupport::Concern
def peer_of(user)
return creditor if user == debtor
return debtor if user == creditor
end
def is_client_transaction?
issuer_type == 'Client'
end
end

View file

@ -1,21 +1,13 @@
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
include BaseTransaction
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
)
creditor.notifications.create message: "Your request for €#{amount/100.0} for \"#{message}\" has been accepted by #{debtor.name}."
Transaction.create info
Notification.create user: creditor,message: confirmed_message
update_attributes status: :confirmed
end
@ -23,15 +15,18 @@ class Request < ActiveRecord::Base
def decline!
return unless open?
creditor.notifications.create message: "#{debtor.name} refuses to pay €#{amount/100.0} for \"#{message}\"."
Notification.create user: creditor, message: declined_message
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
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}\"."
end
end

View file

@ -15,22 +15,13 @@
#
class Transaction < ActiveRecord::Base
belongs_to :debtor, class_name: 'User'
belongs_to :creditor, class_name: 'User'
belongs_to :issuer, polymorphic: true
include BaseTransaction
include TransactionHelpers
after_save :recalculate_balances
after_destroy :recalculate_balances
after_save :recalculate_balances!
after_destroy :recalculate_balances!
validates :amount, numericality: { greater_than: 0 }
validates :id_at_client, presence: true, uniqueness: { scope: :issuer_id }, if: :is_client_transaction?
validate :different_debtor_creditor
def peer_of(user)
return creditor if user == debtor
return debtor if user == creditor
end
def signed_amount_for(user)
return -amount if user == debtor
@ -42,24 +33,10 @@ class Transaction < ActiveRecord::Base
self.amount *= -1
end
def info
attributes.symbolize_keys.extract!(:debtor_id, :creditor_id, :issuer_id, :issuer_type, :message, :amount)
end
private
def recalculate_balances
def recalculate_balances!
creditor.calculate_balance!
debtor.calculate_balance!
end
def different_debtor_creditor
if self.debtor == self.creditor
self.errors.add :base, "Can't write money to yourself"
end
end
def is_client_transaction?
issuer_type == "Client"
end
end

View file

@ -14,7 +14,7 @@
%tr
%td= r.creditor.name
%td= r.issuer.name
%td= "€#{r.amount/100.0}"
%td= r.amount_f
%td= r.message
- if actions
%td