diff --git a/app/models/claim.rb b/app/models/claim.rb index 625b7b2f22..304e4bce68 100644 --- a/app/models/claim.rb +++ b/app/models/claim.rb @@ -181,31 +181,6 @@ class Claim < ApplicationRecord scope :awaiting_qa, -> { approved.qa_required.where(qa_completed_at: nil) } scope :qa_required, -> { where(qa_required: true) } - # This method's intention is to help make a decision on whether a claim should - # be flagged for QA or not. These criteria need to be met for each academic year: - # - # 1. the first claim to be approved should always be flagged for QA - # 2. subsequently approved claims should be flagged for QA, 1 in 100/MIN_QA_THRESHOLD. - # - # This method should be used every time a new approval decision is being made; - # when used retrospectively, i.e. when several claims have been approved, - # the method returns: - # - # 1. `true` if none of then claims have been flagged for QA - # 2. `true` if some claims have been flagged for QA using a lower MIN_QA_THRESHOLD - # 3. `false` if some claims have been flagged for QA using a higher MIN_QA_THRESHOLD - # - # Newly approved claims should not be flagged for QA for as long as the method - # returns `false`; they should be flagged for QA otherwise. - def self.below_min_qa_threshold? - return false if MIN_QA_THRESHOLD.zero? - - claims_approved_so_far = current_academic_year.approved.count - return true if claims_approved_so_far.zero? - - (current_academic_year.approved.qa_required.count.to_f / claims_approved_so_far) * 100 <= MIN_QA_THRESHOLD - end - def hold!(reason:, user:) if holdable? && !held? self.class.transaction do @@ -245,7 +220,32 @@ def holdable? end def flaggable_for_qa? - decision_made? && latest_decision.approved? && Claim.below_min_qa_threshold? && !awaiting_qa? && !qa_completed? + decision_made? && latest_decision.approved? && below_min_qa_threshold? && !awaiting_qa? && !qa_completed? + end + + # This method's intention is to help make a decision on whether a claim should + # be flagged for QA or not. These criteria need to be met for each academic year: + # + # 1. the first claim to be approved should always be flagged for QA + # 2. subsequently approved claims should be flagged for QA, 1 in 100/MIN_QA_THRESHOLD. + # + # This method should be used every time a new approval decision is being made; + # when used retrospectively, i.e. when several claims have been approved, + # the method returns: + # + # 1. `true` if none of then claims have been flagged for QA + # 2. `true` if some claims have been flagged for QA using a lower MIN_QA_THRESHOLD + # 3. `false` if some claims have been flagged for QA using a higher MIN_QA_THRESHOLD + # + # Newly approved claims should not be flagged for QA for as long as the method + # returns `false`; they should be flagged for QA otherwise. + def below_min_qa_threshold? + return false if MIN_QA_THRESHOLD.zero? + + claims_approved_so_far = Claim.current_academic_year.approved.count + return true if claims_approved_so_far.zero? + + (Claim.current_academic_year.approved.qa_required.count.to_f / claims_approved_so_far) * 100 <= MIN_QA_THRESHOLD end def qa_completed? diff --git a/spec/models/claim_spec.rb b/spec/models/claim_spec.rb index ef5c807cab..75897302b5 100644 --- a/spec/models/claim_spec.rb +++ b/spec/models/claim_spec.rb @@ -371,7 +371,7 @@ context "when above the min QA threshold" do before do - allow(Claim).to receive(:below_min_qa_threshold?).and_return(false) + stub_const("Claim::MIN_QA_THRESHOLD", 0) end it { is_expected.to eq(false) } @@ -379,7 +379,7 @@ context "when below the min QA threshold" do before do - allow(Claim).to receive(:below_min_qa_threshold?).and_return(true) + stub_const("Claim::MIN_QA_THRESHOLD", 100) end it { is_expected.to eq(true) } @@ -597,8 +597,8 @@ end end - describe ".below_min_qa_threshold?" do - subject { described_class.below_min_qa_threshold? } + describe "#below_min_qa_threshold?" do + subject { described_class.new.below_min_qa_threshold? } context "when the MIN_QA_THRESHOLD is set to zero" do before do