Skip to content

Instantly share code, notes, and snippets.

@gma
Created September 27, 2010 17:21
Show Gist options
  • Save gma/599418 to your computer and use it in GitHub Desktop.
Save gma/599418 to your computer and use it in GitHub Desktop.
Enabling article dates in Nesta URLs
# This code enables URLs with dates in for pages that have date set.
#
# If you use this code then a page published on 27 September 2010 with a path
# (relative to Nesta's content directory) of /foo/bar will now be available
# from /2010/09/07/foo/bar.
#
# Installation:
#
# $ mkdir -p local
# $ cat >> local/app.rb # (paste the following code, then press ^D)
class Page
def self.find_by_path_and_date(path, year, month, day)
page = find_by_path(path)
page.published_on?(year, month, day) ? page : nil
end
def published_on?(year, month, day)
date = self.date
date.year == year.to_i && date.month == month.to_i && date.day == day.to_i
end
def abspath
date.nil? ? super : File.join("/" + date.strftime("%Y/%m/%d"), super)
end
end
get "/:year/:month/:day/*" do
set_common_variables
parts = params[:splat].map { |p| p.sub(/\/$/, "") }
@page = Page.find_by_path_and_date(
File.join(parts), params[:year], params[:month], params[:day])
raise Sinatra::NotFound if @page.nil?
set_title(@page)
set_from_page(:description, :keywords)
cache haml(:page)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment