diff --git a/app/decorators/time_decorator.rb b/app/decorators/time_decorator.rb new file mode 100644 index 00000000..7314cda6 --- /dev/null +++ b/app/decorators/time_decorator.rb @@ -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 \ No newline at end of file diff --git a/app/views/updates/_detail.haml b/app/views/updates/_detail.haml index ce74ab9e..4ada26c7 100644 --- a/app/views/updates/_detail.haml +++ b/app/views/updates/_detail.haml @@ -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) diff --git a/app/views/updates/_list.html.haml b/app/views/updates/_list.html.haml index 05037a8a..f237a51c 100644 --- a/app/views/updates/_list.html.haml +++ b/app/views/updates/_list.html.haml @@ -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 diff --git a/test/decorators/time_decorator_test.rb b/test/decorators/time_decorator_test.rb new file mode 100644 index 00000000..1ce31ebf --- /dev/null +++ b/test/decorators/time_decorator_test.rb @@ -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