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 | 67 | if request.post? |
68 | 68 | @article.last_changed_by = user |
69 | 69 | if @article.update_attributes(params[:article]) |
70 | - expire_caches(@article) | |
71 | 70 | redirect_back |
72 | 71 | return |
73 | 72 | end |
... | ... | @@ -114,7 +113,6 @@ class CmsController < MyProfileController |
114 | 113 | @article.last_changed_by = user |
115 | 114 | if request.post? |
116 | 115 | if @article.save |
117 | - expire_caches(@article) | |
118 | 116 | redirect_back |
119 | 117 | return |
120 | 118 | end |
... | ... | @@ -161,7 +159,6 @@ class CmsController < MyProfileController |
161 | 159 | def destroy |
162 | 160 | @article = profile.articles.find(params[:id]) |
163 | 161 | if request.post? |
164 | - expire_caches(@article) | |
165 | 162 | @article.destroy |
166 | 163 | redirect_to :action => (@article.parent ? 'view' : 'index'), :id => @article.parent |
167 | 164 | end |
... | ... | @@ -252,12 +249,5 @@ class CmsController < MyProfileController |
252 | 249 | nil |
253 | 250 | end |
254 | 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 | 252 | end |
263 | 253 | ... | ... |
... | ... | @@ -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 | 10 | # Bootstrap the Rails environment, frameworks, and default configuration |
11 | 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 | 23 | # extra directories for controllers organization |
14 | 24 | extra_controller_dirs = %w[ |
15 | 25 | app/controllers/my_profile |
... | ... | @@ -37,6 +47,7 @@ Rails::Initializer.run do |config| |
37 | 47 | |
38 | 48 | # Add additional load paths for your own custom dirs |
39 | 49 | # config.load_paths += %W( #{RAILS_ROOT}/extras ) |
50 | + config.load_paths += %W( #{RAILS_ROOT}/app/sweepers ) | |
40 | 51 | |
41 | 52 | # Force all environments to use the same logger level |
42 | 53 | # (by default production uses :info, the others :debug) |
... | ... | @@ -53,6 +64,7 @@ Rails::Initializer.run do |config| |
53 | 64 | |
54 | 65 | # Activate observers that should always be running |
55 | 66 | # config.active_record.observers = :cacher, :garbage_collector |
67 | + config.active_record.observers = :article_sweeper | |
56 | 68 | # Make Active Record use UTC-base instead of local time |
57 | 69 | # config.active_record.default_timezone = :utc |
58 | 70 | |
... | ... | @@ -94,7 +106,7 @@ end |
94 | 106 | # Include your application configuration below |
95 | 107 | |
96 | 108 | |
97 | -require 'gettext/rails' | |
109 | + | |
98 | 110 | Noosfero.locales = { |
99 | 111 | 'en' => 'English', |
100 | 112 | 'pt_BR' => 'Português', |
... | ... | @@ -107,17 +119,7 @@ Tag.hierarchical = true |
107 | 119 | |
108 | 120 | # several local libraries |
109 | 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 | 122 | require 'sqlite_extension' |
118 | -require 'will_paginate' | |
119 | - | |
120 | -require 'route_if' | |
121 | 123 | |
122 | 124 | # load a local configuration if present, but not under test environment. |
123 | 125 | if ENV['RAILS_ENV'] != 'test' | ... | ... |
test/functional/profile_design_controller_test.rb
... | ... | @@ -60,6 +60,8 @@ class ProfileDesignControllerTest < Test::Unit::TestCase |
60 | 60 | |
61 | 61 | @request.env['HTTP_REFERER'] = '/editor' |
62 | 62 | |
63 | + holder.blocks(true) | |
64 | + | |
63 | 65 | @controller.stubs(:boxes_holder).returns(holder) |
64 | 66 | login_as 'designtestuser' |
65 | 67 | end |
... | ... | @@ -211,6 +213,7 @@ class ProfileDesignControllerTest < Test::Unit::TestCase |
211 | 213 | enterprise.boxes.first.blocks << block |
212 | 214 | enterprise.add_admin(holder) |
213 | 215 | |
216 | + enterprise.blocks(true) | |
214 | 217 | @controller.stubs(:boxes_holder).returns(enterprise) |
215 | 218 | login_as('designtestuser') |
216 | 219 | |
... | ... | @@ -230,6 +233,7 @@ class ProfileDesignControllerTest < Test::Unit::TestCase |
230 | 233 | enterprise.boxes.first.blocks << block |
231 | 234 | enterprise.add_admin(holder) |
232 | 235 | |
236 | + enterprise.blocks(true) | |
233 | 237 | @controller.stubs(:boxes_holder).returns(enterprise) |
234 | 238 | login_as('designtestuser') |
235 | 239 | |
... | ... | @@ -296,6 +300,7 @@ class ProfileDesignControllerTest < Test::Unit::TestCase |
296 | 300 | |
297 | 301 | should 'be able to edit FeedReaderBlock' do |
298 | 302 | @box1.blocks << FeedReaderBlock.new(:address => 'feed address') |
303 | + holder.blocks(true) | |
299 | 304 | |
300 | 305 | get :edit, :profile => 'designtestuser', :id => @box1.blocks[-1].id |
301 | 306 | |
... | ... | @@ -306,6 +311,7 @@ class ProfileDesignControllerTest < Test::Unit::TestCase |
306 | 311 | |
307 | 312 | should 'be able to save FeedReaderBlock configurations' do |
308 | 313 | @box1.blocks << FeedReaderBlock.new(:address => 'feed address') |
314 | + holder.blocks(true) | |
309 | 315 | |
310 | 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 | 239 | profile.boxes.first.blocks << MainBlock.new |
240 | 240 | |
241 | 241 | profile_boxes = profile.boxes.size |
242 | - profile_blocks = profile.blocks.size | |
242 | + profile_blocks = profile.blocks(true).size | |
243 | 243 | |
244 | 244 | assert profile_boxes > 0, 'profile should have some boxes' |
245 | 245 | assert profile_blocks > 0, 'profile should have some blocks' | ... | ... |