Skip to content

Instantly share code, notes, and snippets.

@SamWhited
Created February 28, 2012 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SamWhited/1935006 to your computer and use it in GitHub Desktop.
Save SamWhited/1935006 to your computer and use it in GitHub Desktop.
Two options for fixing the redundant date tags in Radiant CMS
diff --git a/app/models/standard_tags.rb b/app/models/standard_tags.rb
index a71e76b..b7b0918 100644
--- a/app/models/standard_tags.rb
+++ b/app/models/standard_tags.rb
@@ -747,10 +747,10 @@ module StandardTags
desc %{
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
- default it's set to @%A, %B %d, %Y@. The @for@ attribute selects which date to render. Valid
- options are @published_at@, @created_at@, @updated_at@, and @now@. @now@ will render the
- current date/time, regardless of the page.
+ The format attribute uses the same formating codes used by the Ruby @strftime@ function, or
+ the string 'rfc1123'. By default it's set to @%A, %B %d, %Y@. The @for@ attribute selects
+ which date to render. Valid options are @published_at@, @created_at@, @updated_at@, and @now@.
+ @now@ will render the current date/time, regardless of the page.
*Usage:*
@@ -772,9 +772,13 @@ module StandardTags
else
page.published_at || page.created_at
end
- @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
- I18n.l date, :format => format
+ if (format == '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
+ I18n.l date, :format => format
+ end
end
desc %{
@@ -973,16 +977,29 @@ module StandardTags
desc %{
Outputs the published date using the format mandated by RFC 1123. (Ideal for RSS feeds.)
+ The @for@ attribute selects which date to render, and takes the same options as @<r:date />@.
*Usage:*
- <pre><code><r:rfc1123_date /></code></pre>
+ <pre><code><r:rfc1123_date [for="published_at"] /></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)
+ time_attr = tag.attr['for']
+ date = if time_attr
+ case
+ when time_attr == 'now'
+ Time.zone.now
+ when Page.date_column_names.include?(time_attr)
+ page[time_attr]
+ else
+ raise TagError, "Invalid value for 'for' attribute."
+ end
+ else
+ page.published_at || page.created_at
end
+
+ CGI.rfc1123_date(date.to_time)
end
desc %{
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment