Skip to content

Commit

Permalink
Finish the monthly archive view
Browse files Browse the repository at this point in the history
  • Loading branch information
tualatrix committed Jan 18, 2012
1 parent 227d802 commit 63259e3
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 3 deletions.
8 changes: 8 additions & 0 deletions app/controllers/posts_controller.rb
Expand Up @@ -89,4 +89,12 @@ def destroy
def page
@page = Post.find_by_title(params[:title])
end

def date_archives
year = params[:year]
month = params[:month]
date = DateTime.new(year.to_i, month.to_i)
@title = "Monthly Archives: #{date.strftime "%b %Y"}"
@posts = Post.where(:date => (date.at_beginning_of_month)..(date.at_end_of_month))
end
end
2 changes: 1 addition & 1 deletion app/helpers/categories_helper.rb
Expand Up @@ -3,7 +3,7 @@ def get_categories
html_text = []

Category.all.each do |category|
html_text << content_tag(:li, link_to(category.title, category))
html_text << content_tag(:li, link_to(category.title, category))
end
html_text.join("\n").html_safe
end
Expand Down
14 changes: 14 additions & 0 deletions app/helpers/posts_helper.rb
Expand Up @@ -7,4 +7,18 @@ def recent_posts
end
html_text.join("\n").html_safe
end

def get_archives
date_list = []
html_text = []

Post.select('DISTINCT(date)').each do |post|
month = post.date.strftime('%b %Y')
if date_list.index(month) == nil
date_list << month
html_text << content_tag(:li, link_to(month, post.date.strftime('/archives/%Y/%m')))
end
end
html_text.join("\n").html_safe
end
end
11 changes: 11 additions & 0 deletions app/views/posts/date_archives.html
@@ -0,0 +1,11 @@
<% content_for :title do %><%= @title %><% end %>

<% content_for :content do %>
<h2 class="pagetitle"><%= @title %></h2>

<% @posts.each do |post| %>
<div class="post" id="post-<%= post.id %>">
<%= render :partial => 'posts/title', :locals => { :post => post } %>
</div>
<% end %>
<% end %>
6 changes: 4 additions & 2 deletions app/views/shared/_sidebar.html.erb
Expand Up @@ -35,8 +35,10 @@
</li>

<li>
<h2>Archive</h2>
{% get_archive %}
<h2>Archives</h2>
<ul>
<%= get_archives %>
</ul>
</li>

<li>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -54,6 +54,7 @@
# just remember to delete public/index.html.
root :to => 'posts#index'
match ':title', :controller => 'posts', :action => 'page'
match 'archives/:year/:month', :controller => 'posts', :action => 'date_archives'

# See how all your routes lay out with "rake routes"

Expand Down

0 comments on commit 63259e3

Please sign in to comment.