Skip to content

Commit

Permalink
Added a new archive page
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Redpath committed Jan 22, 2009
1 parent 6eb0100 commit 3817dcd
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/marley.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

# FIXME : There must be a clean way to do this :)
req_or_load = (Sinatra.env == :development) ? :load : :require
%w{configuration.rb post.rb comment.rb}.each do |f|
%w{configuration.rb post.rb comment.rb archive.rb}.each do |f|
send(req_or_load, File.join(File.dirname(__FILE__), 'marley', f) )
end

Expand Down Expand Up @@ -98,6 +98,13 @@ def config
end
end

get "/archive" do
@posts = Marley::Repository.default.all.sort
@posts_index = Marley::Archive.new(@posts).posts_indexed_by_month_and_year
@page_title = "Archives of #{marley_config.blog.title}"
erb :archive
end

get '/feed' do
@posts = Marley::Repository.default.all.sort.first(10)
last_modified( @posts.first.updated_on )
Expand Down
16 changes: 16 additions & 0 deletions app/marley/archive.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Marley

class Archive
def initialize(posts)
@posts = posts
end

def posts_indexed_by_month_and_year
@posts.inject({}) do |index, post|
(index[post.month_and_year] ||= []) << post
index
end
end
end

end
4 changes: 4 additions & 0 deletions app/marley/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ def ==(other_post)
def <=>(other_post)
other_post.published_on <=> published_on
end

def month_and_year
[published_on.month, published_on.year]
end

private

Expand Down
27 changes: 27 additions & 0 deletions app/test/archive_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require File.join(File.dirname(__FILE__), 'test_helper')
require 'archive'
require 'post'

class ArchiveTest < Test::Unit::TestCase

context "An archive of posts" do
setup do
@post_one = Marley::Post.new('post-one', 'body', {:published_on => '05 Jan 2009'})
@post_two = Marley::Post.new('post-two', 'body', {:published_on => '01 Jan 2009'})
@post_three = Marley::Post.new('post-thr', 'body', {:published_on => '14 Dec 2008'})
@post_four = Marley::Post.new('post-for', 'body', {:published_on => '22 Nov 2008'})

@archive = Marley::Archive.new([@post_one, @post_two, @post_three, @post_four])
end

should "return posts as a hash indexed by month and year" do
expected = {
[1, 2009] => [@post_one, @post_two],
[12, 2008] => [@post_three],
[11, 2008] => [@post_four]
}
assert_equal expected, @archive.posts_indexed_by_month_and_year
end
end

end
9 changes: 9 additions & 0 deletions themes/lukeredpath/archive.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<% @posts.map { |post| post.month_and_year }.uniq.each do |(month, year)| %>
<h2><%= Date::MONTHNAMES[month] %> <%= year %></h2>

<ul>
<% @posts_index[[month, year]].each do |post| %>
<li><a href="<%= permalink(post) %>"><%= post.title %></a></li>
<% end %>
</ul>
<% end %>

0 comments on commit 3817dcd

Please sign in to comment.