Skip to content
This repository has been archived by the owner on Dec 5, 2018. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix archive links with a much better solution
  • Loading branch information
darkhelmet committed Jan 8, 2010
1 parent 8d81c50 commit f2eb11f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
7 changes: 7 additions & 0 deletions lib/archive_date.rb
@@ -0,0 +1,7 @@
require 'active_support'

class ArchiveDate < Date
def succ
self + 1.month
end
end
26 changes: 6 additions & 20 deletions lib/blog_helper.rb
Expand Up @@ -23,6 +23,7 @@
# require 'newrelic_rpm'
require 'sanitize'
require 'social'
require 'archive_date'

%w(authorization named_routes).each do |ext|
require "sinatra/#{ext}"
Expand Down Expand Up @@ -187,30 +188,15 @@ def category_link(cat)
partial("%a{ :href => '/category/#{cat.url_encode}' } #{h(cat).capitalize}")
end

def next_month(year, month)
if 12 == month
year += 1
month = 1
return year, month
else
return year, month + 1
end
end

def monthly_archive_links
newest = Post.published.first
return Array.new if newest.nil?
oldest = Post.published.last
year = oldest.published_on_local.year
month = oldest.published_on_local.month

links = Array.new
while year <= newest.published_on_local.year && month <= newest.published_on_local.month
date = Date.new(year,month,1)
links << partial("%a{ :href => '/#{date.strftime('%Y/%m')}' } #{date.strftime('%B %Y')}")
year, month = next_month(year,month)
newest = newest.published_on_local
oldest = Post.published.last.published_on_local

(ArchiveDate.new(oldest.year, oldest.month, 1)..ArchiveDate.new(newest.year, newest.month, 1)).map do |date|
partial("%a{ :href => '/#{date.strftime('%Y/%m')}' } #{date.strftime('%B %Y')}")
end
links
end

# TODO: Refactor to support :collection
Expand Down

0 comments on commit f2eb11f

Please sign in to comment.