[#4604] Simplify the cost caching system
This commit is contained in:
@@ -9,8 +9,7 @@ module RateTimeEntryPatch
|
|||||||
unloadable # Send unloadable so it will not be unloaded in development
|
unloadable # Send unloadable so it will not be unloaded in development
|
||||||
belongs_to :rate
|
belongs_to :rate
|
||||||
|
|
||||||
before_save :clear_cost_cache
|
before_save :cost
|
||||||
before_save :cache_cost
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -31,8 +30,10 @@ module RateTimeEntryPatch
|
|||||||
|
|
||||||
module InstanceMethods
|
module InstanceMethods
|
||||||
# Returns the current cost of the TimeEntry based on it's rate and hours
|
# Returns the current cost of the TimeEntry based on it's rate and hours
|
||||||
|
#
|
||||||
|
# Is a read-through cache method
|
||||||
def cost
|
def cost
|
||||||
unless @cost
|
unless read_attribute(:cost)
|
||||||
if self.rate.nil?
|
if self.rate.nil?
|
||||||
amount = Rate.amount_for(self.user, self.project, self.spent_on.to_s)
|
amount = Rate.amount_for(self.user, self.project, self.spent_on.to_s)
|
||||||
else
|
else
|
||||||
@@ -40,27 +41,20 @@ module RateTimeEntryPatch
|
|||||||
end
|
end
|
||||||
|
|
||||||
if amount.nil?
|
if amount.nil?
|
||||||
@cost = 0.0
|
write_attribute(:cost, 0.0)
|
||||||
else
|
else
|
||||||
@cost = amount.to_f * hours.to_f
|
# Write the cost to the database for caching
|
||||||
|
update_attribute(:cost, amount.to_f * hours.to_f)
|
||||||
end
|
end
|
||||||
|
|
||||||
cache_cost
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@cost
|
read_attribute(:cost)
|
||||||
end
|
end
|
||||||
|
|
||||||
def clear_cost_cache
|
def clear_cost_cache
|
||||||
@cost = nil
|
|
||||||
write_attribute(:cost, nil)
|
write_attribute(:cost, nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
def cache_cost
|
|
||||||
@cost ||= cost
|
|
||||||
write_attribute(:cost, @cost)
|
|
||||||
end
|
|
||||||
|
|
||||||
def save_cached_cost
|
def save_cached_cost
|
||||||
clear_cost_cache
|
clear_cost_cache
|
||||||
update_attribute(:cost, cost)
|
update_attribute(:cost, cost)
|
||||||
|
|||||||
@@ -50,7 +50,8 @@ class RateTimeEntryPatchTest < ActiveSupport::TestCase
|
|||||||
|
|
||||||
context "with a cache" do
|
context "with a cache" do
|
||||||
setup do
|
setup do
|
||||||
@time_entry.cache_cost
|
@time_entry.update_attribute(:cost, 2000.0)
|
||||||
|
@time_entry.reload
|
||||||
end
|
end
|
||||||
|
|
||||||
should "return the cached cost" do
|
should "return the cached cost" do
|
||||||
|
|||||||
Reference in New Issue
Block a user