From 347be0633b51db75697b369a417a3b28d7450c8e Mon Sep 17 00:00:00 2001 From: Skud Date: Tue, 20 Aug 2013 18:59:13 +1000 Subject: [PATCH] cache posts/forums on homepage --- app/controllers/forums_controller.rb | 3 +++ app/models/forum_sweeper.rb | 16 ++++++++++++++++ app/views/home/_discuss.html.haml | 22 ++++++++++++---------- 3 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 app/models/forum_sweeper.rb diff --git a/app/controllers/forums_controller.rb b/app/controllers/forums_controller.rb index 6dbfb06ef..c37dc4224 100644 --- a/app/controllers/forums_controller.rb +++ b/app/controllers/forums_controller.rb @@ -1,5 +1,8 @@ class ForumsController < ApplicationController load_and_authorize_resource + + cache_sweeper :forum_sweeper + # GET /forums # GET /forums.json def index diff --git a/app/models/forum_sweeper.rb b/app/models/forum_sweeper.rb new file mode 100644 index 000000000..cccffc524 --- /dev/null +++ b/app/models/forum_sweeper.rb @@ -0,0 +1,16 @@ +class ForumSweeper < ActionController::Caching::Sweeper + observe Forum + + def after_create(forum) + expire_fragment('homepage_forums') + end + + def after_update(forum) + expire_fragment('homepage_forums') + end + + def after_destroy(forum) + expire_fragment('homepage_forums') + end + +end diff --git a/app/views/home/_discuss.html.haml b/app/views/home/_discuss.html.haml index 17b231e78..0d3e3252a 100644 --- a/app/views/home/_discuss.html.haml +++ b/app/views/home/_discuss.html.haml @@ -1,16 +1,18 @@ %h2 Discussion -- posts = Post.limit(6) -- if posts - =render :partial => "posts/summary", :locals => { :posts => posts, :howmany => 6 } +- cache "recent_posts" do + - posts = Post.limit(6) + - if posts + =render :partial => "posts/summary", :locals => { :posts => posts, :howmany => 6 } -- forums = Forum.all -- if forums - %ul.inline - %li - %strong Forums: - - forums.each do |f| - %li= link_to f.name, f +- cache "homepage_forums" do + - forums = Forum.all + - if forums + %ul.inline + %li + %strong Forums: + - forums.each do |f| + %li= link_to f.name, f %p.text-right = link_to "View all posts ยป", posts_path