[#4604] Add basic caching for TimeEntry#cost

This commit is contained in:
Eric Davis
2010-10-06 12:12:45 -07:00
parent cec35ad3af
commit dbfc6c5335
3 changed files with 66 additions and 8 deletions
+20 -7
View File
@@ -20,15 +20,28 @@ module RateTimeEntryPatch
module InstanceMethods
# Returns the current cost of the TimeEntry based on it's rate and hours
def cost
if self.rate.nil?
amount = Rate.amount_for(self.user, self.project, self.spent_on.to_s)
else
amount = rate.amount
unless @cost
if self.rate.nil?
amount = Rate.amount_for(self.user, self.project, self.spent_on.to_s)
else
amount = rate.amount
end
if amount.nil?
@cost = 0.0
else
@cost = amount.to_f * hours.to_f
end
cache_cost
end
return 0.0 if amount.nil?
return amount.to_f * hours.to_f
@cost
end
def cache_cost
@cost ||= cost
update_attribute(:cost, @cost)
end
end
end