diff --git a/app/controllers/my_profile/cms_controller.rb b/app/controllers/my_profile/cms_controller.rb index 8ac9e16..8704657 100644 --- a/app/controllers/my_profile/cms_controller.rb +++ b/app/controllers/my_profile/cms_controller.rb @@ -67,7 +67,6 @@ class CmsController < MyProfileController if request.post? @article.last_changed_by = user if @article.update_attributes(params[:article]) - expire_caches(@article) redirect_back return end @@ -114,7 +113,6 @@ class CmsController < MyProfileController @article.last_changed_by = user if request.post? if @article.save - expire_caches(@article) redirect_back return end @@ -161,7 +159,6 @@ class CmsController < MyProfileController def destroy @article = profile.articles.find(params[:id]) if request.post? - expire_caches(@article) @article.destroy redirect_to :action => (@article.parent ? 'view' : 'index'), :id => @article.parent end @@ -252,12 +249,5 @@ class CmsController < MyProfileController nil end end - - def expire_caches(article) - article.hierarchy.each {|a| expire_fragment(/#{a.cache_key}/) } - blocks = article.profile.blocks.select{|b|[RecentDocumentsBlock, BlogArchivesBlock].any?{|c| b.kind_of?(c)}} - blocks.map(&:cache_keys).each{|ck|expire_timeout_fragment(ck)} - end - end diff --git a/app/sweepers/article_sweeper.rb b/app/sweepers/article_sweeper.rb new file mode 100644 index 0000000..dd786e2 --- /dev/null +++ b/app/sweepers/article_sweeper.rb @@ -0,0 +1,27 @@ +class ArticleSweeper < ActiveRecord::Observer + observe :article + + def after_save(article) + expire_caches(article) + end + + def after_destroy(article) + expire_caches(article) + end + +protected + + def expire_caches(article) + article.hierarchy.each {|a| expire_fragment(/#{a.cache_key}/) } + blocks = article.profile.blocks.select{|b|[RecentDocumentsBlock, BlogArchivesBlock].any?{|c| b.kind_of?(c)}} + blocks.map(&:cache_keys).each{|ck|expire_timeout_fragment(ck)} + 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/config/environment.rb b/config/environment.rb index 7443476..df68b36 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -10,6 +10,16 @@ RAILS_GEM_VERSION = '2.0.2' unless defined? RAILS_GEM_VERSION # Bootstrap the Rails environment, frameworks, and default configuration require File.join(File.dirname(__FILE__), 'boot') +# locally-developed modules +require 'gettext/rails' +require 'acts_as_filesystem' +require 'acts_as_having_settings' +require 'acts_as_searchable' +require 'acts_as_having_boxes' +require 'acts_as_having_image' +require 'will_paginate' +require 'route_if' + # extra directories for controllers organization extra_controller_dirs = %w[ app/controllers/my_profile @@ -37,6 +47,7 @@ Rails::Initializer.run do |config| # Add additional load paths for your own custom dirs # config.load_paths += %W( #{RAILS_ROOT}/extras ) + config.load_paths += %W( #{RAILS_ROOT}/app/sweepers ) # Force all environments to use the same logger level # (by default production uses :info, the others :debug) @@ -53,6 +64,7 @@ Rails::Initializer.run do |config| # Activate observers that should always be running # config.active_record.observers = :cacher, :garbage_collector + config.active_record.observers = :article_sweeper # Make Active Record use UTC-base instead of local time # config.active_record.default_timezone = :utc @@ -94,7 +106,7 @@ end # Include your application configuration below -require 'gettext/rails' + Noosfero.locales = { 'en' => 'English', 'pt_BR' => 'Português', @@ -107,17 +119,7 @@ Tag.hierarchical = true # several local libraries require 'noosfero' - -# locally-developed modules -require 'acts_as_filesystem' -require 'acts_as_searchable' -require 'acts_as_having_boxes' -require 'acts_as_having_settings' -require 'acts_as_having_image' require 'sqlite_extension' -require 'will_paginate' - -require 'route_if' # load a local configuration if present, but not under test environment. if ENV['RAILS_ENV'] != 'test' diff --git a/test/functional/profile_design_controller_test.rb b/test/functional/profile_design_controller_test.rb index dd97196..15ab0ae 100644 --- a/test/functional/profile_design_controller_test.rb +++ b/test/functional/profile_design_controller_test.rb @@ -60,6 +60,8 @@ class ProfileDesignControllerTest < Test::Unit::TestCase @request.env['HTTP_REFERER'] = '/editor' + holder.blocks(true) + @controller.stubs(:boxes_holder).returns(holder) login_as 'designtestuser' end @@ -211,6 +213,7 @@ class ProfileDesignControllerTest < Test::Unit::TestCase enterprise.boxes.first.blocks << block enterprise.add_admin(holder) + enterprise.blocks(true) @controller.stubs(:boxes_holder).returns(enterprise) login_as('designtestuser') @@ -230,6 +233,7 @@ class ProfileDesignControllerTest < Test::Unit::TestCase enterprise.boxes.first.blocks << block enterprise.add_admin(holder) + enterprise.blocks(true) @controller.stubs(:boxes_holder).returns(enterprise) login_as('designtestuser') @@ -296,6 +300,7 @@ class ProfileDesignControllerTest < Test::Unit::TestCase should 'be able to edit FeedReaderBlock' do @box1.blocks << FeedReaderBlock.new(:address => 'feed address') + holder.blocks(true) get :edit, :profile => 'designtestuser', :id => @box1.blocks[-1].id @@ -306,6 +311,7 @@ class ProfileDesignControllerTest < Test::Unit::TestCase should 'be able to save FeedReaderBlock configurations' do @box1.blocks << FeedReaderBlock.new(:address => 'feed address') + holder.blocks(true) post :save, :profile => 'designtestuser', :id => @box1.blocks[-1].id, :block => {:address => 'new feed address', :limit => '20'} diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 051b590..34fce7b 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -239,7 +239,7 @@ class ProfileTest < Test::Unit::TestCase profile.boxes.first.blocks << MainBlock.new profile_boxes = profile.boxes.size - profile_blocks = profile.blocks.size + profile_blocks = profile.blocks(true).size assert profile_boxes > 0, 'profile should have some boxes' assert profile_blocks > 0, 'profile should have some blocks' -- libgit2 0.21.2