From ad4f1fd630dbeeb66b9cde0979518b4af96201ad Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 25 May 2007 21:43:19 +0000 Subject: Date, Time, and DateTime support formatting blocks in addition to strftime strings. Introduce :long_ordinal format, e.g. 'February 21st, 2005'. Closes #8191. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6844 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/active_support/core_ext/date/conversions.rb | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'activesupport/lib/active_support/core_ext/date') diff --git a/activesupport/lib/active_support/core_ext/date/conversions.rb b/activesupport/lib/active_support/core_ext/date/conversions.rb index 8f48ccacde..01dd8461e4 100644 --- a/activesupport/lib/active_support/core_ext/date/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date/conversions.rb @@ -4,9 +4,10 @@ module ActiveSupport #:nodoc: # Getting dates in different convenient string representations and other objects module Conversions DATE_FORMATS = { - :short => "%e %b", - :long => "%B %e, %Y", - :db => "%Y-%m-%d" + :short => "%e %b", + :long => "%B %e, %Y", + :db => "%Y-%m-%d", + :long_ordinal => lambda { |date| date.strftime("%B #{date.day.ordinalize}, %Y") } # => "April 25th, 2007" } def self.included(klass) #:nodoc: @@ -15,7 +16,15 @@ module ActiveSupport #:nodoc: end def to_formatted_s(format = :default) - DATE_FORMATS[format] ? strftime(DATE_FORMATS[format]).strip : to_default_s + if formatter = DATE_FORMATS[format] + if formatter.respond_to?(:call) + formatter.call(self).to_s + else + strftime(formatter).strip + end + else + to_default_s + end end # To be able to keep Dates and Times interchangeable on conversions -- cgit v1.2.3