Commit 2fd72e941a04770080b5a88f536d55dd8fbac365

Authored by Daniela Feitosa
Committed by Antonio Terceiro
1 parent 9628a907

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
app/models/article.rb
... ... @@ -214,6 +214,13 @@ class Article < ActiveRecord::Base
214 214 end
215 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 224 def comments_updated
218 225 ferret_update
219 226 end
... ... @@ -266,8 +273,10 @@ class Article < ActiveRecord::Base
266 273 profile
267 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 280 (params[:npage] ? "-npage-#{params[:npage]}" : '') +
272 281 (params[:year] ? "-year-#{params[:year]}" : '') +
273 282 (params[:month] ? "-month-#{params[:month]}" : '')
... ...
app/sweepers/article_sweeper.rb
... ... @@ -13,7 +13,11 @@ class ArticleSweeper < ActiveRecord::Observer
13 13 protected
14 14  
15 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 21 blocks = (article.profile.blocks + article.profile.environment.blocks).select{|b|[RecentDocumentsBlock, BlogArchivesBlock].any?{|c| b.kind_of?(c)}}
18 22 blocks.map(&:cache_keys).each{|ck|expire_timeout_fragment(ck)}
19 23 env = article.profile.environment
... ...
app/views/content_viewer/view_page.rhtml
... ... @@ -23,7 +23,7 @@
23 23  
24 24 <div>
25 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 27 <div id="article-actions">
28 28 <% unless @page.blog? %>
29 29 <%= link_to content_tag( 'span', label_for_edit_article(@page) ),
... ... @@ -80,7 +80,7 @@
80 80 </div>
81 81 <% end %>
82 82  
83   -<% cache(@page.cache_key(params)) do %>
  83 +<% cache(@page.cache_key(params, user)) do %>
84 84 <div class="<%="article-body article-body-" + @page.css_class_name %>">
85 85 <%= article_to_html(@page) %>
86 86 <br style="clear:both" />
... ...
db/migrate/070_add_updated_at_to_profiles.rb 0 → 100644
... ... @@ -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
... ...
db/migrate/071_remove_lock_version.rb 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +class RemoveLockVersion < ActiveRecord::Migration
  2 + def self.up
  3 + remove_column :articles, :lock_version
  4 + end
  5 +
  6 + def self.down
  7 + add_column :articles, :lock_version, :integer, :default => 0
  8 + end
  9 +end
... ...
test/unit/article_test.rb
... ... @@ -705,15 +705,13 @@ class ArticleTest &lt; Test::Unit::TestCase
705 705 end
706 706  
707 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 710 end
712 711  
713 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 715 end
718 716  
719 717 should 'not be highlighted by default' do
... ... @@ -749,4 +747,20 @@ class ArticleTest &lt; Test::Unit::TestCase
749 747 assert_equal [c], a.categories
750 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 766 end
... ...
test/unit/blog_test.rb
... ... @@ -130,16 +130,6 @@ class BlogTest &lt; ActiveSupport::TestCase
130 130 assert ! blog.valid?
131 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 133 should 'remove external feed when removing blog' do
144 134 p = create_user('testuser').person
145 135 blog = Blog.create!(:name => 'Blog test', :profile => p, :external_feed_builder => {:enabled => true, :address => "http://bli.org/feed"})
... ...
vendor/plugins/touch/init.rb 0 → 100644
... ... @@ -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)
... ...