diff options
author | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-06-20 11:42:21 -0300 |
---|---|---|
committer | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-06-20 11:42:21 -0300 |
commit | 4f700edcc094406155785c693363801d0277792c (patch) | |
tree | bbe0186dc87c3039abd13d59c39f8eee8bba14b1 | |
parent | 265f13495fb326bd2e6a5bc9cae7e297e42893d0 (diff) | |
parent | 2773257ac64c82affae527436b08e6d627853c1d (diff) | |
download | rails-4f700edcc094406155785c693363801d0277792c.tar.gz rails-4f700edcc094406155785c693363801d0277792c.tar.bz2 rails-4f700edcc094406155785c693363801d0277792c.zip |
Merge pull request #6402 from paranoiase/prev-and-next-quarter
Add prev_quarter and next_quarter methods in Time/Date/DateTime calculations.
Similar to prev_month and next_month, it returns the date with the same day
in the previous or next quarter:
t = Time.local(2010, 5, 8) # => Sat, 08 May 2010
t.prev_quarter # => Mon, 08 Feb 2010
t.next_quarter # => Sun, 08 Aug 2010
Closes #6402
-rw-r--r-- | Gemfile | 2 | ||||
-rw-r--r-- | activesupport/CHANGELOG.md | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/date/calculations.rb | 11 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/time/calculations.rb | 11 | ||||
-rw-r--r-- | activesupport/lib/active_support/log_subscriber.rb | 1 | ||||
-rw-r--r-- | activesupport/lib/active_support/testing/isolation.rb | 2 | ||||
-rw-r--r-- | activesupport/test/core_ext/date_ext_test.rb | 12 | ||||
-rw-r--r-- | activesupport/test/core_ext/date_time_ext_test.rb | 12 | ||||
-rw-r--r-- | activesupport/test/core_ext/time_ext_test.rb | 13 | ||||
-rw-r--r-- | guides/source/active_support_core_extensions.textile | 21 | ||||
-rw-r--r-- | railties/test/application/rack/logger_test.rb | 2 | ||||
-rw-r--r-- | railties/test/application/rake/notes_test.rb | 4 |
12 files changed, 91 insertions, 2 deletions
@@ -8,7 +8,7 @@ else gem 'arel' end -gem 'minitest', '~> 3.0.0' +gem 'minitest', '~> 3.1.0' gem 'mocha', '>= 0.11.2' gem 'rack-test', github: "brynary/rack-test" gem 'bcrypt-ruby', '~> 3.0.0' diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 804336da91..00fcfe2001 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,5 +1,7 @@ ## Rails 4.0.0 (unreleased) ## +* Add `Time#prev_quarter' and 'Time#next_quarter' short-hands for months_ago(3) and months_since(3). *SungHee Kang* + * Remove obsolete and unused `require_association` method from dependencies. *fxn* * Add `:instance_accessor` option for `config_accessor`. diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index 8a7eb6bc6b..7fe4161fb4 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -202,6 +202,17 @@ class Date acts_like?(:time) ? result.change(:hour => 0) : result end + # Short-hand for months_ago(3) + def prev_quarter + months_ago(3) + end + alias_method :last_quarter, :prev_quarter + + # Short-hand for months_since(3) + def next_quarter + months_since(3) + end + # Returns a new Date/DateTime representing the start of the month (1st of the month; DateTime objects will have time set to 0:00) def beginning_of_month acts_like?(:time) ? change(:day => 1, :hour => 0) : change(:day => 1) diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 92b8417150..28c8b53b78 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -186,6 +186,17 @@ class Time months_since(1) end + # Short-hand for months_ago(3) + def prev_quarter + months_ago(3) + end + alias_method :last_quarter, :prev_quarter + + # Short-hand for months_since(3) + def next_quarter + months_since(3) + end + # Returns number of days to start of this week, week starts on start_day (default is :monday). def days_to_week_start(start_day = :monday) start_day_number = DAYS_INTO_WEEK[start_day] diff --git a/activesupport/lib/active_support/log_subscriber.rb b/activesupport/lib/active_support/log_subscriber.rb index 720954625a..8c3921c423 100644 --- a/activesupport/lib/active_support/log_subscriber.rb +++ b/activesupport/lib/active_support/log_subscriber.rb @@ -111,6 +111,7 @@ module ActiveSupport finished = Time.now event = @event_stack.pop event.end = finished + event.payload.merge!(payload) method = name.split('.').first begin diff --git a/activesupport/lib/active_support/testing/isolation.rb b/activesupport/lib/active_support/testing/isolation.rb index 43cb16aefa..50ec50ca52 100644 --- a/activesupport/lib/active_support/testing/isolation.rb +++ b/activesupport/lib/active_support/testing/isolation.rb @@ -39,7 +39,7 @@ module ActiveSupport include Enumerable # default to 2 cores - CORES = ENV['TEST_CORES'] || 2 + CORES = (ENV['TEST_CORES'] || 2).to_i def initialize list @list = list diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index e14a137f84..088b74a29a 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -289,6 +289,18 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.new(2004, 2, 29), Date.new(2004, 3, 31).last_month end + def test_next_quarter_on_31st + assert_equal Date.new(2005, 11, 30), Date.new(2005, 8, 31).next_quarter + end + + def test_prev_quarter_on_31st + assert_equal Date.new(2004, 2, 29), Date.new(2004, 5, 31).prev_quarter + end + + def test_last_quarter_on_31st + assert_equal Date.new(2004, 2, 29), Date.new(2004, 5, 31).last_quarter + end + def test_yesterday_constructor assert_equal Date.current - 1, Date.yesterday end diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index 3da0825489..183d58482d 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -271,6 +271,18 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 3, 31).last_month end + def test_next_quarter_on_31st + assert_equal DateTime.civil(2005, 11, 30), DateTime.civil(2005, 8, 31).next_quarter + end + + def test_prev_quarter_on_31st + assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 5, 31).prev_quarter + end + + def test_last_quarter_on_31st + assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 5, 31).last_quarter + end + def test_xmlschema assert_match(/^1880-02-28T15:15:10\+00:?00$/, DateTime.civil(1880, 2, 28, 15, 15, 10).xmlschema) assert_match(/^1980-02-28T15:15:10\+00:?00$/, DateTime.civil(1980, 2, 28, 15, 15, 10).xmlschema) diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 15c04bedf7..d6f285598e 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -906,4 +906,17 @@ class TimeExtMarshalingTest < ActiveSupport::TestCase assert_equal t.to_f, unmarshaled.to_f assert_equal t, unmarshaled end + + + def test_next_quarter_on_31st + assert_equal Time.local(2005, 11, 30), Time.local(2005, 8, 31).next_quarter + end + + def test_prev_quarter_on_31st + assert_equal Time.local(2004, 2, 29), Time.local(2004, 5, 31).prev_quarter + end + + def test_last_quarter_on_31st + assert_equal Time.local(2004, 2, 29), Time.local(2004, 5, 31).last_quarter + end end diff --git a/guides/source/active_support_core_extensions.textile b/guides/source/active_support_core_extensions.textile index 40ef19cfbf..99b8bdd3fd 100644 --- a/guides/source/active_support_core_extensions.textile +++ b/guides/source/active_support_core_extensions.textile @@ -3045,6 +3045,27 @@ Date.new(2000, 1, 31).next_month # => Tue, 29 Feb 2000 +prev_month+ is aliased to +last_month+. +h6. +prev_quarter+, +next_quarter+ + +Same as +prev_month+ and +next_month+. It returns the date with the same day in the previous or next quarter: + +<ruby> +t = Time.local(2010, 5, 8) # => Sat, 08 May 2010 +t.prev_quarter # => Mon, 08 Feb 2010 +t.next_quarter # => Sun, 08 Aug 2010 +</ruby> + +If such a day does not exist, the last day of the corresponding month is returned: + +<ruby> +Time.local(2000, 7, 31).prev_quarter # => Sun, 30 Apr 2000 +Time.local(2000, 5, 31).prev_quarter # => Tue, 29 Feb 2000 +Time.local(2000, 10, 31).prev_quarter # => Mon, 30 Oct 2000 +Time.local(2000, 11, 31).next_quarter # => Wed, 28 Feb 2001 +</ruby> + ++prev_quarter+ is aliased to +last_quarter+. + h6. +beginning_of_week+, +end_of_week+ The methods +beginning_of_week+ and +end_of_week+ return the dates for the diff --git a/railties/test/application/rack/logger_test.rb b/railties/test/application/rack/logger_test.rb index a77c6f472c..46fd09cb26 100644 --- a/railties/test/application/rack/logger_test.rb +++ b/railties/test/application/rack/logger_test.rb @@ -5,6 +5,7 @@ require "rack/test" module ApplicationTests module RackTests class LoggerTest < ActiveSupport::TestCase + include ActiveSupport::Testing::Isolation include ActiveSupport::LogSubscriber::TestHelper include Rack::Test::Methods @@ -17,6 +18,7 @@ module ApplicationTests end def teardown + super teardown_app end diff --git a/railties/test/application/rake/notes_test.rb b/railties/test/application/rake/notes_test.rb index 3f4db77897..27de75b63b 100644 --- a/railties/test/application/rake/notes_test.rb +++ b/railties/test/application/rake/notes_test.rb @@ -3,12 +3,16 @@ require "isolation/abstract_unit" module ApplicationTests module RakeTests class RakeNotesTest < ActiveSupport::TestCase + include ActiveSupport::Testing::Isolation + def setup build_app require "rails/all" + super end def teardown + super teardown_app end |