Skip to content

Commit

Permalink
Merge pull request #322 from SamWhited/rfc1123-date
Browse files Browse the repository at this point in the history
Multiple Radius tags for rendering dates is redundant
  • Loading branch information
saturnflyer committed Aug 27, 2012
2 parents f927223 + dac90ef commit 47097fa
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
9 changes: 8 additions & 1 deletion app/models/deprecated_tags.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -30,5 +30,12 @@ module DeprecatedTags
keywords keywords
end end
end end

deprecated_tag "rfc1123_date", :deadline => '2.0' do |tag|
page = tag.locals.page
if date = page.published_at || page.created_at
CGI.rfc1123_date(date.to_time)
end
end


end end
32 changes: 12 additions & 20 deletions app/models/standard_tags.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -747,10 +747,11 @@ class RequiredAttributeError < StandardError; end


desc %{ desc %{
Renders the date based on the current page (by default when it was published or created). Renders the date based on the current page (by default when it was published or created).
The format attribute uses the same formating codes used by the Ruby @strftime@ function. By The format attribute uses the same formating codes used by the Ruby @strftime@ function.
default it's set to @%A, %B %d, %Y@. The @for@ attribute selects which date to render. Valid By default it's set to @%A, %B %d, %Y@. You may also use the string @rfc1123@ as a shortcut
options are @published_at@, @created_at@, @updated_at@, and @now@. @now@ will render the for @%a, %d %b %Y %H:%M:%S GMT@ (non-localized). The @for@ attribute selects which date to
current date/time, regardless of the page. render. Valid options are @published_at@, @created_at@, @updated_at@, and @now@. @now@ will
render the current date/time, regardless of the page.
*Usage:* *Usage:*
Expand All @@ -772,9 +773,14 @@ class RequiredAttributeError < StandardError; end
else else
page.published_at || page.created_at page.published_at || page.created_at
end end
@i18n_date_format_keys ||= (I18n.config.backend.send(:translations)[I18n.locale][:date][:formats].keys rescue []) case format
when 'rfc1123'
CGI.rfc1123_date(date.to_time)
else
@i18n_date_format_keys ||= (I18n.config.backend.send(:translations)[I18n.locale][:date][:formats].keys rescue [])
format = @i18n_date_format_keys.include?(format.to_sym) ? format.to_sym : format format = @i18n_date_format_keys.include?(format.to_sym) ? format.to_sym : format
I18n.l date, :format => format I18n.l date, :format => format
end
end end


desc %{ desc %{
Expand Down Expand Up @@ -896,20 +902,6 @@ class RequiredAttributeError < StandardError; end
CGI.escapeHTML(tag.expand) CGI.escapeHTML(tag.expand)
end end


desc %{
Outputs the published date using the format mandated by RFC 1123. (Ideal for RSS feeds.)
*Usage:*
<pre><code><r:rfc1123_date /></code></pre>
}
tag "rfc1123_date" do |tag|
page = tag.locals.page
if date = page.published_at || page.created_at
CGI.rfc1123_date(date.to_time)
end
end

desc %{ desc %{
Renders a list of links specified in the @paths@ attribute according to three Renders a list of links specified in the @paths@ attribute according to three
states: states:
Expand Down
8 changes: 7 additions & 1 deletion spec/models/deprecated_tags_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@
end end
end end
end end

describe "<r:rfc1123_date>" do
it 'should render an RFC1123-compatible date' do
page(:dated).should render('<r:rfc1123_date />').as('Wed, 11 Jan 2006 00:00:00 GMT')
end
end


describe "<r:navigation>" do describe "<r:navigation>" do
it "should render with deprecated url attribute" do it "should render with deprecated url attribute" do
Expand All @@ -121,4 +127,4 @@
end end
end end
end end
end end
8 changes: 4 additions & 4 deletions spec/models/standard_tags_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -613,6 +613,10 @@
page.should render('<r:date format="%d %b %Y" />').as('11 Jan 2006') page.should render('<r:date format="%d %b %Y" />').as('11 Jan 2006')
end end


it "should render an RFC1123-compatible date if the 'format' attribute is set to 'rfc1123'" do
page.should render('<r:date format="rfc1123" />').as('Wed, 11 Jan 2006 00:00:00 GMT')
end

it "should format the published date according to localized format" do it "should format the published date according to localized format" do
page.should render('<r:date format="short" />').as(I18n.l(page.published_at, :format => :short)) page.should render('<r:date format="short" />').as(I18n.l(page.published_at, :format => :short))
end end
Expand Down Expand Up @@ -822,10 +826,6 @@
page.should render('<r:escape_html><strong>a bold move</strong></r:escape_html>').as('&lt;strong&gt;a bold move&lt;/strong&gt;') page.should render('<r:escape_html><strong>a bold move</strong></r:escape_html>').as('&lt;strong&gt;a bold move&lt;/strong&gt;')
end end


it '<r:rfc1123_date> should render an RFC1123-compatible date' do
page(:dated).should render('<r:rfc1123_date />').as('Wed, 11 Jan 2006 00:00:00 GMT')
end

describe "<r:breadcrumbs>" do describe "<r:breadcrumbs>" do
it "should render a series of breadcrumb links separated by &gt;" do it "should render a series of breadcrumb links separated by &gt;" do
expected = %{<a href="/">Home</a> &gt; <a href="/parent/">Parent</a> &gt; <a href="/parent/child/">Child</a> &gt; <a href="/parent/child/grandchild/">Grandchild</a> &gt; Great Grandchild} expected = %{<a href="/">Home</a> &gt; <a href="/parent/">Parent</a> &gt; <a href="/parent/child/">Child</a> &gt; <a href="/parent/child/grandchild/">Grandchild</a> &gt; Great Grandchild}
Expand Down

0 comments on commit 47097fa

Please sign in to comment.