Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added aliases for prev_year, prev_month, and prev_week in Time and Date calculations #4284

Merged
merged 1 commit into from

4 participants

@mattdbridges

As I was working with some time methods, I noticed that using the word last made more sense as it is generally how it is thought of. I have added aliases for these three methods last_year, last_month, and last_week.

@waseem

:+1: Some tests would have been nice.

@mattdbridges

What tests would I need to write? I provided some already...

@waseem

@mattdbridges I apologize. Perhaps I was too sleepy to see those tests. :)

@vijaydev
Collaborator

The AS guide needs to be updated. cc: @fxn

@mattdbridges

@vijaydev How do I go about doing this? or does someone else need to do this?

@vijaydev
Collaborator

@mattdbridges It's expected to be done as part of the patch. You would need to update this file: https://github.com/rails/rails/blob/master/railties/guides/source/active_support_core_extensions.textile. This will reflect in http://edgeguides.rubyonrails.org/active_support_core_extensions.html once the PR is merged.

@fxn
Owner

@mattdbridges I like the aliases.

We have those also in Date, could you please add them also to Date? That way DateTime will also have them via inheritance.

Then, you'd also need to edit the file railties/guides/source/active_support_core_extensions.textile and document the aliases in the corresponding section of each of the three classes somehow.

@fxn fxn was assigned
@mattdbridges

Thanks @vijaydev and @fxn. I'll work on these as soon as I can and make push them up.

@mattdbridges

@fxn Correct me if I'm wrong, but it looks like I have added these aliases to Date already. These would be in active_support/core_ext/date/calculations.rb, am I right?

@mattdbridges

Ignore the last comment. I have the prev_week method in Date already, but where would the aliases for last_month or last_year go since they are not actually defined in Date?

@fxn
Owner

They have been removed in master because they come with Ruby 1.9. Previous versions defined them for forward compatibility.

@mattdbridges

Ah, thanks for the clarification.

@mattdbridges

Is this what you were looking for?

...port/lib/active_support/core_ext/date/calculations.rb
@@ -182,6 +182,13 @@ def prev_week(day = :monday)
result = (self - 7).beginning_of_week + DAYS_INTO_WEEK[day]
self.acts_like?(:time) ? result.change(:hour => 0) : result
end
+ alias :last_week, :prev_week
@vijaydev Collaborator

Why alias here and alias_method for the other two? Moreover, this syntax is incorrect for alias. It should be alias :last_week :prev_week

I used alias here as I noticed many other other aliases used it instead of alias_method. I was unsure of what the convention was, so I went with what I saw.

I am more than willing to change it to whatever you recommend.

@vijaydev Collaborator

Fair enough, but you've to fix the error then. Also in that case, just to be consistent within this PR, please change the two alias_methods below into aliases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@vijaydev
Collaborator

Can you please remove the unrelated merge commits, and squash all your commits into one?

@mattdbridges

@vijaydev, Yes, but how do I do that exactly? I am familiar with git rebase -i, but couldn't that potentially harm the references when I push to a remote repo, as in this case?

@vijaydev
Collaborator

Yes, you've to use interactive rebase rebase -i and squash. Since this PR is created from a topic branch, you can do a force push git push -f which will overwrite the commits in this PR with the one you push. Remember to remove the unrelated commits.

In general, do not try to make a topic branch up to date with master by pulling in from master. Just work on your commits, squash and push (with force pushing whenever necessary).

@mattdbridges

@vijaydev Thanks for the tip. Actually, I never merged from the master. I am not sure how that unrelated commit ended up in there. Any thoughts on how to get it out of this PR?

@mattdbridges

Actually, it's not there anymore. I'll push my squashed commits.

@fxn fxn merged commit 699ba8a into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 21, 2012
  1. @mattdbridges
This page is out of date. Refresh to see the latest.
View
7 activesupport/lib/active_support/core_ext/date/calculations.rb
@@ -182,6 +182,13 @@ def prev_week(day = :monday)
result = (self - 7).beginning_of_week + DAYS_INTO_WEEK[day]
self.acts_like?(:time) ? result.change(:hour => 0) : result
end
+ alias :last_week :prev_week
+
+ # Alias of prev_month
+ alias :last_month :prev_month
+
+ # Alias of prev_year
+ alias :last_year :prev_year
# Returns a new Date/DateTime representing the start of the given day in next week (default is :monday).
def next_week(day = :monday)
View
3  activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -145,6 +145,7 @@ def years_since(years)
def prev_year
years_ago(1)
end
+ alias_method :last_year, :prev_year
# Short-hand for years_since(1)
def next_year
@@ -155,6 +156,7 @@ def next_year
def prev_month
months_ago(1)
end
+ alias_method :last_month, :prev_month
# Short-hand for months_since(1)
def next_month
@@ -199,6 +201,7 @@ def sunday
def prev_week(day = :monday)
ago(1.week).beginning_of_week.since(DAYS_INTO_WEEK[day].day).change(:hour => 0)
end
+ alias_method :last_week, :prev_week
# Returns a new Time representing the start of the given day in next week (default is :monday).
def next_week(day = :monday)
View
24 activesupport/test/core_ext/date_ext_test.rb
@@ -175,6 +175,18 @@ def test_prev_year_in_calendar_reform
assert_equal Date.new(1582,10,4), Date.new(1583,10,14).prev_year
end
+ def test_last_year
+ assert_equal Date.new(2004,6,5), Date.new(2005,6,5).last_year
+ end
+
+ def test_last_year_in_leap_years
+ assert_equal Date.new(1999,2,28), Date.new(2000,2,29).last_year
+ end
+
+ def test_last_year_in_calendar_reform
+ assert_equal Date.new(1582,10,4), Date.new(1583,10,14).last_year
+ end
+
def test_next_year
assert_equal Date.new(2006,6,5), Date.new(2005,6,5).next_year
end
@@ -245,6 +257,14 @@ def test_prev_week
assert_equal Date.new(2010,2,27), Date.new(2010,3,4).prev_week(:saturday)
end
+ def test_last_week
+ assert_equal Date.new(2005,5,9), Date.new(2005,5,17).last_week
+ assert_equal Date.new(2006,12,25), Date.new(2007,1,7).last_week
+ assert_equal Date.new(2010,2,12), Date.new(2010,2,19).last_week(:friday)
+ assert_equal Date.new(2010,2,13), Date.new(2010,2,19).last_week(:saturday)
+ assert_equal Date.new(2010,2,27), Date.new(2010,3,4).last_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)
@@ -265,6 +285,10 @@ def test_prev_month_on_31st
assert_equal Date.new(2004, 2, 29), Date.new(2004, 3, 31).prev_month
end
+ def test_last_month_on_31st
+ assert_equal Date.new(2004, 2, 29), Date.new(2004, 3, 31).last_month
+ end
+
def test_yesterday_constructor
assert_equal Date.current - 1, Date.yesterday
end
View
16 activesupport/test/core_ext/date_time_ext_test.rb
@@ -159,6 +159,10 @@ def test_prev_year
assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).prev_year
end
+ def test_last_year
+ assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).last_year
+ end
+
def test_next_year
assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,6,5,10,0,0).next_year
end
@@ -232,6 +236,14 @@ def test_prev_week
assert_equal DateTime.civil(2006,11,15), DateTime.civil(2006,11,23,0,0,0).prev_week(:wednesday)
end
+ def test_last_week
+ assert_equal DateTime.civil(2005,2,21), DateTime.civil(2005,3,1,15,15,10).last_week
+ assert_equal DateTime.civil(2005,2,22), DateTime.civil(2005,3,1,15,15,10).last_week(:tuesday)
+ assert_equal DateTime.civil(2005,2,25), DateTime.civil(2005,3,1,15,15,10).last_week(:friday)
+ assert_equal DateTime.civil(2006,10,30), DateTime.civil(2006,11,6,0,0,0).last_week
+ assert_equal DateTime.civil(2006,11,15), DateTime.civil(2006,11,23,0,0,0).last_week(:wednesday)
+ end
+
def test_next_week
assert_equal DateTime.civil(2005,2,28), DateTime.civil(2005,2,22,15,15,10).next_week
assert_equal DateTime.civil(2005,3,4), DateTime.civil(2005,2,22,15,15,10).next_week(:friday)
@@ -247,6 +259,10 @@ def test_prev_month_on_31st
assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 3, 31).prev_month
end
+ def test_last_month_on_31st
+ assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 3, 31).last_month
+ 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)
View
18 activesupport/test/core_ext/time_ext_test.rb
@@ -198,6 +198,10 @@ def test_prev_year
assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).prev_year
end
+ def test_last_year
+ assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).last_year
+ end
+
def test_next_year
assert_equal Time.local(2006,6,5,10), Time.local(2005,6,5,10,0,0).next_year
end
@@ -505,6 +509,16 @@ def test_prev_week
assert_equal Time.local(2006,11,15), Time.local(2006,11,23,0,0,0).prev_week(:wednesday)
end
end
+
+ def test_last_week
+ with_env_tz 'US/Eastern' do
+ assert_equal Time.local(2005,2,21), Time.local(2005,3,1,15,15,10).last_week
+ assert_equal Time.local(2005,2,22), Time.local(2005,3,1,15,15,10).last_week(:tuesday)
+ assert_equal Time.local(2005,2,25), Time.local(2005,3,1,15,15,10).last_week(:friday)
+ assert_equal Time.local(2006,10,30), Time.local(2006,11,6,0,0,0).last_week
+ assert_equal Time.local(2006,11,15), Time.local(2006,11,23,0,0,0).last_week(:wednesday)
+ end
+ end
def test_next_week
with_env_tz 'US/Eastern' do
@@ -662,6 +676,10 @@ def test_prev_month_on_31st
assert_equal Time.local(2004, 2, 29), Time.local(2004, 3, 31).prev_month
end
+ def test_last_month_on_31st
+ assert_equal Time.local(2004, 2, 29), Time.local(2004, 3, 31).last_month
+ end
+
def test_xmlschema_is_available
assert_nothing_raised { Time.now.xmlschema }
end
View
18 railties/guides/source/active_support_core_extensions.textile
@@ -2858,6 +2858,8 @@ d.next_year # => Wed, 28 Feb 2001
Active Support defines these methods as well for Ruby 1.8.
++prev_year+ is aliased to +last_year+.
+
h6. +prev_month+, +next_month+
In Ruby 1.9 +prev_month+ and +next_month+ return the date with the same day in the last or next month:
@@ -2879,6 +2881,8 @@ Date.new(2000, 1, 31).next_month # => Tue, 29 Feb 2000
Active Support defines these methods as well for Ruby 1.8.
++prev_month+ is aliased to +last_month+.
+
h6. +beginning_of_week+, +end_of_week+
The methods +beginning_of_week+ and +end_of_week+ return the dates for the
@@ -2924,6 +2928,8 @@ d.prev_week(:saturday) # => Sat, 01 May 2010
d.prev_week(:friday) # => Fri, 30 Apr 2010
</ruby>
++prev_week+ is aliased to +last_week+.
+
h6. +beginning_of_month+, +end_of_month+
The methods +beginning_of_month+ and +end_of_month+ return the dates for the beginning and end of the month:
@@ -3134,13 +3140,13 @@ end_of_week (at_end_of_week)
monday
sunday
weeks_ago
-prev_week
+prev_week (last_week)
next_week
months_ago
months_since
beginning_of_month (at_beginning_of_month)
end_of_month (at_end_of_month)
-prev_month
+prev_month (last_month)
next_month
beginning_of_quarter (at_beginning_of_quarter)
end_of_quarter (at_end_of_quarter)
@@ -3148,7 +3154,7 @@ beginning_of_year (at_beginning_of_year)
end_of_year (at_end_of_year)
years_ago
years_since
-prev_year
+prev_year (last_year)
next_year
</ruby>
@@ -3310,13 +3316,13 @@ end_of_week (at_end_of_week)
monday
sunday
weeks_ago
-prev_week
+prev_week (last_week)
next_week
months_ago
months_since
beginning_of_month (at_beginning_of_month)
end_of_month (at_end_of_month)
-prev_month
+prev_month (last_month)
next_month
beginning_of_quarter (at_beginning_of_quarter)
end_of_quarter (at_end_of_quarter)
@@ -3324,7 +3330,7 @@ beginning_of_year (at_beginning_of_year)
end_of_year (at_end_of_year)
years_ago
years_since
-prev_year
+prev_year (last_year)
next_year
</ruby>
Something went wrong with that request. Please try again.