From 87e400599805f5591f5330e22449bf18658ee3a7 Mon Sep 17 00:00:00 2001 From: Rob Zolkos Date: Thu, 19 Aug 2010 19:28:22 +1000 Subject: [PATCH 1/4] Added weeks_ago and prev_week helpers --- .../active_support/core_ext/date/calculations.rb | 10 ++++++++++ activesupport/test/core_ext/date_ext_test.rb | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 0 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index 1856b4d..faaa139 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -127,6 +127,11 @@ class Date ) end + # Returns a new Date/DateTime representing the time a number of specified weeks ago + def weeks_ago(weeks) + advance(:weeks => -weeks) + end + # Returns a new Date/DateTime representing the time a number of specified months ago. def months_ago(months) advance(:months => -months) @@ -167,6 +172,11 @@ class Date months_since(1) end unless method_defined?(:next_month) + # Short-hand for weeks_ago(1) + def prev_week + weeks_ago(1) + end + # Returns a new Date/DateTime representing the "start" of this week (i.e, Monday; DateTime objects will have time set to 0:00). def beginning_of_week days_to_monday = self.wday!=0 ? self.wday-1 : 6 diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 3141e89..457f3e4 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -110,6 +110,21 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.new(2005,1,1).to_s, Date.new(2005,2,22).beginning_of_year.to_s end + def test_weeks_ago + assert_equal Date.new(2005,5,10), Date.new(2005,5,17).weeks_ago(1) + assert_equal Date.new(2005,5,10), Date.new(2005,5,24).weeks_ago(2) + assert_equal Date.new(2005,5,10), Date.new(2005,5,31).weeks_ago(3) + assert_equal Date.new(2005,5,10), Date.new(2005,6,7).weeks_ago(4) + assert_equal Date.new(2006,12,31), Date.new(2007,2,4).weeks_ago(5) + end + + def test_prev_week + assert_equal Date.new(2005,5,10), Date.new(2005,5,17).prev_week + assert_equal Date.new(2005,5,12), Date.new(2005,5,19).prev_week + assert_equal Date.new(2005,5,29), Date.new(2005,6,5).prev_week + assert_equal Date.new(2006,12,31), Date.new(2007,1,7).prev_week + end + def test_months_ago assert_equal Date.new(2005,5,5), Date.new(2005,6,5).months_ago(1) assert_equal Date.new(2004,11,5), Date.new(2005,6,5).months_ago(7) -- 1.7.2.3 From 66e28f397adc59b74d7c0b2c452be9e0332377ef Mon Sep 17 00:00:00 2001 From: Rob Zolkos Date: Thu, 14 Oct 2010 16:09:12 +1100 Subject: [PATCH 2/4] added documentation to guides for weeks_ago and prev_week --- .../source/active_support_core_extensions.textile | 21 ++++++++++++++++++++ 1 files changed, 21 insertions(+), 0 deletions(-) diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile index dfc4d38..0dc8b47 100644 --- a/railties/guides/source/active_support_core_extensions.textile +++ b/railties/guides/source/active_support_core_extensions.textile @@ -2859,6 +2859,15 @@ d.end_of_week # => Sun, 09 May 2010 +beginning_of_week+ is aliased to +monday+ and +at_beginning_of_week+. +end_of_week+ is aliased to +sunday+ and +at_end_of_week+. +h6. +prev_week+ + ++prev_week+ returns the date corresponding to that day in the previous week. + + +d = Date.new(2010, 5, 9) # => Sun, 09 May 2010 +d.prev_week # => Sun, 02 May 2010 + + h6. +next_week+ +next_week+ receives a symbol with a day name in English (in lowercase, default is +:monday+) and it returns the date corresponding to that day in the next week: @@ -2946,6 +2955,14 @@ Date.new(2010, 4, 30).months_ago(2) # => Sun, 28 Feb 2010 Date.new(2009, 12, 31).months_since(2) # => Sun, 28 Feb 2010 +h6. +weeks_ago+ + +The method +weeks_ago+ works analogously for weeks: + + +Date.new(2010, 5, 24).weeks_ago(2) # => Mon, 17 May 2010 + + h6. +advance+ The most generic way to jump to other days is +advance+. This method receives a hash with keys +:years+, +:months+, +:weeks+, +:days+, and returns a date advanced as much as the present keys indicate: @@ -3067,6 +3084,8 @@ yesterday tomorrow beginning_of_week (monday, at_beginning_of_week) end_on_week (at_end_of_week) +weeks_ago +prev_week next_week months_ago months_since @@ -3239,6 +3258,8 @@ beginning_of_day (midnight, at_midnight, at_beginning_of_day) end_of_day beginning_of_week (monday, at_beginning_of_week) end_on_week (at_end_of_week) +weeks_ago +prev_week next_week months_ago months_since -- 1.7.2.3 From 00cae123e91bad7c30f95e310d811bd74d51fa33 Mon Sep 17 00:00:00 2001 From: Rob Zolkos Date: Thu, 14 Oct 2010 22:27:23 +1100 Subject: [PATCH 3/4] added better doc and prev_week to work like next_week --- .../active_support/core_ext/date/calculations.rb | 12 +++++++----- activesupport/test/core_ext/date_ext_test.rb | 17 ++++++++++------- .../source/active_support_core_extensions.textile | 7 +++++-- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index faaa139..c90b8dd 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -172,11 +172,6 @@ class Date months_since(1) end unless method_defined?(:next_month) - # Short-hand for weeks_ago(1) - def prev_week - weeks_ago(1) - end - # Returns a new Date/DateTime representing the "start" of this week (i.e, Monday; DateTime objects will have time set to 0:00). def beginning_of_week days_to_monday = self.wday!=0 ? self.wday-1 : 6 @@ -195,6 +190,13 @@ class Date alias :sunday :end_of_week alias :at_end_of_week :end_of_week + # Returns a new Date/DateTime representing the start of the given day in the previous week (default is the current day) + def prev_week(day = Date::DAYNAMES[self.wday].downcase.to_sym) + days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6} + result = (self - 7).beginning_of_week + days_into_week[day] + self.acts_like?(:time) ? result.change(:hour => 0) : result + end + # Returns a new Date/DateTime representing the start of the given day in next week (default is Monday). def next_week(day = :monday) days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6} diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 457f3e4..30fc8f8 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -118,13 +118,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.new(2006,12,31), Date.new(2007,2,4).weeks_ago(5) end - def test_prev_week - assert_equal Date.new(2005,5,10), Date.new(2005,5,17).prev_week - assert_equal Date.new(2005,5,12), Date.new(2005,5,19).prev_week - assert_equal Date.new(2005,5,29), Date.new(2005,6,5).prev_week - assert_equal Date.new(2006,12,31), Date.new(2007,1,7).prev_week - end - def test_months_ago assert_equal Date.new(2005,5,5), Date.new(2005,6,5).months_ago(1) assert_equal Date.new(2004,11,5), Date.new(2005,6,5).months_ago(7) @@ -234,6 +227,16 @@ class DateExtCalculationsTest < ActiveSupport::TestCase end end + def test_prev_week + assert_equal Date.new(2005,5,10), Date.new(2005,5,17).prev_week + assert_equal Date.new(2005,5,12), Date.new(2005,5,19).prev_week + assert_equal Date.new(2005,5,29), Date.new(2005,6,5).prev_week + assert_equal Date.new(2006,12,31), Date.new(2007,1,7).prev_week + assert_equal Date.new(2010,2,12), Date.new(2010,2,19).prev_week(:friday) + assert_equal Date.new(2010,2,13), Date.new(2010,2,19).prev_week(:saturday) + assert_equal Date.new(2010,2,27), Date.new(2010,3,4).prev_week(:saturday) + end + def test_next_week assert_equal Date.new(2005,2,28), Date.new(2005,2,22).next_week assert_equal Date.new(2005,3,4), Date.new(2005,2,22).next_week(:friday) diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile index 0dc8b47..8bc5ac0 100644 --- a/railties/guides/source/active_support_core_extensions.textile +++ b/railties/guides/source/active_support_core_extensions.textile @@ -2861,11 +2861,13 @@ d.end_of_week # => Sun, 09 May 2010 h6. +prev_week+ -+prev_week+ returns the date corresponding to that day in the previous week. ++prev_week+ receives a symbol with a day name in English (in lowercase, eg :monday, default is the day of the receivers date) and it returns the date corresponding to that day in the previous week. d = Date.new(2010, 5, 9) # => Sun, 09 May 2010 d.prev_week # => Sun, 02 May 2010 +d.prev_week(:saturday) # => Sat, 01 May 2010 +d.prev_week(:friday) # => Fri, 30 Apr 2010 h6. +next_week+ @@ -2960,7 +2962,8 @@ h6. +weeks_ago+ The method +weeks_ago+ works analogously for weeks: -Date.new(2010, 5, 24).weeks_ago(2) # => Mon, 17 May 2010 +Date.new(2010, 5, 24).weeks_ago(1) # => Mon, 17 May 2010 +Date.new(2010, 5, 24).weeks_ago(2) # => Mon, 10 May 2010 h6. +advance+ -- 1.7.2.3 From 398b1f37ec71ec4b17dfbd55800297fd9d8d3e1b Mon Sep 17 00:00:00 2001 From: Rob Zolkos Date: Thu, 14 Oct 2010 23:08:38 +1100 Subject: [PATCH 4/4] made prev_week be like next_week, and documented --- .../active_support/core_ext/date/calculations.rb | 4 ++-- activesupport/test/core_ext/date_ext_test.rb | 6 ++---- .../source/active_support_core_extensions.textile | 18 +++++------------- 3 files changed, 9 insertions(+), 19 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index c90b8dd..881007d 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -190,8 +190,8 @@ class Date alias :sunday :end_of_week alias :at_end_of_week :end_of_week - # Returns a new Date/DateTime representing the start of the given day in the previous week (default is the current day) - def prev_week(day = Date::DAYNAMES[self.wday].downcase.to_sym) + # Returns a new Date/DateTime representing the start of the given day in the previous week (default is Monday) + def prev_week(day = :monday) days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6} result = (self - 7).beginning_of_week + days_into_week[day] self.acts_like?(:time) ? result.change(:hour => 0) : result diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 30fc8f8..342a31c 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -228,10 +228,8 @@ class DateExtCalculationsTest < ActiveSupport::TestCase end def test_prev_week - assert_equal Date.new(2005,5,10), Date.new(2005,5,17).prev_week - assert_equal Date.new(2005,5,12), Date.new(2005,5,19).prev_week - assert_equal Date.new(2005,5,29), Date.new(2005,6,5).prev_week - assert_equal Date.new(2006,12,31), Date.new(2007,1,7).prev_week + assert_equal Date.new(2005,5,9), Date.new(2005,5,17).prev_week + assert_equal Date.new(2006,12,25), Date.new(2007,1,7).prev_week assert_equal Date.new(2010,2,12), Date.new(2010,2,19).prev_week(:friday) assert_equal Date.new(2010,2,13), Date.new(2010,2,19).prev_week(:saturday) assert_equal Date.new(2010,2,27), Date.new(2010,3,4).prev_week(:saturday) diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile index 8bc5ac0..0824a52 100644 --- a/railties/guides/source/active_support_core_extensions.textile +++ b/railties/guides/source/active_support_core_extensions.textile @@ -2859,25 +2859,17 @@ d.end_of_week # => Sun, 09 May 2010 +beginning_of_week+ is aliased to +monday+ and +at_beginning_of_week+. +end_of_week+ is aliased to +sunday+ and +at_end_of_week+. -h6. +prev_week+ +h6. +next_week+, +prev_week+ -+prev_week+ receives a symbol with a day name in English (in lowercase, eg :monday, default is the day of the receivers date) and it returns the date corresponding to that day in the previous week. - - -d = Date.new(2010, 5, 9) # => Sun, 09 May 2010 -d.prev_week # => Sun, 02 May 2010 -d.prev_week(:saturday) # => Sat, 01 May 2010 -d.prev_week(:friday) # => Fri, 30 Apr 2010 - - -h6. +next_week+ - -+next_week+ receives a symbol with a day name in English (in lowercase, default is +:monday+) and it returns the date corresponding to that day in the next week: ++next_week+ receives a symbol with a day name in English (in lowercase, default is +:monday+) and it returns the date corresponding to that day in the next week or previous week: d = Date.new(2010, 5, 9) # => Sun, 09 May 2010 d.next_week # => Mon, 10 May 2010 d.next_week(:saturday) # => Sat, 15 May 2010 +d.prev_week # => Mon, 26 Apr 2010 +d.prev_week(:saturday) # => Sat, 01 May 2010 +d.prev_week(:friday) # => Fri, 30 Apr 2010 h6. +beginning_of_month+, +end_of_month+ -- 1.7.2.3