diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 5eb5cf1637..634294f9f8 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Readable Date and DateTime#inspect. #8570 [Geoff Buesing] + * Added proper handling of arrays #8537 [hasmanyjosh] Before: diff --git a/activesupport/lib/active_support/core_ext/date/conversions.rb b/activesupport/lib/active_support/core_ext/date/conversions.rb index 25745b0f8b..6e864aee5e 100644 --- a/activesupport/lib/active_support/core_ext/date/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date/conversions.rb @@ -14,6 +14,8 @@ module ActiveSupport #:nodoc: def self.included(klass) #:nodoc: klass.send(:alias_method, :to_default_s, :to_s) klass.send(:alias_method, :to_s, :to_formatted_s) + klass.send(:alias_method, :default_inspect, :inspect) + klass.send(:alias_method, :inspect, :readable_inspect) end def to_formatted_s(format = :default) @@ -27,6 +29,11 @@ module ActiveSupport #:nodoc: to_default_s end end + + # Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005" + def readable_inspect + strftime("%a, %d %b %Y") + end # To be able to keep Times, Dates and DateTimes interchangeable on conversions def to_date diff --git a/activesupport/lib/active_support/core_ext/date_time/conversions.rb b/activesupport/lib/active_support/core_ext/date_time/conversions.rb index 1b78a72eea..ce0bfd8e0a 100644 --- a/activesupport/lib/active_support/core_ext/date_time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date_time/conversions.rb @@ -6,6 +6,8 @@ module ActiveSupport #:nodoc: def self.included(klass) klass.send(:alias_method, :to_datetime_default_s, :to_s) klass.send(:alias_method, :to_s, :to_formatted_s) + klass.send(:alias_method, :default_inspect, :inspect) + klass.send(:alias_method, :inspect, :readable_inspect) end def to_formatted_s(format = :default) @@ -19,6 +21,11 @@ module ActiveSupport #:nodoc: to_datetime_default_s end end + + # Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005 14:30:00 +0000" + def readable_inspect + to_s(:rfc822) + end # Converts self to a Ruby Date object; time portion is discarded def to_date diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 0d68d6b398..04100eec15 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -8,6 +8,11 @@ class DateExtCalculationsTest < Test::Unit::TestCase assert_equal "2005-02-21", Date.new(2005, 2, 21).to_s(:db) assert_equal "21 Feb 2005", Date.new(2005, 2, 21).to_s(:rfc822) end + + def test_readable_inspect + assert_equal "Mon, 21 Feb 2005", Date.new(2005, 2, 21).readable_inspect + assert_equal Date.new(2005, 2, 21).readable_inspect, Date.new(2005, 2, 21).inspect + end def test_to_time assert_equal Time.local(2005, 2, 21), Date.new(2005, 2, 21).to_time diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index b5d80295f0..8c663ee431 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -12,6 +12,12 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase assert_equal "February 21st, 2005 14:30", datetime.to_s(:long_ordinal) end + def test_readable_inspect + datetime = DateTime.new(2005, 2, 21, 14, 30, 0) + assert_equal "Mon, 21 Feb 2005 14:30:00 +0000", datetime.readable_inspect + assert_equal datetime.readable_inspect, datetime.inspect + end + def test_custom_date_format Time::DATE_FORMATS[:custom] = '%Y%m%d%H%M%S' assert_equal '20050221143000', DateTime.new(2005, 2, 21, 14, 30, 0).to_s(:custom)