Skip to content

Commit

Permalink
Make below_min_qa_threshold an instance method
Browse files Browse the repository at this point in the history
This method is only used in one place, in an instance method on Claim,
so it doesn't need to be a class method. By moving this method to be an
instance method it will be easier to overwrite the MIN_QA_THRESHOLD on a
per policy basis.
  • Loading branch information
rjlynch committed Jul 2, 2024
1 parent db317ae commit de4fc18
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
52 changes: 26 additions & 26 deletions app/models/claim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down
8 changes: 4 additions & 4 deletions spec/models/claim_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -371,15 +371,15 @@

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) }
end

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) }
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit de4fc18

Please sign in to comment.