Skip to content

Commit

Permalink
Include last modified headers and conditional get support in posts co…
Browse files Browse the repository at this point in the history
…ntroller.
  • Loading branch information
xaviershay committed Jul 9, 2011
1 parent b3bb3d9 commit 2051eda
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/controllers/posts_controller.rb
Expand Up @@ -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
Expand All @@ -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
4 changes: 4 additions & 0 deletions app/models/post.rb
Expand Up @@ -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")
Expand Down

0 comments on commit 2051eda

Please sign in to comment.