From 221acbe37eed5716bc9be773fb71843ca49aa6f2 Mon Sep 17 00:00:00 2001 From: Joenio Costa Date: Wed, 22 Apr 2009 23:08:49 -0300 Subject: [PATCH] ActionItem955: cache crash blog pagination and year/month filter --- app/controllers/public/content_viewer_controller.rb | 6 +----- app/models/article.rb | 7 +++++-- app/models/blog_archives_block.rb | 2 +- app/views/content_viewer/view_page.rhtml | 2 +- test/functional/content_viewer_controller_test.rb | 4 ++-- test/unit/article_test.rb | 12 ++++++++++++ test/unit/blog_archives_block_test.rb | 2 +- test/unit/blog_test.rb | 10 ++++++++++ 8 files changed, 33 insertions(+), 12 deletions(-) diff --git a/app/controllers/public/content_viewer_controller.rb b/app/controllers/public/content_viewer_controller.rb index 5bf74ed..a83e608 100644 --- a/app/controllers/public/content_viewer_controller.rb +++ b/app/controllers/public/content_viewer_controller.rb @@ -14,10 +14,6 @@ class ContentViewerController < ApplicationController return end else - path.gsub!(/\/(\d{4})\/(\d{2})\Z/, '') - year = $1 - month = $2 - @page = profile.articles.find_by_path(path) unless @page page_from_old_path = profile.articles.find_by_old_path(path) @@ -79,7 +75,7 @@ class ContentViewerController < ApplicationController end if @page.blog? - @page.filter = {:year => year, :month => month} + @page.filter = {:year => params[:year], :month => params[:month]} end if @page.folder? && @page.view_as == 'image_gallery' diff --git a/app/models/article.rb b/app/models/article.rb index e72683b..42f15f0 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -258,8 +258,11 @@ class Article < ActiveRecord::Base profile end - def cache_key - "article-id-#{id}" + def cache_key(params = {}) + "article-id-#{id}" + + (params[:npage] ? "-npage-#{params[:npage]}" : '') + + (params[:year] ? "-year-#{params[:year]}" : '') + + (params[:month] ? "-month-#{params[:month]}" : '') end private diff --git a/app/models/blog_archives_block.rb b/app/models/blog_archives_block.rb index d397021..d2c95b5 100644 --- a/app/models/blog_archives_block.rb +++ b/app/models/blog_archives_block.rb @@ -22,7 +22,7 @@ class BlogArchivesBlock < Block results << content_tag('li', content_tag('strong', "#{year} (#{results_by_year.size})")) results << "" end diff --git a/app/views/content_viewer/view_page.rhtml b/app/views/content_viewer/view_page.rhtml index ff1290c..2982c2c 100644 --- a/app/views/content_viewer/view_page.rhtml +++ b/app/views/content_viewer/view_page.rhtml @@ -83,7 +83,7 @@ <% end %> -<% cache(@page.cache_key) do %> +<% cache(@page.cache_key(params)) do %>
"> <%= article_to_html(@page) %>
diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb index b1a7cc7..642d28f 100644 --- a/test/functional/content_viewer_controller_test.rb +++ b/test/functional/content_viewer_controller_test.rb @@ -638,10 +638,10 @@ class ContentViewerControllerTest < Test::Unit::TestCase assert_tag :tag => 'a', :attributes => { :href => "/#{profile.identifier}/#{blog.path}?npage=2", :rel => 'next' } end - should 'extract year and month from path' do + should 'set year and month filter from URL params' do profile.articles << Blog.new(:name => 'A blog test', :profile => profile) year, month = profile.blog.created_at.year.to_s, '%02d' % profile.blog.created_at.month - get :view_page, :profile => profile.identifier, :page => [profile.blog.path, year, month] + get :view_page, :profile => profile.identifier, :page => [profile.blog.path], :year => year, :month => month assert_equal({ :year => year.to_s, :month => month.to_s }, assigns(:page).filter) end diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 3a0b56c..7196539 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -695,4 +695,16 @@ class ArticleTest < Test::Unit::TestCase assert_equal a.created_at, a.published_at 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) + 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') + end + end diff --git a/test/unit/blog_archives_block_test.rb b/test/unit/blog_archives_block_test.rb index deafe26..217294e 100644 --- a/test/unit/blog_archives_block_test.rb +++ b/test/unit/blog_archives_block_test.rb @@ -38,7 +38,7 @@ class BlogArchivesBlockTest < ActiveSupport::TestCase end block = BlogArchivesBlock.new block.stubs(:owner).returns(profile) - assert_tag_in_string block.content, :tag => 'a', :content => 'January (10)', :attributes => {:href => /2008\/01/} + assert_tag_in_string block.content, :tag => 'a', :content => 'January (10)', :attributes => {:href => 'http://colivre.net/flatline/blog?month=01&year=2008' } end should 'order list of amount posts' do diff --git a/test/unit/blog_test.rb b/test/unit/blog_test.rb index 744ec4a..e5f8c28 100644 --- a/test/unit/blog_test.rb +++ b/test/unit/blog_test.rb @@ -126,4 +126,14 @@ 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 + end -- libgit2 0.21.2