Commit cafc8c58c2cdb29410b3c80f16b072a63185f0f7

Authored by Moises Machado
Committed by Antonio Terceiro
1 parent 7c1dde22

ActionItem1128: create category sweeper to expire the categories_menu cache

* added a sweeper helper to remove code duplication
  * changed the fragment cache key for the category menu cache
app/helpers/sweeper_helper.rb 0 → 100644
@@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
  1 +module SweeperHelper
  2 +
  3 + def expire_fragment(*args)
  4 + ActionController::Base.new().expire_fragment(*args)
  5 + end
  6 +
  7 + def expire_timeout_fragment(*args)
  8 + ActionController::Base.new().expire_timeout_fragment(*args)
  9 + end
  10 +
  11 +end
app/sweepers/article_sweeper.rb
1 class ArticleSweeper < ActiveRecord::Observer 1 class ArticleSweeper < ActiveRecord::Observer
  2 + include SweeperHelper
2 observe :article 3 observe :article
3 4
4 def after_save(article) 5 def after_save(article)
@@ -21,11 +22,4 @@ protected @@ -21,11 +22,4 @@ protected
21 end 22 end
22 end 23 end
23 24
24 - def expire_fragment(*args)  
25 - ActionController::Base.new().expire_fragment(*args)  
26 - end  
27 -  
28 - def expire_timeout_fragment(*args)  
29 - ActionController::Base.new().expire_timeout_fragment(*args)  
30 - end  
31 end 25 end
app/sweepers/category_sweeper.rb 0 → 100644
@@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
  1 +class CategorySweeper < ActiveRecord::Observer
  2 + observe :category
  3 + include SweeperHelper
  4 +
  5 + def after_save(category)
  6 + expire_fragment(category.environment.name + "_categories_menu")
  7 + end
  8 +
  9 +end
app/sweepers/friendship_sweeper.rb
1 class FriendshipSweeper < ActiveRecord::Observer 1 class FriendshipSweeper < ActiveRecord::Observer
2 observe :friendship 2 observe :friendship
  3 + include SweeperHelper
3 4
4 def after_create(friendship) 5 def after_create(friendship)
5 expire_caches(friendship) 6 expire_caches(friendship)
@@ -26,7 +27,4 @@ protected @@ -26,7 +27,4 @@ protected
26 blocks.map(&:cache_keys).each{|ck|expire_timeout_fragment(ck)} 27 blocks.map(&:cache_keys).each{|ck|expire_timeout_fragment(ck)}
27 end 28 end
28 29
29 - def expire_timeout_fragment(*args)  
30 - ActionController::Base.new().expire_timeout_fragment(*args)  
31 - end  
32 end 30 end
app/sweepers/role_assignment_sweeper.rb
1 class RoleAssignmentSweeper < ActiveRecord::Observer 1 class RoleAssignmentSweeper < ActiveRecord::Observer
2 observe :role_assignment 2 observe :role_assignment
  3 + include SweeperHelper
3 4
4 def after_create(role_assignment) 5 def after_create(role_assignment)
5 expire_caches(role_assignment) 6 expire_caches(role_assignment)
@@ -28,7 +29,4 @@ protected @@ -28,7 +29,4 @@ protected
28 } 29 }
29 end 30 end
30 31
31 - def expire_timeout_fragment(*args)  
32 - ActionController::Base.new().expire_timeout_fragment(*args)  
33 - end  
34 end 32 end
app/views/layouts/application.rhtml
@@ -87,7 +87,7 @@ @@ -87,7 +87,7 @@
87 :id=>"menu_link_to_envhome", 87 :id=>"menu_link_to_envhome",
88 :title=>@environment.name %> 88 :title=>@environment.name %>
89 <% unless environment.enabled?(:disable_categories) %> 89 <% unless environment.enabled?(:disable_categories) %>
90 - <% cache(:controller => 'public', :action => 'categories_menu') do %> 90 + <% cache(environment.name + '_categories_menu') do %>
91 <%= render :file => 'shared/categories_menu' %> 91 <%= render :file => 'shared/categories_menu' %>
92 <% end %> 92 <% end %>
93 <% end %> 93 <% end %>
config/environment.rb
@@ -67,7 +67,7 @@ Rails::Initializer.run do |config| @@ -67,7 +67,7 @@ Rails::Initializer.run do |config|
67 67
68 # don't load the sweepers while loading the database 68 # don't load the sweepers while loading the database
69 unless $PROGRAM_NAME =~ /rake$/ && ARGV.first == 'db:schema:load' 69 unless $PROGRAM_NAME =~ /rake$/ && ARGV.first == 'db:schema:load'
70 - config.active_record.observers = :article_sweeper, :role_assignment_sweeper, :friendship_sweeper 70 + config.active_record.observers = :article_sweeper, :role_assignment_sweeper, :friendship_sweeper, :category_sweeper
71 end 71 end
72 # Make Active Record use UTC-base instead of local time 72 # Make Active Record use UTC-base instead of local time
73 # config.active_record.default_timezone = :utc 73 # config.active_record.default_timezone = :utc
test/integration/categories_menu_test.rb
@@ -35,7 +35,7 @@ class CategoriesMenuTest &lt; ActionController::IntegrationTest @@ -35,7 +35,7 @@ class CategoriesMenuTest &lt; ActionController::IntegrationTest
35 end 35 end
36 36
37 should 'cache the categories menu' do 37 should 'cache the categories menu' do
38 - ActionView::Base.any_instance.expects(:cache).with(:controller => 'public', :action => 'categories_menu') 38 + ActionView::Base.any_instance.expects(:cache).with(Environment.default.name + "_categories_menu")
39 get '/' 39 get '/'
40 end 40 end
41 41