Commit 1ab18c97f17cc26ab9a78fa02fd9e136b8e28637
Committed by
Antonio Terceiro
1 parent
229bedec
Exists in
master
and in
29 other branches
ActionItem955: created a sweeper to observe the article model
changed the expiration of caches based on article changes to observe the model instead of being called by the cms controller so the daemon scripts that update/create/destroy articles cause the expiration of the caches too
Showing
5 changed files
with
47 additions
and
22 deletions
Show diff stats
app/controllers/my_profile/cms_controller.rb
@@ -67,7 +67,6 @@ class CmsController < MyProfileController | @@ -67,7 +67,6 @@ class CmsController < MyProfileController | ||
67 | if request.post? | 67 | if request.post? |
68 | @article.last_changed_by = user | 68 | @article.last_changed_by = user |
69 | if @article.update_attributes(params[:article]) | 69 | if @article.update_attributes(params[:article]) |
70 | - expire_caches(@article) | ||
71 | redirect_back | 70 | redirect_back |
72 | return | 71 | return |
73 | end | 72 | end |
@@ -114,7 +113,6 @@ class CmsController < MyProfileController | @@ -114,7 +113,6 @@ class CmsController < MyProfileController | ||
114 | @article.last_changed_by = user | 113 | @article.last_changed_by = user |
115 | if request.post? | 114 | if request.post? |
116 | if @article.save | 115 | if @article.save |
117 | - expire_caches(@article) | ||
118 | redirect_back | 116 | redirect_back |
119 | return | 117 | return |
120 | end | 118 | end |
@@ -161,7 +159,6 @@ class CmsController < MyProfileController | @@ -161,7 +159,6 @@ class CmsController < MyProfileController | ||
161 | def destroy | 159 | def destroy |
162 | @article = profile.articles.find(params[:id]) | 160 | @article = profile.articles.find(params[:id]) |
163 | if request.post? | 161 | if request.post? |
164 | - expire_caches(@article) | ||
165 | @article.destroy | 162 | @article.destroy |
166 | redirect_to :action => (@article.parent ? 'view' : 'index'), :id => @article.parent | 163 | redirect_to :action => (@article.parent ? 'view' : 'index'), :id => @article.parent |
167 | end | 164 | end |
@@ -252,12 +249,5 @@ class CmsController < MyProfileController | @@ -252,12 +249,5 @@ class CmsController < MyProfileController | ||
252 | nil | 249 | nil |
253 | end | 250 | end |
254 | end | 251 | end |
255 | - | ||
256 | - def expire_caches(article) | ||
257 | - article.hierarchy.each {|a| expire_fragment(/#{a.cache_key}/) } | ||
258 | - blocks = article.profile.blocks.select{|b|[RecentDocumentsBlock, BlogArchivesBlock].any?{|c| b.kind_of?(c)}} | ||
259 | - blocks.map(&:cache_keys).each{|ck|expire_timeout_fragment(ck)} | ||
260 | - end | ||
261 | - | ||
262 | end | 252 | end |
263 | 253 |
@@ -0,0 +1,27 @@ | @@ -0,0 +1,27 @@ | ||
1 | +class ArticleSweeper < ActiveRecord::Observer | ||
2 | + observe :article | ||
3 | + | ||
4 | + def after_save(article) | ||
5 | + expire_caches(article) | ||
6 | + end | ||
7 | + | ||
8 | + def after_destroy(article) | ||
9 | + expire_caches(article) | ||
10 | + end | ||
11 | + | ||
12 | +protected | ||
13 | + | ||
14 | + def expire_caches(article) | ||
15 | + article.hierarchy.each {|a| expire_fragment(/#{a.cache_key}/) } | ||
16 | + blocks = article.profile.blocks.select{|b|[RecentDocumentsBlock, BlogArchivesBlock].any?{|c| b.kind_of?(c)}} | ||
17 | + blocks.map(&:cache_keys).each{|ck|expire_timeout_fragment(ck)} | ||
18 | + end | ||
19 | + | ||
20 | + def expire_fragment(*args) | ||
21 | + ActionController::Base.new().expire_fragment(*args) | ||
22 | + end | ||
23 | + | ||
24 | + def expire_timeout_fragment(*args) | ||
25 | + ActionController::Base.new().expire_timeout_fragment(*args) | ||
26 | + end | ||
27 | +end |
config/environment.rb
@@ -10,6 +10,16 @@ RAILS_GEM_VERSION = '2.0.2' unless defined? RAILS_GEM_VERSION | @@ -10,6 +10,16 @@ RAILS_GEM_VERSION = '2.0.2' unless defined? RAILS_GEM_VERSION | ||
10 | # Bootstrap the Rails environment, frameworks, and default configuration | 10 | # Bootstrap the Rails environment, frameworks, and default configuration |
11 | require File.join(File.dirname(__FILE__), 'boot') | 11 | require File.join(File.dirname(__FILE__), 'boot') |
12 | 12 | ||
13 | +# locally-developed modules | ||
14 | +require 'gettext/rails' | ||
15 | +require 'acts_as_filesystem' | ||
16 | +require 'acts_as_having_settings' | ||
17 | +require 'acts_as_searchable' | ||
18 | +require 'acts_as_having_boxes' | ||
19 | +require 'acts_as_having_image' | ||
20 | +require 'will_paginate' | ||
21 | +require 'route_if' | ||
22 | + | ||
13 | # extra directories for controllers organization | 23 | # extra directories for controllers organization |
14 | extra_controller_dirs = %w[ | 24 | extra_controller_dirs = %w[ |
15 | app/controllers/my_profile | 25 | app/controllers/my_profile |
@@ -37,6 +47,7 @@ Rails::Initializer.run do |config| | @@ -37,6 +47,7 @@ Rails::Initializer.run do |config| | ||
37 | 47 | ||
38 | # Add additional load paths for your own custom dirs | 48 | # Add additional load paths for your own custom dirs |
39 | # config.load_paths += %W( #{RAILS_ROOT}/extras ) | 49 | # config.load_paths += %W( #{RAILS_ROOT}/extras ) |
50 | + config.load_paths += %W( #{RAILS_ROOT}/app/sweepers ) | ||
40 | 51 | ||
41 | # Force all environments to use the same logger level | 52 | # Force all environments to use the same logger level |
42 | # (by default production uses :info, the others :debug) | 53 | # (by default production uses :info, the others :debug) |
@@ -53,6 +64,7 @@ Rails::Initializer.run do |config| | @@ -53,6 +64,7 @@ Rails::Initializer.run do |config| | ||
53 | 64 | ||
54 | # Activate observers that should always be running | 65 | # Activate observers that should always be running |
55 | # config.active_record.observers = :cacher, :garbage_collector | 66 | # config.active_record.observers = :cacher, :garbage_collector |
67 | + config.active_record.observers = :article_sweeper | ||
56 | # Make Active Record use UTC-base instead of local time | 68 | # Make Active Record use UTC-base instead of local time |
57 | # config.active_record.default_timezone = :utc | 69 | # config.active_record.default_timezone = :utc |
58 | 70 | ||
@@ -94,7 +106,7 @@ end | @@ -94,7 +106,7 @@ end | ||
94 | # Include your application configuration below | 106 | # Include your application configuration below |
95 | 107 | ||
96 | 108 | ||
97 | -require 'gettext/rails' | 109 | + |
98 | Noosfero.locales = { | 110 | Noosfero.locales = { |
99 | 'en' => 'English', | 111 | 'en' => 'English', |
100 | 'pt_BR' => 'Português', | 112 | 'pt_BR' => 'Português', |
@@ -107,17 +119,7 @@ Tag.hierarchical = true | @@ -107,17 +119,7 @@ Tag.hierarchical = true | ||
107 | 119 | ||
108 | # several local libraries | 120 | # several local libraries |
109 | require 'noosfero' | 121 | require 'noosfero' |
110 | - | ||
111 | -# locally-developed modules | ||
112 | -require 'acts_as_filesystem' | ||
113 | -require 'acts_as_searchable' | ||
114 | -require 'acts_as_having_boxes' | ||
115 | -require 'acts_as_having_settings' | ||
116 | -require 'acts_as_having_image' | ||
117 | require 'sqlite_extension' | 122 | require 'sqlite_extension' |
118 | -require 'will_paginate' | ||
119 | - | ||
120 | -require 'route_if' | ||
121 | 123 | ||
122 | # load a local configuration if present, but not under test environment. | 124 | # load a local configuration if present, but not under test environment. |
123 | if ENV['RAILS_ENV'] != 'test' | 125 | if ENV['RAILS_ENV'] != 'test' |
test/functional/profile_design_controller_test.rb
@@ -60,6 +60,8 @@ class ProfileDesignControllerTest < Test::Unit::TestCase | @@ -60,6 +60,8 @@ class ProfileDesignControllerTest < Test::Unit::TestCase | ||
60 | 60 | ||
61 | @request.env['HTTP_REFERER'] = '/editor' | 61 | @request.env['HTTP_REFERER'] = '/editor' |
62 | 62 | ||
63 | + holder.blocks(true) | ||
64 | + | ||
63 | @controller.stubs(:boxes_holder).returns(holder) | 65 | @controller.stubs(:boxes_holder).returns(holder) |
64 | login_as 'designtestuser' | 66 | login_as 'designtestuser' |
65 | end | 67 | end |
@@ -211,6 +213,7 @@ class ProfileDesignControllerTest < Test::Unit::TestCase | @@ -211,6 +213,7 @@ class ProfileDesignControllerTest < Test::Unit::TestCase | ||
211 | enterprise.boxes.first.blocks << block | 213 | enterprise.boxes.first.blocks << block |
212 | enterprise.add_admin(holder) | 214 | enterprise.add_admin(holder) |
213 | 215 | ||
216 | + enterprise.blocks(true) | ||
214 | @controller.stubs(:boxes_holder).returns(enterprise) | 217 | @controller.stubs(:boxes_holder).returns(enterprise) |
215 | login_as('designtestuser') | 218 | login_as('designtestuser') |
216 | 219 | ||
@@ -230,6 +233,7 @@ class ProfileDesignControllerTest < Test::Unit::TestCase | @@ -230,6 +233,7 @@ class ProfileDesignControllerTest < Test::Unit::TestCase | ||
230 | enterprise.boxes.first.blocks << block | 233 | enterprise.boxes.first.blocks << block |
231 | enterprise.add_admin(holder) | 234 | enterprise.add_admin(holder) |
232 | 235 | ||
236 | + enterprise.blocks(true) | ||
233 | @controller.stubs(:boxes_holder).returns(enterprise) | 237 | @controller.stubs(:boxes_holder).returns(enterprise) |
234 | login_as('designtestuser') | 238 | login_as('designtestuser') |
235 | 239 | ||
@@ -296,6 +300,7 @@ class ProfileDesignControllerTest < Test::Unit::TestCase | @@ -296,6 +300,7 @@ class ProfileDesignControllerTest < Test::Unit::TestCase | ||
296 | 300 | ||
297 | should 'be able to edit FeedReaderBlock' do | 301 | should 'be able to edit FeedReaderBlock' do |
298 | @box1.blocks << FeedReaderBlock.new(:address => 'feed address') | 302 | @box1.blocks << FeedReaderBlock.new(:address => 'feed address') |
303 | + holder.blocks(true) | ||
299 | 304 | ||
300 | get :edit, :profile => 'designtestuser', :id => @box1.blocks[-1].id | 305 | get :edit, :profile => 'designtestuser', :id => @box1.blocks[-1].id |
301 | 306 | ||
@@ -306,6 +311,7 @@ class ProfileDesignControllerTest < Test::Unit::TestCase | @@ -306,6 +311,7 @@ class ProfileDesignControllerTest < Test::Unit::TestCase | ||
306 | 311 | ||
307 | should 'be able to save FeedReaderBlock configurations' do | 312 | should 'be able to save FeedReaderBlock configurations' do |
308 | @box1.blocks << FeedReaderBlock.new(:address => 'feed address') | 313 | @box1.blocks << FeedReaderBlock.new(:address => 'feed address') |
314 | + holder.blocks(true) | ||
309 | 315 | ||
310 | post :save, :profile => 'designtestuser', :id => @box1.blocks[-1].id, :block => {:address => 'new feed address', :limit => '20'} | 316 | post :save, :profile => 'designtestuser', :id => @box1.blocks[-1].id, :block => {:address => 'new feed address', :limit => '20'} |
311 | 317 |
test/unit/profile_test.rb
@@ -239,7 +239,7 @@ class ProfileTest < Test::Unit::TestCase | @@ -239,7 +239,7 @@ class ProfileTest < Test::Unit::TestCase | ||
239 | profile.boxes.first.blocks << MainBlock.new | 239 | profile.boxes.first.blocks << MainBlock.new |
240 | 240 | ||
241 | profile_boxes = profile.boxes.size | 241 | profile_boxes = profile.boxes.size |
242 | - profile_blocks = profile.blocks.size | 242 | + profile_blocks = profile.blocks(true).size |
243 | 243 | ||
244 | assert profile_boxes > 0, 'profile should have some boxes' | 244 | assert profile_boxes > 0, 'profile should have some boxes' |
245 | assert profile_blocks > 0, 'profile should have some blocks' | 245 | assert profile_blocks > 0, 'profile should have some blocks' |