From 2fd72e941a04770080b5a88f536d55dd8fbac365 Mon Sep 17 00:00:00 2001 From: Daniela Soares Feitosa Date: Fri, 21 Aug 2009 19:48:08 -0300 Subject: [PATCH] ActionItem1185: fixing blog cache --- app/models/article.rb | 13 +++++++++++-- app/sweepers/article_sweeper.rb | 6 +++++- app/views/content_viewer/view_page.rhtml | 4 ++-- db/migrate/070_add_updated_at_to_profiles.rb | 10 ++++++++++ db/migrate/071_remove_lock_version.rb | 9 +++++++++ test/unit/article_test.rb | 26 ++++++++++++++++++++------ test/unit/blog_test.rb | 10 ---------- vendor/plugins/touch/init.rb | 13 +++++++++++++ 8 files changed, 70 insertions(+), 21 deletions(-) create mode 100644 db/migrate/070_add_updated_at_to_profiles.rb create mode 100644 db/migrate/071_remove_lock_version.rb create mode 100644 vendor/plugins/touch/init.rb diff --git a/app/models/article.rb b/app/models/article.rb index 5ac8c6a..9686564 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -214,6 +214,13 @@ class Article < ActiveRecord::Base end end + def allow_post_content?(logged_person = nil) + if logged_person && logged_person.has_permission?('post_content', profile) + return true + end + false + end + def comments_updated ferret_update end @@ -266,8 +273,10 @@ class Article < ActiveRecord::Base profile end - def cache_key(params = {}) - "article-id-#{id}" + + alias :active_record_cache_key :cache_key + def cache_key(params = {}, the_profile = nil) + active_record_cache_key + + (allow_post_content?(the_profile) ? "-owner" : '') + (params[:npage] ? "-npage-#{params[:npage]}" : '') + (params[:year] ? "-year-#{params[:year]}" : '') + (params[:month] ? "-month-#{params[:month]}" : '') diff --git a/app/sweepers/article_sweeper.rb b/app/sweepers/article_sweeper.rb index d0a0650..4037221 100644 --- a/app/sweepers/article_sweeper.rb +++ b/app/sweepers/article_sweeper.rb @@ -13,7 +13,11 @@ class ArticleSweeper < ActiveRecord::Observer protected def expire_caches(article) - article.hierarchy.each {|a| expire_fragment(a.cache_key) } + article.hierarchy.each do |a| + if a != article + a.touch + end + end blocks = (article.profile.blocks + article.profile.environment.blocks).select{|b|[RecentDocumentsBlock, BlogArchivesBlock].any?{|c| b.kind_of?(c)}} blocks.map(&:cache_keys).each{|ck|expire_timeout_fragment(ck)} env = article.profile.environment diff --git a/app/views/content_viewer/view_page.rhtml b/app/views/content_viewer/view_page.rhtml index 8ab75b8..35425a8 100644 --- a/app/views/content_viewer/view_page.rhtml +++ b/app/views/content_viewer/view_page.rhtml @@ -23,7 +23,7 @@
<%= article_title(@page, :no_link => true) %> - <% if logged_in? && current_user.person.has_permission?('post_content', profile) %> + <% if @page.allow_post_content?(user) %>
<% unless @page.blog? %> <%= link_to content_tag( 'span', label_for_edit_article(@page) ), @@ -80,7 +80,7 @@
<% end %> -<% cache(@page.cache_key(params)) do %> +<% cache(@page.cache_key(params, user)) do %>
"> <%= article_to_html(@page) %>
diff --git a/db/migrate/070_add_updated_at_to_profiles.rb b/db/migrate/070_add_updated_at_to_profiles.rb new file mode 100644 index 0000000..b6e1bd6 --- /dev/null +++ b/db/migrate/070_add_updated_at_to_profiles.rb @@ -0,0 +1,10 @@ +class AddUpdatedAtToProfiles < ActiveRecord::Migration + def self.up + add_column :profiles, :updated_at, :datetime + execute 'update profiles set updated_at = created_at' + end + + def self.down + remove_column :profiles, :updated_at + end +end diff --git a/db/migrate/071_remove_lock_version.rb b/db/migrate/071_remove_lock_version.rb new file mode 100644 index 0000000..8ad2251 --- /dev/null +++ b/db/migrate/071_remove_lock_version.rb @@ -0,0 +1,9 @@ +class RemoveLockVersion < ActiveRecord::Migration + def self.up + remove_column :articles, :lock_version + end + + def self.down + add_column :articles, :lock_version, :integer, :default => 0 + end +end diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 759dfcb..d3a0344 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -705,15 +705,13 @@ class ArticleTest < Test::Unit::TestCase end should 'use npage to compose cache key' do - a = Article.new - a.expects(:id).returns(34) - assert_equal 'article-id-34-npage-2', a.cache_key(:npage => 2) + a = Article.create!(:name => 'Published at', :profile => profile) + assert_match(/-npage-2/,a.cache_key(:npage => 2)) end should 'use year and month to compose cache key' do - a = Article.new - a.expects(:id).returns(34) - assert_equal 'article-id-34-year-2009-month-04', a.cache_key(:year => '2009', :month => '04') + a = Article.create!(:name => 'Published at', :profile => profile) + assert_match(/-year-2009-month-04/, a.cache_key(:year => '2009', :month => '04')) end should 'not be highlighted by default' do @@ -749,4 +747,20 @@ class ArticleTest < Test::Unit::TestCase assert_equal [c], a.categories end + should 'add owner on cache_key when has profile' do + a = profile.articles.create!(:name => 'a test article') + assert_match(/-owner/, a.cache_key({}, profile)) + end + + should 'not add owner on cache_key when has no profile' do + a = profile.articles.create!(:name => 'a test article') + assert_no_match(/-owner/, a.cache_key({})) + end + + should 'add owner on cache_key when profile is community' do + c = Community.create!(:name => 'new_comm') + a = c.articles.create!(:name => 'a test article') + assert_match(/-owner/, a.cache_key({}, c)) + end + end diff --git a/test/unit/blog_test.rb b/test/unit/blog_test.rb index f39f08c..125d68c 100644 --- a/test/unit/blog_test.rb +++ b/test/unit/blog_test.rb @@ -130,16 +130,6 @@ class BlogTest < ActiveSupport::TestCase assert ! blog.valid? end - should 'expires cache when update post' do - p = create_user('testuser').person - blog = Blog.create!(:name => 'Blog test', :profile => p) - post = Article.create!(:name => 'First post', :profile => p, :parent => blog) - ArticleSweeper.any_instance.expects(:expire_fragment).with(blog.cache_key) - ArticleSweeper.any_instance.expects(:expire_fragment).with(post.cache_key) - post.name = 'Edited First post' - assert post.save! - end - should 'remove external feed when removing blog' do p = create_user('testuser').person blog = Blog.create!(:name => 'Blog test', :profile => p, :external_feed_builder => {:enabled => true, :address => "http://bli.org/feed"}) diff --git a/vendor/plugins/touch/init.rb b/vendor/plugins/touch/init.rb new file mode 100644 index 0000000..df721a4 --- /dev/null +++ b/vendor/plugins/touch/init.rb @@ -0,0 +1,13 @@ +if ActiveRecord::Base.instance_methods.include?("touch") && Class.const_defined?('TOUCH_LOADED') + puts "W: ActiveRecord already provides a touch method, which means you must be using rails 2.3.3 or later." + puts "W: In this case the touch plugin could probably be removed" +end +TOUCH_LOADED = true + +module Touch + def touch + update_attribute(:updated_at, Time.now) + end +end + +ActiveRecord::Base.send(:include, Touch) -- libgit2 0.21.2