diff --git a/app/models/rate.rb b/app/models/rate.rb index 160109c..9978e22 100644 --- a/app/models/rate.rb +++ b/app/models/rate.rb @@ -70,7 +70,7 @@ class Rate < ActiveRecord::Base return rate.amount end - def self.update_all_time_entires_with_missing_cost + def self.update_all_time_entries_with_missing_cost Lockfile('update_cost_cache', :retries => 0) do TimeEntry.all(:conditions => {:cost => nil}).each do |time_entry| begin diff --git a/lib/tasks/cache.rake b/lib/tasks/cache.rake index 76365bd..e60cf15 100644 --- a/lib/tasks/cache.rake +++ b/lib/tasks/cache.rake @@ -2,7 +2,7 @@ namespace :rate_plugin do namespace :cache do desc "Update Time Entry cost caches" task :update_cost_cache => :environment do - Rate.update_all_time_entires_with_missing_cost + Rate.update_all_time_entries_with_missing_cost end end end diff --git a/test/unit/rate_test.rb b/test/unit/rate_test.rb index 0223b00..e2267e4 100644 --- a/test/unit/rate_test.rb +++ b/test/unit/rate_test.rb @@ -269,5 +269,29 @@ class RateTest < ActiveSupport::TestCase end end - + + context "#update_all_time_entries_with_missing_cost" do + setup do + @user = User.generate! + @project = Project.generate! + @date = Date.today.to_s + @rate = Rate.generate!(:user => @user, :project => @project, :date_in_effect => @date, :amount => 200.0) + @time_entry1 = TimeEntry.generate!({:user => @user, :project => @project, :spent_on => @date, :hours => 10.0, :activity => TimeEntryActivity.generate!}) + @time_entry2 = TimeEntry.generate!({:user => @user, :project => @project, :spent_on => @date, :hours => 20.0, :activity => TimeEntryActivity.generate!}) + end + + should "update the caches of all Time Entries" do + TimeEntry.update_all('cost = null') + + # Check that cost is NULL in the database, which skips the caching + assert_equal "2", ActiveRecord::Base.connection.select_all('select count(*) as count from time_entries where cost IS NULL').first["count"] + + Rate.update_all_time_entries_with_missing_cost + + assert_equal "0", ActiveRecord::Base.connection.select_all('select count(*) as count from time_entries where cost IS NULL').first["count"] + + end + + should "timestamp a successful run" + end end