diff options
author | Philip Arndt <parndt@gmail.com> | 2012-02-22 23:43:01 -0800 |
---|---|---|
committer | Philip Arndt <parndt@gmail.com> | 2012-02-22 23:43:01 -0800 |
commit | f82d2c96e49cdb3c649358b5fd3fbf3f52389660 (patch) | |
tree | 1a58fb580bee0466c9135320f51ec01619e6417b /app | |
parent | aa729edc8495598693d758ad77608293e8be43c9 (diff) | |
parent | 2bcb7c85513c24d579c1134a47a5aeddf5968a9a (diff) | |
download | refinerycms-blog-f82d2c96e49cdb3c649358b5fd3fbf3f52389660.tar.gz refinerycms-blog-f82d2c96e49cdb3c649358b5fd3fbf3f52389660.tar.bz2 refinerycms-blog-f82d2c96e49cdb3c649358b5fd3fbf3f52389660.zip |
Merge pull request #195 from substantial/refactor_archive_widget
Refactor archive widget
Diffstat (limited to 'app')
-rw-r--r-- | app/helpers/refinery/blog/posts_helper.rb | 65 | ||||
-rw-r--r-- | app/models/refinery/blog/post.rb | 12 | ||||
-rw-r--r-- | app/views/refinery/blog/widgets/_blog_archive.html.erb | 6 |
3 files changed, 52 insertions, 31 deletions
diff --git a/app/helpers/refinery/blog/posts_helper.rb b/app/helpers/refinery/blog/posts_helper.rb index 4847ab2..dfb742a 100644 --- a/app/helpers/refinery/blog/posts_helper.rb +++ b/app/helpers/refinery/blog/posts_helper.rb @@ -1,14 +1,6 @@ module Refinery module Blog module PostsHelper - def blog_archive_widget - posts = Refinery::Blog::Post.select('published_at').all_previous - return nil if posts.blank? - - render :partial => "/refinery/blog/widgets/blog_archive", :locals => { :posts => posts } - end - alias_method :blog_archive_list, :blog_archive_widget - def next_or_previous?(post) post.next.present? or post.prev.present? end @@ -28,22 +20,51 @@ module Refinery end end - def archive_link(post) - if post.published_at >= Time.now.end_of_year.advance(:years => -3) - post_date = post.published_at.strftime('%m/%Y') - year = post_date.split('/')[1] - month = post_date.split('/')[0] - count = Blog::Post.by_archive(Time.parse(post_date)).size - text = t("date.month_names")[month.to_i] + " #{year} (#{count})" + def blog_archive_widget(dates=blog_archive_dates) + ArchiveWidget.new(dates, self).display + end - link_to(text, refinery.blog_archive_posts_path(:year => year, :month => month)) - else - post_date = post.published_at.strftime('01/%Y') - year = post_date.split('/')[1] - count = Refinery::Blog::Post.by_year(Time.parse(post_date)).size - text = "#{year} (#{count})" + def blog_archive_dates(cutoff=Time.now.beginning_of_month) + Refinery::Blog::Post.published_dates_older_than(cutoff) + end + + class ArchiveWidget + delegate :t, :link_to, :refinery, :render, :to => :view_context + attr_reader :view_context + + def initialize(dates, view_context, cutoff=3.years.ago.end_of_year) + @recent_dates, @old_dates = dates.sort_by {|date| -date.to_i }. + partition {|date| date > cutoff } + + @view_context = view_context + end + + def recent_links + @recent_dates.group_by {|date| [date.year, date.month] }. + map {|(year, month), dates| recent_link(year, month, dates.count) } + end + + def recent_link(year, month, count) + link_to "#{t("date.month_names")[month]} #{year} (#{count})", + refinery.blog_archive_posts_path(:year => year, :month => month) + end + + def old_links + @old_dates.group_by {|date| date.year }. + map {|year, dates| old_link(year, dates.size) } + end + + def old_link(year, count) + link_to "#{year} (#{count})", refinery.blog_archive_posts_path(:year => year) + end + + def links + recent_links + old_links + end - link_to(text, refinery.blog_archive_posts_path(:year => year)) + def display + return "" if links.empty? + render "refinery/blog/widgets/blog_archive", :links => links end end end diff --git a/app/models/refinery/blog/post.rb b/app/models/refinery/blog/post.rb index 522e000..ddfc3cc 100644 --- a/app/models/refinery/blog/post.rb +++ b/app/models/refinery/blog/post.rb @@ -63,16 +63,16 @@ module Refinery end class << self - def by_archive(archive_date) - where(['published_at between ? and ?', archive_date.beginning_of_month, archive_date.end_of_month]) + def by_archive(date) + where(:published_at => date.beginning_of_month..date.end_of_month) end - def by_year(archive_year) - where(['published_at between ? and ?', archive_year.beginning_of_year, archive_year.end_of_year]) + def by_year(date) + where(:published_at => date.beginning_of_year..date.end_of_year) end - def all_previous - where(['published_at <= ?', Time.now.beginning_of_month]) + def published_dates_older_than(date) + where("published_at <= ?", date).map(&:published_at) end def live diff --git a/app/views/refinery/blog/widgets/_blog_archive.html.erb b/app/views/refinery/blog/widgets/_blog_archive.html.erb index 7679a5b..b3522c6 100644 --- a/app/views/refinery/blog/widgets/_blog_archive.html.erb +++ b/app/views/refinery/blog/widgets/_blog_archive.html.erb @@ -2,9 +2,9 @@ <h2><%= t('archives', :scope => 'refinery.blog.shared') %></h2> <nav> <ul> - <% posts.each do |post| %> - <li><%= archive_link(post) %></li> + <% links.each do |link| %> + <li><%= link %></li> <% end %> </ul> </nav> -</section>
\ No newline at end of file +</section> |