Commit 221acbe37eed5716bc9be773fb71843ca49aa6f2

Authored by Joenio Costa
Committed by Antonio Terceiro
1 parent 856560cc

ActionItem955: cache crash blog pagination and year/month filter

app/controllers/public/content_viewer_controller.rb
... ... @@ -14,10 +14,6 @@ class ContentViewerController < ApplicationController
14 14 return
15 15 end
16 16 else
17   - path.gsub!(/\/(\d{4})\/(\d{2})\Z/, '')
18   - year = $1
19   - month = $2
20   -
21 17 @page = profile.articles.find_by_path(path)
22 18 unless @page
23 19 page_from_old_path = profile.articles.find_by_old_path(path)
... ... @@ -79,7 +75,7 @@ class ContentViewerController < ApplicationController
79 75 end
80 76  
81 77 if @page.blog?
82   - @page.filter = {:year => year, :month => month}
  78 + @page.filter = {:year => params[:year], :month => params[:month]}
83 79 end
84 80  
85 81 if @page.folder? && @page.view_as == 'image_gallery'
... ...
app/models/article.rb
... ... @@ -258,8 +258,11 @@ class Article < ActiveRecord::Base
258 258 profile
259 259 end
260 260  
261   - def cache_key
262   - "article-id-#{id}"
  261 + def cache_key(params = {})
  262 + "article-id-#{id}" +
  263 + (params[:npage] ? "-npage-#{params[:npage]}" : '') +
  264 + (params[:year] ? "-year-#{params[:year]}" : '') +
  265 + (params[:month] ? "-month-#{params[:month]}" : '')
263 266 end
264 267  
265 268 private
... ...
app/models/blog_archives_block.rb
... ... @@ -22,7 +22,7 @@ class BlogArchivesBlock < Block
22 22 results << content_tag('li', content_tag('strong', "#{year} (#{results_by_year.size})"))
23 23 results << "<ul class='#{year}-archive'>"
24 24 results_by_year.group_by{|i| [ ('%02d' % i.published_at.month()), gettext(MONTHS[i.published_at.month() - 1])]}.sort.each do |month, results_by_month|
25   - results << content_tag('li', link_to("#{month[1]} (#{results_by_month.size})", owner.generate_url(:controller => 'content_viewer', :action => 'view_page', :page => [owner.blog.path, year, month[0]])))
  25 + results << content_tag('li', link_to("#{month[1]} (#{results_by_month.size})", owner.generate_url(:controller => 'content_viewer', :action => 'view_page', :page => [owner.blog.path], :year => year, :month => month[0])))
26 26 end
27 27 results << "</ul>"
28 28 end
... ...
app/views/content_viewer/view_page.rhtml
... ... @@ -83,7 +83,7 @@
83 83 </div>
84 84 <% end %>
85 85  
86   -<% cache(@page.cache_key) do %>
  86 +<% cache(@page.cache_key(params)) do %>
87 87 <div class="<%="article-body article-body-" + @page.css_class_name %>">
88 88 <%= article_to_html(@page) %>
89 89 <br style="clear:both" />
... ...
test/functional/content_viewer_controller_test.rb
... ... @@ -638,10 +638,10 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase
638 638 assert_tag :tag => 'a', :attributes => { :href => "/#{profile.identifier}/#{blog.path}?npage=2", :rel => 'next' }
639 639 end
640 640  
641   - should 'extract year and month from path' do
  641 + should 'set year and month filter from URL params' do
642 642 profile.articles << Blog.new(:name => 'A blog test', :profile => profile)
643 643 year, month = profile.blog.created_at.year.to_s, '%02d' % profile.blog.created_at.month
644   - get :view_page, :profile => profile.identifier, :page => [profile.blog.path, year, month]
  644 + get :view_page, :profile => profile.identifier, :page => [profile.blog.path], :year => year, :month => month
645 645 assert_equal({ :year => year.to_s, :month => month.to_s }, assigns(:page).filter)
646 646 end
647 647  
... ...
test/unit/article_test.rb
... ... @@ -695,4 +695,16 @@ class ArticleTest &lt; Test::Unit::TestCase
695 695 assert_equal a.created_at, a.published_at
696 696 end
697 697  
  698 + should 'use npage to compose cache key' do
  699 + a = Article.new
  700 + a.expects(:id).returns(34)
  701 + assert_equal 'article-id-34-npage-2', a.cache_key(:npage => 2)
  702 + end
  703 +
  704 + should 'use year and month to compose cache key' do
  705 + a = Article.new
  706 + a.expects(:id).returns(34)
  707 + assert_equal 'article-id-34-year-2009-month-04', a.cache_key(:year => '2009', :month => '04')
  708 + end
  709 +
698 710 end
... ...
test/unit/blog_archives_block_test.rb
... ... @@ -38,7 +38,7 @@ class BlogArchivesBlockTest &lt; ActiveSupport::TestCase
38 38 end
39 39 block = BlogArchivesBlock.new
40 40 block.stubs(:owner).returns(profile)
41   - assert_tag_in_string block.content, :tag => 'a', :content => 'January (10)', :attributes => {:href => /2008\/01/}
  41 + assert_tag_in_string block.content, :tag => 'a', :content => 'January (10)', :attributes => {:href => 'http://colivre.net/flatline/blog?month=01&year=2008' }
42 42 end
43 43  
44 44 should 'order list of amount posts' do
... ...
test/unit/blog_test.rb
... ... @@ -126,4 +126,14 @@ class BlogTest &lt; ActiveSupport::TestCase
126 126 assert ! blog.valid?
127 127 end
128 128  
  129 + should 'expires cache when update post' do
  130 + p = create_user('testuser').person
  131 + blog = Blog.create!(:name => 'Blog test', :profile => p)
  132 + post = Article.create!(:name => 'First post', :profile => p, :parent => blog)
  133 + ArticleSweeper.any_instance.expects(:expire_fragment).with(/#{blog.cache_key}/)
  134 + ArticleSweeper.any_instance.expects(:expire_fragment).with(/#{post.cache_key}/)
  135 + post.name = 'Edited First post'
  136 + assert post.save!
  137 + end
  138 +
129 139 end
... ...