Skip to content

Commit

Permalink
Replaced constant PO_RATES with a cached model key
Browse files Browse the repository at this point in the history
  • Loading branch information
martent committed Sep 2, 2019
1 parent f78474c commit 162f4c0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 1 addition & 4 deletions app/lib/economy/cost_with_po_rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ module Economy
# Calulates costs of type FamilyAndEmergencyHomeCost and ExtraContribution
# that requires PoRate for a contractor
class CostWithPoRate < Base

PO_RATES = PoRate.all.to_a.freeze

def initialize(cost, options = {})
@cost = cost
@interval = { from: (options[:from] || cost.period_start), to: (options[:to] || Date.today) }
Expand Down Expand Up @@ -72,7 +69,7 @@ def months_and_po_rates(interval)
end

def po_rate_for_date(date)
po_rate = PO_RATES.find do |rate|
po_rate = PoRate.all_cached.find do |rate|
rate.start_date <= date && rate.end_date >= date
end

Expand Down
8 changes: 8 additions & 0 deletions app/models/po_rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ class PoRate < ApplicationRecord
validate do
date_range(:start_date, start_date, end_date)
end

after_commit do
Rails.cache.delete('PoRate/all')
end

def self.all_cached
Rails.cache.fetch('PoRate/all') { all.load }
end
end

0 comments on commit 162f4c0

Please sign in to comment.