forked from hotsh/rstat.us
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out a time decorator to handle the update time display
- Loading branch information
1 parent
ba38820
commit 437fad0
Showing
4 changed files
with
111 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |