aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPete Higgins <pete@peterhiggins.org>2012-02-10 00:28:29 -0800
committerPete Higgins <pete@peterhiggins.org>2012-02-10 01:05:51 -0800
commit80187eac7d35a9a5ac67019a317e678c4445e44e (patch)
tree32627b15dd6f95edc468b5579a27e4bd246e669c
parent1af631b501bbd8457d114805f729e8af59edeeee (diff)
downloadrefinerycms-blog-80187eac7d35a9a5ac67019a317e678c4445e44e.tar.gz
refinerycms-blog-80187eac7d35a9a5ac67019a317e678c4445e44e.tar.bz2
refinerycms-blog-80187eac7d35a9a5ac67019a317e678c4445e44e.zip
Refactor blog archive widget.
-rw-r--r--app/helpers/refinery/blog/posts_helper.rb65
-rw-r--r--app/views/refinery/blog/widgets/_blog_archive.html.erb6
-rw-r--r--spec/helpers/refinery/blog/posts_helper_spec.rb71
3 files changed, 117 insertions, 25 deletions
diff --git a/app/helpers/refinery/blog/posts_helper.rb b/app/helpers/refinery/blog/posts_helper.rb
index 4847ab2..4653617 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
+ Refinery::Blog::Post.select('published_at').all_previous.map(&:published_at)
+ 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/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>
diff --git a/spec/helpers/refinery/blog/posts_helper_spec.rb b/spec/helpers/refinery/blog/posts_helper_spec.rb
new file mode 100644
index 0000000..8d74b57
--- /dev/null
+++ b/spec/helpers/refinery/blog/posts_helper_spec.rb
@@ -0,0 +1,71 @@
+require 'spec_helper'
+
+module Refinery
+ module Blog
+ describe PostsHelper do
+ describe "#blog_archive_widget" do
+ let(:html) { helper.blog_archive_widget(dates) }
+ let(:links) { Capybara.string(html).find("#blog_archive_widget ul") }
+
+ context "without no archive dates" do
+ let(:dates) { [] }
+
+ it "does not display anything" do
+ html.should be_blank
+ end
+ end
+
+ context "with archive dates" do
+ let(:recent_post) { 2.months.ago }
+ let(:old_post) { 4.years.ago }
+
+ let(:dates) do
+ [old_post, recent_post].map do |date|
+ [date, date.beginning_of_month, date.end_of_month]
+ end.flatten
+ end
+
+ it "has a link for the month of dates not older than one year" do
+ month = Date::MONTHNAMES[recent_post.month]
+ year = recent_post.year
+
+ links.should have_link("#{month} #{year} (3)")
+ end
+
+ it "has a link for the year of dates older than one year" do
+ year = old_post.year
+
+ links.should have_link("#{year} (3)")
+ end
+
+ it "sorts recent links before old links" do
+ links.find("li:first").should have_content(recent_post.year.to_s)
+ links.find("li:last").should have_content(old_post.year.to_s)
+ end
+ end
+
+ context "with multiple recent dates" do
+ let(:dates) { [3.months.ago, 2.months.ago] }
+
+ it "sorts by the more recent date" do
+ first, second = dates.map {|p| Date::MONTHNAMES[p.month] }
+
+ links.find("li:first").should have_content(second)
+ links.find("li:last").should have_content(first)
+ end
+ end
+
+ context "with multiple old dates" do
+ let(:dates) { [5.years.ago, 4.years.ago] }
+
+ it "sorts by the more recent date" do
+ first, second = dates.map {|p| p.year.to_s }
+
+ links.find("li:first").should have_content(second)
+ links.find("li:last").should have_content(first)
+ end
+ end
+ end
+ end
+ end
+end