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 class Request < ActiveRecord::Base
belongs_to :debtor, class_name: 'User' include BaseTransaction
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] enum status: [:open, :confirmed, :declined]
def confirm! def confirm!
return unless open? return unless open?
Transaction.create attributes.symbolize_keys.extract!( Transaction.create info
:debtor_id, :creditor_id, :issuer_id, :issuer_type, :amount, :message Notification.create user: creditor,message: confirmed_message
)
creditor.notifications.create message: "Your request for €#{amount/100.0} for \"#{message}\" has been accepted by #{debtor.name}."
update_attributes status: :confirmed update_attributes status: :confirmed
end end
@ -23,15 +15,18 @@ class Request < ActiveRecord::Base
def decline! def decline!
return unless open? 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 update_attributes status: :declined
end end
private private
def different_debtor_creditor def confirmed_message
if self.debtor == self.creditor "Your request for €#{amount/100.0} for \"#{message}\" has been accepted by #{debtor.name}."
self.errors.add :base, "Can't write money to yourself" end
end
def declined_message
"#{debtor.name} refuses to pay €#{amount/100.0} for \"#{message}\"."
end end
end end

View file

@ -15,22 +15,13 @@
# #
class Transaction < ActiveRecord::Base class Transaction < ActiveRecord::Base
belongs_to :debtor, class_name: 'User' include BaseTransaction
belongs_to :creditor, class_name: 'User' include TransactionHelpers
belongs_to :issuer, polymorphic: true
after_save :recalculate_balances after_save :recalculate_balances!
after_destroy :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? 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) def signed_amount_for(user)
return -amount if user == debtor return -amount if user == debtor
@ -42,24 +33,10 @@ class Transaction < ActiveRecord::Base
self.amount *= -1 self.amount *= -1
end end
def info
attributes.symbolize_keys.extract!(:debtor_id, :creditor_id, :issuer_id, :issuer_type, :message, :amount)
end
private private
def recalculate_balances def recalculate_balances!
creditor.calculate_balance! creditor.calculate_balance!
debtor.calculate_balance! debtor.calculate_balance!
end 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 end

View file

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