[#4604] Add basic caching for TimeEntry#cost
This commit is contained in:
9
db/migrate/004_add_cost_to_time_entries.rb
Normal file
9
db/migrate/004_add_cost_to_time_entries.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
class AddCostToTimeEntries < ActiveRecord::Migration
|
||||
def self.up
|
||||
add_column :time_entries, :cost, :decimal, :precision => 15, :scale => 2
|
||||
end
|
||||
|
||||
def self.down
|
||||
remove_column :time_entries, :cost
|
||||
end
|
||||
end
|
||||
@@ -20,15 +20,28 @@ module RateTimeEntryPatch
|
||||
module InstanceMethods
|
||||
# Returns the current cost of the TimeEntry based on it's rate and hours
|
||||
def cost
|
||||
unless @cost
|
||||
if self.rate.nil?
|
||||
amount = Rate.amount_for(self.user, self.project, self.spent_on.to_s)
|
||||
else
|
||||
amount = rate.amount
|
||||
end
|
||||
|
||||
return 0.0 if amount.nil?
|
||||
if amount.nil?
|
||||
@cost = 0.0
|
||||
else
|
||||
@cost = amount.to_f * hours.to_f
|
||||
end
|
||||
|
||||
return amount.to_f * hours.to_f
|
||||
cache_cost
|
||||
end
|
||||
|
||||
@cost
|
||||
end
|
||||
|
||||
def cache_cost
|
||||
@cost ||= cost
|
||||
update_attribute(:cost, @cost)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,7 +5,7 @@ class RateTimeEntryPatchTest < ActiveSupport::TestCase
|
||||
@user = User.generate!
|
||||
@project = Project.generate!
|
||||
@date = Date.today.to_s
|
||||
@time_entry = TimeEntry.new({:user => @user, :project => @project, :spent_on => @date, :hours => 10.0})
|
||||
@time_entry = TimeEntry.new({:user => @user, :project => @project, :spent_on => @date, :hours => 10.0, :activity => TimeEntryActivity.generate!})
|
||||
end
|
||||
|
||||
should 'should return 0.0 if there are no rates for the user' do
|
||||
@@ -26,4 +26,40 @@ class RateTimeEntryPatchTest < ActiveSupport::TestCase
|
||||
|
||||
end
|
||||
|
||||
context "#cost" do
|
||||
setup do
|
||||
@time_entry.save!
|
||||
Rate.generate!(:user => @user, :project => @project, :date_in_effect => @date, :amount => 200.0)
|
||||
end
|
||||
|
||||
context "without a cache" do
|
||||
should "return the calculated cost" do
|
||||
@time_entry.update_attribute(:cost, nil)
|
||||
assert_equal 2000.0, @time_entry.cost
|
||||
end
|
||||
|
||||
should "cache the cost to the field" do
|
||||
@time_entry.update_attribute(:cost, nil)
|
||||
@time_entry.cost
|
||||
|
||||
assert_equal 2000.0, @time_entry.read_attribute(:cost)
|
||||
assert_equal 2000.0, @time_entry.reload.read_attribute(:cost)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "with a cache" do
|
||||
setup do
|
||||
@time_entry.cache_cost
|
||||
end
|
||||
|
||||
should "return the cached cost" do
|
||||
assert_equal 2000.0, @time_entry.read_attribute(:cost)
|
||||
assert_equal 2000.0, @time_entry.cost
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user