Reduce complexity of autobindable?() conditional

This commit is contained in:
Karl Erik Õunapuu 2020-09-07 17:06:36 +03:00
parent be960457e6
commit 5991ea6be7
No known key found for this signature in database
GPG key ID: C9DD647298A34764
2 changed files with 8 additions and 11 deletions

View file

@ -32,12 +32,9 @@ class BankTransaction < ApplicationRecord
end end
def autobindable? def autobindable?
return false if binded? binded? && registrar.present && invoice.payable? ? true : false
return false unless registrar rescue NoMethodError
return false unless invoice false
return false unless invoice.payable?
true
end end
# For successful binding, reference number, invoice id and sum must match with the invoice # For successful binding, reference number, invoice id and sum must match with the invoice
@ -113,6 +110,10 @@ class BankTransaction < ApplicationRecord
end end
end end
def parsed_ref_number
reference_no || ref_number_from_description
end
private private
def reset_pending_registrar_balance_reload def reset_pending_registrar_balance_reload
@ -122,10 +123,6 @@ class BankTransaction < ApplicationRecord
registrar.save! registrar.save!
end end
def parsed_ref_number
reference_no || ref_number_from_description
end
def ref_number_from_description def ref_number_from_description
/(\d{7})/.match(description)[0] /(\d{7})/.match(description)[0]
end end

View file

@ -113,7 +113,7 @@ class Invoice < ApplicationRecord
end end
def self.create_from_transaction!(transaction) def self.create_from_transaction!(transaction)
registrar_user = Registrar.find_by(reference_no: transasction.parsed_ref_number) registrar_user = Registrar.find_by(reference_no: transaction.parsed_ref_number)
return unless registrar_user return unless registrar_user
registrar_user.issue_prepayment_invoice(amount: transaction.sum, registrar_user.issue_prepayment_invoice(amount: transaction.sum,