Archives group by year after 2 years

This commit is contained in:
Joe Sak 2011-01-10 16:11:53 -06:00
parent b29b7df9ac
commit bf74b32f3f
4 changed files with 39 additions and 10 deletions

View file

@ -45,12 +45,23 @@ class Blog::PostsController < BlogController
end
def archive
if params[:month].present?
date = "#{params[:month]}/#{params[:year]}"
@archive_date = Time.parse(date)
@date_title = @archive_date.strftime('%B %Y')
@blog_posts = BlogPost.live.by_archive(@archive_date).paginate({
:page => params[:page],
:per_page => RefinerySetting.find_or_set(:blog_posts_per_page, 10)
})
else
date = "01/#{params[:year]}"
@archive_date = Time.parse(date)
@date_title = @archive_date.strftime('%Y')
@blog_posts = BlogPost.live.by_year(@archive_date).paginate({
:page => params[:page],
:per_page => RefinerySetting.find_or_set(:blog_posts_per_page, 10)
})
end
respond_with (@blog_posts)
end

View file

@ -6,18 +6,32 @@ module BlogPostsHelper
html << t('blog.shared.archives')
html << '</h2><nav><ul>'
links = []
super_old_links = []
posts.each do |e|
if e.published_at >= Time.now.end_of_year.advance(:years => -3)
links << e.published_at.strftime('%m/%Y')
else
super_old_links << e.published_at.strftime('01/%Y')
end
end
links.uniq!
super_old_links.uniq!
links.each do |l|
year = l.split('/')[1]
month = l.split('/')[0]
count = BlogPost.by_archive(Time.parse(l)).size
count = NewsItem.by_archive(Time.parse(l)).size
text = t("date.month_names")[month.to_i] + " #{year} (#{count})"
html << "<li>"
html << link_to(text, archive_blog_posts_path(:year => year, :month => month))
html << link_to(text, archive_news_items_path(:year => year, :month => month))
html << "</li>"
end
super_old_links.each do |l|
year = l.split('/')[1]
count = NewsItem.by_year(Time.parse(l)).size
text = "#{year} (#{count})"
html << "<li>"
html << link_to(text, archive_news_items_path(:year => year))
html << "</li>"
end
html << '</ul></nav></section>'

View file

@ -14,6 +14,10 @@ class BlogPost < ActiveRecord::Base
where(['published_at between ? and ?', archive_date.beginning_of_month, archive_date.end_of_month]).order("published_at DESC")
}
scope :by_year, lambda { |archive_year|
where(['published_at between ? and ?', archive_year.beginning_of_year, archive_year.end_of_year]).order("published_at DESC")
}
scope :all_previous, where(['published_at <= ?', Time.now.beginning_of_month]).order("published_at DESC")
scope :live, lambda { where( "published_at < ? and draft = ?", Time.now, false).order("published_at DESC") }

View file

@ -5,7 +5,7 @@ Refinery::Application.routes.draw do
match ':id', :to => 'posts#show', :as => 'blog_post'
match 'categories/:id', :to => 'categories#show', :as => 'blog_category'
match ':id/comments', :to => 'posts#comment', :as => 'blog_post_blog_comments'
get 'archive/:year/:month', :to => 'posts#archive', :as => 'archive_blog_posts'
get 'archive/:year(/:month)', :to => 'posts#archive', :as => 'archive_blog_posts'
end
scope(:path => 'refinery', :as => 'admin', :module => 'admin') do