Skip to content

Commit

Permalink
Cache building line items
Browse files Browse the repository at this point in the history
We hit this line items method multiple times with the same policy, cache
the results.
  • Loading branch information
rjlynch committed Oct 4, 2024
1 parent cdbd983 commit 4726b10
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions app/models/payroll_run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,34 @@ def payments_count
@payments_count ||= payments.count
end


def line_items(policy, filter: :all)
items = []
case filter
when :all
policy_items(policy)
when :claims
policy_items(policy).select { |item| item.is_a?(Claim) }
when :topups
policy_items(policy).select { |item| item.is_a?(Topup) }
end
end

def policy_items(policy)
policy_items_cache[policy] ||= build_policy_items(policy)
end

def policy_items_cache
@policy_items_cache ||= {}
end

def build_policy_items(policy)
policy_payments = if policy == :all
payments
else
payments.joins(:claims).merge(Claim.by_policy(policy))
end

items = []

policy_payments.includes(:claims).includes(:topups).each do |payment|
payment.claims.each do |claim|
# Payments can have multiple claims
Expand All @@ -109,14 +127,7 @@ def line_items(policy, filter: :all)
end
end

case filter
when :all
items
when :claims
items.select { |item| item.is_a?(Claim) }
when :topups
items.select { |item| item.is_a?(Topup) }
end
items
end

def ensure_no_payroll_run_this_month
Expand Down

0 comments on commit 4726b10

Please sign in to comment.