Commit 2fd72e941a04770080b5a88f536d55dd8fbac365
Committed by
Antonio Terceiro
1 parent
9628a907
Exists in
master
and in
28 other branches
ActionItem1185: fixing blog cache
* now is created one cache to user who can edit and other to other people * added touch plugin to update articles * added migrate to add column updated_at to profiles * added migrate to remove column lock_version from articles
Showing
8 changed files
with
70 additions
and
21 deletions
Show diff stats
app/models/article.rb
| @@ -214,6 +214,13 @@ class Article < ActiveRecord::Base | @@ -214,6 +214,13 @@ class Article < ActiveRecord::Base | ||
| 214 | end | 214 | end |
| 215 | end | 215 | end |
| 216 | 216 | ||
| 217 | + def allow_post_content?(logged_person = nil) | ||
| 218 | + if logged_person && logged_person.has_permission?('post_content', profile) | ||
| 219 | + return true | ||
| 220 | + end | ||
| 221 | + false | ||
| 222 | + end | ||
| 223 | + | ||
| 217 | def comments_updated | 224 | def comments_updated |
| 218 | ferret_update | 225 | ferret_update |
| 219 | end | 226 | end |
| @@ -266,8 +273,10 @@ class Article < ActiveRecord::Base | @@ -266,8 +273,10 @@ class Article < ActiveRecord::Base | ||
| 266 | profile | 273 | profile |
| 267 | end | 274 | end |
| 268 | 275 | ||
| 269 | - def cache_key(params = {}) | ||
| 270 | - "article-id-#{id}" + | 276 | + alias :active_record_cache_key :cache_key |
| 277 | + def cache_key(params = {}, the_profile = nil) | ||
| 278 | + active_record_cache_key + | ||
| 279 | + (allow_post_content?(the_profile) ? "-owner" : '') + | ||
| 271 | (params[:npage] ? "-npage-#{params[:npage]}" : '') + | 280 | (params[:npage] ? "-npage-#{params[:npage]}" : '') + |
| 272 | (params[:year] ? "-year-#{params[:year]}" : '') + | 281 | (params[:year] ? "-year-#{params[:year]}" : '') + |
| 273 | (params[:month] ? "-month-#{params[:month]}" : '') | 282 | (params[:month] ? "-month-#{params[:month]}" : '') |
app/sweepers/article_sweeper.rb
| @@ -13,7 +13,11 @@ class ArticleSweeper < ActiveRecord::Observer | @@ -13,7 +13,11 @@ class ArticleSweeper < ActiveRecord::Observer | ||
| 13 | protected | 13 | protected |
| 14 | 14 | ||
| 15 | def expire_caches(article) | 15 | def expire_caches(article) |
| 16 | - article.hierarchy.each {|a| expire_fragment(a.cache_key) } | 16 | + article.hierarchy.each do |a| |
| 17 | + if a != article | ||
| 18 | + a.touch | ||
| 19 | + end | ||
| 20 | + end | ||
| 17 | blocks = (article.profile.blocks + article.profile.environment.blocks).select{|b|[RecentDocumentsBlock, BlogArchivesBlock].any?{|c| b.kind_of?(c)}} | 21 | blocks = (article.profile.blocks + article.profile.environment.blocks).select{|b|[RecentDocumentsBlock, BlogArchivesBlock].any?{|c| b.kind_of?(c)}} |
| 18 | blocks.map(&:cache_keys).each{|ck|expire_timeout_fragment(ck)} | 22 | blocks.map(&:cache_keys).each{|ck|expire_timeout_fragment(ck)} |
| 19 | env = article.profile.environment | 23 | env = article.profile.environment |
app/views/content_viewer/view_page.rhtml
| @@ -23,7 +23,7 @@ | @@ -23,7 +23,7 @@ | ||
| 23 | 23 | ||
| 24 | <div> | 24 | <div> |
| 25 | <%= article_title(@page, :no_link => true) %> | 25 | <%= article_title(@page, :no_link => true) %> |
| 26 | - <% if logged_in? && current_user.person.has_permission?('post_content', profile) %> | 26 | + <% if @page.allow_post_content?(user) %> |
| 27 | <div id="article-actions"> | 27 | <div id="article-actions"> |
| 28 | <% unless @page.blog? %> | 28 | <% unless @page.blog? %> |
| 29 | <%= link_to content_tag( 'span', label_for_edit_article(@page) ), | 29 | <%= link_to content_tag( 'span', label_for_edit_article(@page) ), |
| @@ -80,7 +80,7 @@ | @@ -80,7 +80,7 @@ | ||
| 80 | </div> | 80 | </div> |
| 81 | <% end %> | 81 | <% end %> |
| 82 | 82 | ||
| 83 | -<% cache(@page.cache_key(params)) do %> | 83 | +<% cache(@page.cache_key(params, user)) do %> |
| 84 | <div class="<%="article-body article-body-" + @page.css_class_name %>"> | 84 | <div class="<%="article-body article-body-" + @page.css_class_name %>"> |
| 85 | <%= article_to_html(@page) %> | 85 | <%= article_to_html(@page) %> |
| 86 | <br style="clear:both" /> | 86 | <br style="clear:both" /> |
| @@ -0,0 +1,10 @@ | @@ -0,0 +1,10 @@ | ||
| 1 | +class AddUpdatedAtToProfiles < ActiveRecord::Migration | ||
| 2 | + def self.up | ||
| 3 | + add_column :profiles, :updated_at, :datetime | ||
| 4 | + execute 'update profiles set updated_at = created_at' | ||
| 5 | + end | ||
| 6 | + | ||
| 7 | + def self.down | ||
| 8 | + remove_column :profiles, :updated_at | ||
| 9 | + end | ||
| 10 | +end |
test/unit/article_test.rb
| @@ -705,15 +705,13 @@ class ArticleTest < Test::Unit::TestCase | @@ -705,15 +705,13 @@ class ArticleTest < Test::Unit::TestCase | ||
| 705 | end | 705 | end |
| 706 | 706 | ||
| 707 | should 'use npage to compose cache key' do | 707 | should 'use npage to compose cache key' do |
| 708 | - a = Article.new | ||
| 709 | - a.expects(:id).returns(34) | ||
| 710 | - assert_equal 'article-id-34-npage-2', a.cache_key(:npage => 2) | 708 | + a = Article.create!(:name => 'Published at', :profile => profile) |
| 709 | + assert_match(/-npage-2/,a.cache_key(:npage => 2)) | ||
| 711 | end | 710 | end |
| 712 | 711 | ||
| 713 | should 'use year and month to compose cache key' do | 712 | should 'use year and month to compose cache key' do |
| 714 | - a = Article.new | ||
| 715 | - a.expects(:id).returns(34) | ||
| 716 | - assert_equal 'article-id-34-year-2009-month-04', a.cache_key(:year => '2009', :month => '04') | 713 | + a = Article.create!(:name => 'Published at', :profile => profile) |
| 714 | + assert_match(/-year-2009-month-04/, a.cache_key(:year => '2009', :month => '04')) | ||
| 717 | end | 715 | end |
| 718 | 716 | ||
| 719 | should 'not be highlighted by default' do | 717 | should 'not be highlighted by default' do |
| @@ -749,4 +747,20 @@ class ArticleTest < Test::Unit::TestCase | @@ -749,4 +747,20 @@ class ArticleTest < Test::Unit::TestCase | ||
| 749 | assert_equal [c], a.categories | 747 | assert_equal [c], a.categories |
| 750 | end | 748 | end |
| 751 | 749 | ||
| 750 | + should 'add owner on cache_key when has profile' do | ||
| 751 | + a = profile.articles.create!(:name => 'a test article') | ||
| 752 | + assert_match(/-owner/, a.cache_key({}, profile)) | ||
| 753 | + end | ||
| 754 | + | ||
| 755 | + should 'not add owner on cache_key when has no profile' do | ||
| 756 | + a = profile.articles.create!(:name => 'a test article') | ||
| 757 | + assert_no_match(/-owner/, a.cache_key({})) | ||
| 758 | + end | ||
| 759 | + | ||
| 760 | + should 'add owner on cache_key when profile is community' do | ||
| 761 | + c = Community.create!(:name => 'new_comm') | ||
| 762 | + a = c.articles.create!(:name => 'a test article') | ||
| 763 | + assert_match(/-owner/, a.cache_key({}, c)) | ||
| 764 | + end | ||
| 765 | + | ||
| 752 | end | 766 | end |
test/unit/blog_test.rb
| @@ -130,16 +130,6 @@ class BlogTest < ActiveSupport::TestCase | @@ -130,16 +130,6 @@ class BlogTest < ActiveSupport::TestCase | ||
| 130 | assert ! blog.valid? | 130 | assert ! blog.valid? |
| 131 | end | 131 | end |
| 132 | 132 | ||
| 133 | - should 'expires cache when update post' do | ||
| 134 | - p = create_user('testuser').person | ||
| 135 | - blog = Blog.create!(:name => 'Blog test', :profile => p) | ||
| 136 | - post = Article.create!(:name => 'First post', :profile => p, :parent => blog) | ||
| 137 | - ArticleSweeper.any_instance.expects(:expire_fragment).with(blog.cache_key) | ||
| 138 | - ArticleSweeper.any_instance.expects(:expire_fragment).with(post.cache_key) | ||
| 139 | - post.name = 'Edited First post' | ||
| 140 | - assert post.save! | ||
| 141 | - end | ||
| 142 | - | ||
| 143 | should 'remove external feed when removing blog' do | 133 | should 'remove external feed when removing blog' do |
| 144 | p = create_user('testuser').person | 134 | p = create_user('testuser').person |
| 145 | blog = Blog.create!(:name => 'Blog test', :profile => p, :external_feed_builder => {:enabled => true, :address => "http://bli.org/feed"}) | 135 | blog = Blog.create!(:name => 'Blog test', :profile => p, :external_feed_builder => {:enabled => true, :address => "http://bli.org/feed"}) |
| @@ -0,0 +1,13 @@ | @@ -0,0 +1,13 @@ | ||
| 1 | +if ActiveRecord::Base.instance_methods.include?("touch") && Class.const_defined?('TOUCH_LOADED') | ||
| 2 | + puts "W: ActiveRecord already provides a touch method, which means you must be using rails 2.3.3 or later." | ||
| 3 | + puts "W: In this case the touch plugin could probably be removed" | ||
| 4 | +end | ||
| 5 | +TOUCH_LOADED = true | ||
| 6 | + | ||
| 7 | +module Touch | ||
| 8 | + def touch | ||
| 9 | + update_attribute(:updated_at, Time.now) | ||
| 10 | + end | ||
| 11 | +end | ||
| 12 | + | ||
| 13 | +ActiveRecord::Base.send(:include, Touch) |