From 2051edacf727b97dec71510281c9f55def124631 Mon Sep 17 00:00:00 2001 From: Xavier Shay Date: Sat, 9 Jul 2011 10:26:24 +1000 Subject: [PATCH] Include last modified headers and conditional get support in posts controller. --- app/controllers/posts_controller.rb | 11 +++++++++++ app/models/post.rb | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 028d2b4..ee4246e 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -2,6 +2,7 @@ class PostsController < ApplicationController def index @tag = params[:tag] @posts = Post.find_recent(:tag => @tag, :include => :tags) + fresh_when last_modified: posts_last_updated(@posts), public: true respond_to do |format| format.html @@ -11,6 +12,16 @@ def index def show @post = Post.find_by_permalink(*([:year, :month, :day, :slug].collect {|x| params[x] } << {:include => [:comments, :tags]})) + fresh_when last_modified: @post.last_changed_at.utc, public: true @comment = Comment.new end + + private + + def posts_last_updated(posts) + ( + [Comment.where(post_id: posts.map(&:id)).maximum(:updated_at)] + + posts.map(&:updated_at) + ).max + end end diff --git a/app/models/post.rb b/app/models/post.rb index d8d4170..0d27208 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -37,6 +37,10 @@ def published? published_at? end + def last_changed_at + [comments.maximum(:updated_at), updated_at].compact.max + end + attr_accessor :published_at_natural def published_at_natural @published_at_natural ||= published_at.send_with_default(:strftime, 'now', "%Y-%m-%d %H:%M")