diff --git a/activesupport/lib/active_support/core_ext/date/conversions.rb b/activesupport/lib/active_support/core_ext/date/conversions.rb index e6ddf9fade6f0822b167ce9601764c2bc695c28e..048a260ada3e9df8e85902748e8c0e45ffd25d2e 100644 --- a/activesupport/lib/active_support/core_ext/date/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date/conversions.rb @@ -3,6 +3,11 @@ module CoreExtensions #:nodoc: module Date #:nodoc: # Getting dates in different convenient string representations and other objects module Conversions + DATE_FORMATS = { + :short => "%e %b", + :long => "%B %e, %Y" + } + def self.append_features(klass) #:nodoc: super klass.send(:alias_method, :to_default_s, :to_s) @@ -10,11 +15,7 @@ def self.append_features(klass) #:nodoc: end def to_formatted_s(format = :default) - case format - when :default then to_default_s - when :short then strftime("%e %b").strip - when :long then strftime("%B %e, %Y").strip - end + DATE_FORMATS[format] ? strftime(DATE_FORMATS[format]).strip : to_default_s end # To be able to keep Dates and Times interchangeable on conversions diff --git a/activesupport/lib/active_support/core_ext/time/conversions.rb b/activesupport/lib/active_support/core_ext/time/conversions.rb index 689da6db521aa0a4aef77dbf6df35cee19f5bb8b..fadfb6b0df8577161b8896c2b385882f1d25db29 100644 --- a/activesupport/lib/active_support/core_ext/time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/time/conversions.rb @@ -5,6 +5,12 @@ module CoreExtensions #:nodoc: module Time #:nodoc: # Getting times in different convenient string representations and other objects module Conversions + DATE_FORMATS = { + :db => "%Y-%m-%d %H:%M:%S", + :short => "%e %b %H:%M", + :long => "%B %e, %Y %H:%M" + } + def self.append_features(klass) super klass.send(:alias_method, :to_default_s, :to_s) @@ -12,12 +18,7 @@ def self.append_features(klass) end def to_formatted_s(format = :default) - case format - when :default then to_default_s - when :db then strftime("%Y-%m-%d %H:%M:%S") - when :short then strftime("%e %b %H:%M").strip - when :long then strftime("%B %e, %Y %H:%M").strip - end + DATE_FORMATS[format] ? strftime(DATE_FORMATS[format]).strip : to_default_s end def to_date