Skip to content

Commit

Permalink
Factor out a time decorator to handle the update time display
Browse files Browse the repository at this point in the history
  • Loading branch information
carols10cents committed May 15, 2012
1 parent ba38820 commit 437fad0
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 10 deletions.
45 changes: 45 additions & 0 deletions app/decorators/time_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class TimeDecorator < ApplicationDecorator
decorates :update

def abbr
h.content_tag "abbr",
{:class => "timeago",
:title => iso8601_time} do
alps_time_span
end
end

def permalink
h.content_tag "time",
{:class => "published",
:pubdate => "pubdate",
:datetime => iso8601_time} do
h.content_tag "a", {:class => "timeago",
:href => update.url,
:rel => "bookmark message",
:title => iso8601_time} do
alps_time_span
end
end
end

private

def alps_time_span
h.content_tag "span", {:class => "date-time"} do
alps_time
end
end

def utc_time
update.created_at.getutc
end

def iso8601_time
utc_time.iso8601
end

def alps_time
utc_time.strftime("%FT%T")
end
end
8 changes: 2 additions & 6 deletions app/views/updates/_detail.haml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@
!= update.to_html
.update-date
- if reply
%a.timeago{:href => "/updates/#{update.id}", :title => update.created_at.getutc.iso8601}
%span.date-time
= update.created_at.getutc.strftime("%FT%T")
= TimeDecorator.decorate(update).permalink
- else
%abbr.timeago{:title => update.created_at.getutc.iso8601}
%span.date-time
= update.created_at.getutc.strftime("%FT%T")
= TimeDecorator.decorate(update).abbr

.actions
-unless current_user.nil? or (current_user.author.id == update.author.id)
Expand Down
5 changes: 1 addition & 4 deletions app/views/updates/_list.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
!= update.to_html

.info
%time.published{:pubdate => "pubdate", :datetime => update.created_at.getutc.iso8601}
%a.timeago{:href => update.url, :rel => "bookmark message", :title => update.created_at.getutc.iso8601}
%span.date-time
= update.created_at.getutc.strftime("%FT%T")
= TimeDecorator.decorate(update).permalink

- if !update.referral.nil?
%span.in-reply
Expand Down
63 changes: 63 additions & 0 deletions test/decorators/time_decorator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require_relative '../test_helper'

describe TimeDecorator do
include TestHelper

before do
@update = TimeDecorator.decorate(
Fabricate(:update,
:created_at => Time.parse("2012-07-04 22:44:11 EDT")
)
)
end

describe "#abbr" do
before do
@abbr_html = Nokogiri::HTML.parse(@update.abbr)
end

it "has an abbr element with the correct attributes" do
abbr = @abbr_html.at_xpath("//abbr")
assert abbr
abbr["class"].must_equal("timeago")
abbr["title"].must_equal("2012-07-05T02:44:11Z")
end

it "has the ALPS date-time element in the correct format" do
span = @abbr_html.at_xpath("//abbr/span")
assert span
span["class"].must_equal("date-time")
span.text.must_equal("2012-07-05T02:44:11")
end
end

describe "#permalink" do
before do
@permalink_html = Nokogiri::HTML.parse(@update.permalink)
end

it "has the time element with the correct attributes" do
time_element = @permalink_html.at_xpath("//time")
assert time_element
time_element["class"].must_equal("published")
time_element["pubdate"].must_equal("pubdate")
time_element["datetime"].must_equal("2012-07-05T02:44:11Z")
end

it "has a link element for the permalink" do
link = @permalink_html.at_xpath("//time/a")
assert link
link["class"].must_equal("timeago")
link["href"].must_equal("/updates/#{@update.id}")
link["rel"].must_equal("bookmark message")
link["title"].must_equal("2012-07-05T02:44:11Z")
end

it "has the ALPS date-time element in the correct format" do
span = @permalink_html.at_xpath("//time/a/span")
assert span
span["class"].must_equal("date-time")
span.text.must_equal("2012-07-05T02:44:11")
end
end
end

0 comments on commit 437fad0

Please sign in to comment.