diff options
author | Joe Sak <joe@joesak.com> | 2011-01-10 16:11:53 -0600 |
---|---|---|
committer | Joe Sak <joe@joesak.com> | 2011-01-10 16:11:53 -0600 |
commit | bf74b32f3fbdd50957bdf308b79846eeda7ee1fa (patch) | |
tree | ba836797c939e54c60bf92c30323e2e7c666a8dc | |
parent | b29b7df9acd67e8d2059766db4a743bc98d3effd (diff) | |
download | refinerycms-blog-bf74b32f3fbdd50957bdf308b79846eeda7ee1fa.tar.gz refinerycms-blog-bf74b32f3fbdd50957bdf308b79846eeda7ee1fa.tar.bz2 refinerycms-blog-bf74b32f3fbdd50957bdf308b79846eeda7ee1fa.zip |
Archives group by year after 2 years
-rw-r--r-- | app/controllers/blog/posts_controller.rb | 23 | ||||
-rw-r--r-- | app/helpers/blog_posts_helper.rb | 20 | ||||
-rw-r--r-- | app/models/blog_post.rb | 4 | ||||
-rw-r--r-- | config/routes.rb | 2 |
4 files changed, 39 insertions, 10 deletions
diff --git a/app/controllers/blog/posts_controller.rb b/app/controllers/blog/posts_controller.rb index f7b6f3c..576073e 100644 --- a/app/controllers/blog/posts_controller.rb +++ b/app/controllers/blog/posts_controller.rb @@ -45,12 +45,23 @@ class Blog::PostsController < BlogController end def archive - date = "#{params[:month]}/#{params[:year]}" - @archive_date = Time.parse(date) - @blog_posts = BlogPost.live.by_archive(@archive_date).paginate({ - :page => params[:page], - :per_page => RefinerySetting.find_or_set(:blog_posts_per_page, 10) - }) + 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 diff --git a/app/helpers/blog_posts_helper.rb b/app/helpers/blog_posts_helper.rb index 965eb8f..9cc637f 100644 --- a/app/helpers/blog_posts_helper.rb +++ b/app/helpers/blog_posts_helper.rb @@ -6,18 +6,32 @@ module BlogPostsHelper html << t('blog.shared.archives') html << '</h2><nav><ul>' links = [] + super_old_links = [] posts.each do |e| - links << e.published_at.strftime('%m/%Y') + 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>' diff --git a/app/models/blog_post.rb b/app/models/blog_post.rb index 1887a97..60d982a 100644 --- a/app/models/blog_post.rb +++ b/app/models/blog_post.rb @@ -13,6 +13,10 @@ class BlogPost < ActiveRecord::Base scope :by_archive, lambda { |archive_date| 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") diff --git a/config/routes.rb b/config/routes.rb index 20fa1b1..8664468 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 |