From 5991ea6be7e13dbe2caadf72d4f837149e7d450c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Mon, 7 Sep 2020 17:06:36 +0300 Subject: [PATCH] Reduce complexity of autobindable?() conditional --- app/models/bank_transaction.rb | 17 +++++++---------- app/models/invoice.rb | 2 +- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/app/models/bank_transaction.rb b/app/models/bank_transaction.rb index 51895ae5e..fa4d63bff 100644 --- a/app/models/bank_transaction.rb +++ b/app/models/bank_transaction.rb @@ -32,12 +32,9 @@ class BankTransaction < ApplicationRecord end def autobindable? - return false if binded? - return false unless registrar - return false unless invoice - return false unless invoice.payable? - - true + binded? && registrar.present && invoice.payable? ? true : false + rescue NoMethodError + false end # For successful binding, reference number, invoice id and sum must match with the invoice @@ -113,6 +110,10 @@ class BankTransaction < ApplicationRecord end end + def parsed_ref_number + reference_no || ref_number_from_description + end + private def reset_pending_registrar_balance_reload @@ -122,10 +123,6 @@ class BankTransaction < ApplicationRecord registrar.save! end - def parsed_ref_number - reference_no || ref_number_from_description - end - def ref_number_from_description /(\d{7})/.match(description)[0] end diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 28fd60f00..9e4f43dd8 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -113,7 +113,7 @@ class Invoice < ApplicationRecord end 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 registrar_user.issue_prepayment_invoice(amount: transaction.sum,