From cafc8c58c2cdb29410b3c80f16b072a63185f0f7 Mon Sep 17 00:00:00 2001 From: Moises Machado Date: Tue, 7 Jul 2009 18:40:48 -0300 Subject: [PATCH] ActionItem1128: create category sweeper to expire the categories_menu cache --- app/helpers/sweeper_helper.rb | 11 +++++++++++ app/sweepers/article_sweeper.rb | 8 +------- app/sweepers/category_sweeper.rb | 9 +++++++++ app/sweepers/friendship_sweeper.rb | 4 +--- app/sweepers/role_assignment_sweeper.rb | 4 +--- app/views/layouts/application.rhtml | 2 +- config/environment.rb | 2 +- test/integration/categories_menu_test.rb | 2 +- 8 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 app/helpers/sweeper_helper.rb create mode 100644 app/sweepers/category_sweeper.rb diff --git a/app/helpers/sweeper_helper.rb b/app/helpers/sweeper_helper.rb new file mode 100644 index 0000000..155a897 --- /dev/null +++ b/app/helpers/sweeper_helper.rb @@ -0,0 +1,11 @@ +module SweeperHelper + + def expire_fragment(*args) + ActionController::Base.new().expire_fragment(*args) + end + + def expire_timeout_fragment(*args) + ActionController::Base.new().expire_timeout_fragment(*args) + end + +end diff --git a/app/sweepers/article_sweeper.rb b/app/sweepers/article_sweeper.rb index cc2c180..cc35ad7 100644 --- a/app/sweepers/article_sweeper.rb +++ b/app/sweepers/article_sweeper.rb @@ -1,4 +1,5 @@ class ArticleSweeper < ActiveRecord::Observer + include SweeperHelper observe :article def after_save(article) @@ -21,11 +22,4 @@ protected end end - def expire_fragment(*args) - ActionController::Base.new().expire_fragment(*args) - end - - def expire_timeout_fragment(*args) - ActionController::Base.new().expire_timeout_fragment(*args) - end end diff --git a/app/sweepers/category_sweeper.rb b/app/sweepers/category_sweeper.rb new file mode 100644 index 0000000..92a0491 --- /dev/null +++ b/app/sweepers/category_sweeper.rb @@ -0,0 +1,9 @@ +class CategorySweeper < ActiveRecord::Observer + observe :category + include SweeperHelper + + def after_save(category) + expire_fragment(category.environment.name + "_categories_menu") + end + +end diff --git a/app/sweepers/friendship_sweeper.rb b/app/sweepers/friendship_sweeper.rb index 6bb546a..5a50be8 100644 --- a/app/sweepers/friendship_sweeper.rb +++ b/app/sweepers/friendship_sweeper.rb @@ -1,5 +1,6 @@ class FriendshipSweeper < ActiveRecord::Observer observe :friendship + include SweeperHelper def after_create(friendship) expire_caches(friendship) @@ -26,7 +27,4 @@ protected blocks.map(&:cache_keys).each{|ck|expire_timeout_fragment(ck)} end - def expire_timeout_fragment(*args) - ActionController::Base.new().expire_timeout_fragment(*args) - end end diff --git a/app/sweepers/role_assignment_sweeper.rb b/app/sweepers/role_assignment_sweeper.rb index e4b2536..7e79068 100644 --- a/app/sweepers/role_assignment_sweeper.rb +++ b/app/sweepers/role_assignment_sweeper.rb @@ -1,5 +1,6 @@ class RoleAssignmentSweeper < ActiveRecord::Observer observe :role_assignment + include SweeperHelper def after_create(role_assignment) expire_caches(role_assignment) @@ -28,7 +29,4 @@ protected } end - def expire_timeout_fragment(*args) - ActionController::Base.new().expire_timeout_fragment(*args) - end end diff --git a/app/views/layouts/application.rhtml b/app/views/layouts/application.rhtml index 73e7d5f..e968992 100644 --- a/app/views/layouts/application.rhtml +++ b/app/views/layouts/application.rhtml @@ -87,7 +87,7 @@ :id=>"menu_link_to_envhome", :title=>@environment.name %> <% unless environment.enabled?(:disable_categories) %> - <% cache(:controller => 'public', :action => 'categories_menu') do %> + <% cache(environment.name + '_categories_menu') do %> <%= render :file => 'shared/categories_menu' %> <% end %> <% end %> diff --git a/config/environment.rb b/config/environment.rb index f2167bf..70e1c7f 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -67,7 +67,7 @@ Rails::Initializer.run do |config| # don't load the sweepers while loading the database unless $PROGRAM_NAME =~ /rake$/ && ARGV.first == 'db:schema:load' - config.active_record.observers = :article_sweeper, :role_assignment_sweeper, :friendship_sweeper + config.active_record.observers = :article_sweeper, :role_assignment_sweeper, :friendship_sweeper, :category_sweeper end # Make Active Record use UTC-base instead of local time # config.active_record.default_timezone = :utc diff --git a/test/integration/categories_menu_test.rb b/test/integration/categories_menu_test.rb index 3636224..f960ac0 100644 --- a/test/integration/categories_menu_test.rb +++ b/test/integration/categories_menu_test.rb @@ -35,7 +35,7 @@ class CategoriesMenuTest < ActionController::IntegrationTest end should 'cache the categories menu' do - ActionView::Base.any_instance.expects(:cache).with(:controller => 'public', :action => 'categories_menu') + ActionView::Base.any_instance.expects(:cache).with(Environment.default.name + "_categories_menu") get '/' end -- libgit2 0.21.2