Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Add weeks_ago helper

#5122

Rails has helpers for months_ago, years_ago etc. But not weeks_ago. This patch adds a weeks_ago method to active_support, as well as a prev_week short hand method.

Usage is the same as months_ago or prev_month.

So,
Date.new(2005,5,24).weeks_ago(2) will return Date.new(2005,5,10)

And
Date.new(2005,5,17).prev_week will return Date.new(2005,5,10)

This is my first patch to Rails so hopefully I've done everything correctly.

Reported by Rob Zolkos · July 15th, 2010 @ 03:25 PM

State: committed
Milestone: 3.x
Assigned to: Xavier Noria Xavier Noria
Importance: Low

Activity

  1. Rob Zolkos
    Rob Zolkos
    • Title changed from Add weeks_ago helper to [PATCH] Add weeks_ago helper

    July 19th, 2010 @ 11:15 AM

  2. Steven Hancock
    Steven Hancock

    Seems useful, and consistent with the date features Rails already has.

    All tests pass for me with (rvm) ruby-1.8.7-p299, ree-1.8.7-2010.02 and ruby-1.9.2-rc2.

    +1

    July 19th, 2010 @ 12:18 PM

  3. John Timms
    John Timms

    This feature would be very useful, I was about to create a request for it myself. Thanks for your work, Rob!

    August 2nd, 2010 @ 05:18 PM

  4. xds2000
    xds2000

    rvm ruby 1.8.7 (2010-04-19 patchlevel 253) [i686-darwin10.4.0], MBARI 0x6770, Ruby Enterprise Edition 2010.02

    xiaodeshi:~/projects/rails[weeks_ago*]$ git am < weeks_ago.diff
    Applying: weeks_ago
    error: patch failed: activesupport/lib/active_support/core_ext/date/calculations.rb:7
    error: activesupport/lib/active_support/core_ext/date/calculations.rb: patch does not apply
    error: patch failed: activesupport/test/core_ext/date_ext_test.rb:26
    error: activesupport/test/core_ext/date_ext_test.rb: patch does not apply
    Patch failed at 0001 weeks_ago

    August 14th, 2010 @ 11:34 AM

  5. xds2000
    xds2000

    just test in rails master brach in github.So i don't clear this patch can suite any version rails.

    August 14th, 2010 @ 11:44 AM

  6. Steven Hancock
    Steven Hancock

    When the patch was submitted it applied cleanly to Rails master branch and all tests passed (for me anyway) with 3 different versions of Ruby. I can't say why nobody else tested it or committed it at that time, most likely it just got overlooked.. but you tested it a month later (almost to the day), with an understanding of how diff/patch work and how much daily activity the Rails repo gets I would probably die of shock if I tried to apply a month old patch and it applied cleanly.

    August 18th, 2010 @ 12:18 AM

  7. Rob Zolkos
    Rob Zolkos

    I'll update this patch to work with latest master later today and update the ticket accordingly.

    August 18th, 2010 @ 02:08 AM

  8. Rob Zolkos
    Rob Zolkos

    Attached is the updated weeks_ago.diff patch which applies cleanly to the current rails master and tests ok. Hope this gets looked at :)

    August 19th, 2010 @ 10:33 AM

  9. Jeff Kreeftmeijer
    Jeff Kreeftmeijer
    • State changed from new to open
    • Title changed from [PATCH] Add weeks_ago helper to Add weeks_ago helper
    • Tag changed from activesupport, date, helpers to activesupport, date, helpers, patch
    • Importance changed from to Low

    +1

    Nice one. Patch applies to master cleanly and the tests pass.

    Please remember to use the "patch" tag instead of prefixing the ticket title with "[PATCH]" to make sure patched tickets end up in the open patches bin. :)

    October 11th, 2010 @ 08:15 AM

  10. Aditya Sanghi
    Aditya Sanghi
    • Milestone set to 3.x
    • Assigned user set to Santiago Pastorino

    +1 i think weeks_ago is fairly useful.

    October 11th, 2010 @ 01:40 PM

  11. Santiago Pastorino
    Santiago Pastorino
    • Assigned user changed from Santiago Pastorino to Xavier Noria

    October 11th, 2010 @ 02:05 PM

  12. Xavier Noria
    Xavier Noria

    Looks good. Could you please add this stuff to the Active Support Core Extensions guide and submit a new patch including that?

    October 11th, 2010 @ 02:11 PM

  13. Rob Zolkos
    Rob Zolkos

    New patch that includes additions to the guide as requested.

    October 11th, 2010 @ 02:59 PM

  14. Xavier Noria
    Xavier Noria

    Rob, I have had a look at this. weeks_ago seems fine to me, but I think one would expect prev_week to be symmetric with the existing next_week helper in Active Support (see its docs in the guide for example).

    Also, are you sure Ruby 1.9 defines Date#prev_week?

    October 13th, 2010 @ 08:27 PM

  15. Rob Zolkos
    Rob Zolkos

    Here is a new patch with some fixes. Not sure what you mean by prev_week and next_week being symmetric? They work slightly differently so thought they needed to be separate in the guide. Happy to lump them together however. Let me know :)

    October 14th, 2010 @ 06:13 AM

  16. Xavier Noria
    Xavier Noria

    I mean, next_week is not defined to be weeks_since(1), it expects a day of the week as a symbol etc. Since next_week exists and it is defined that way, one would expect prev_week to be similar but about the previous week.

    October 14th, 2010 @ 09:01 AM

  17. Rob Zolkos
    Rob Zolkos

    I've redone prev_week to behave similar to next_week. However, instead of the default day being Monday, I have made it so that it is the same day as the receiver, meaning if its currently Thursday then it will return the date last Thursday (by default). This can be overriden by passing in the day name as a symbol (eg :monday) just like in next_week. I can easily change this to be exactly like the bahaviour of next_week, however I feel that by default, most will want to know the date exactly one week ago from the current day.

    Guides have been updated too.

    This is my first patch so I'm learning heaps :)

    October 14th, 2010 @ 12:34 PM

  18. Xavier Noria
    Xavier Noria

    Good!

    I agree that it is a better default, and it would be also a better default for next_week.

    But since we cannot change next_week right now, I think it would still be better that they are consistent in this regard. They should have the same default. And the documentation for both should be very similar in consequence, that's an indicator (rather than needing warnings: "careful! my other friend has a different default").

    Thanks for your effort with this patch Rob, I think we are close :).

    October 14th, 2010 @ 12:47 PM

  19. Rob Zolkos
    Rob Zolkos

    new patch attached, with prev_week default like next_week and documented in the same place in similar ways in the guides.

    October 14th, 2010 @ 01:11 PM

  20. Elad Meidar
    Elad Meidar

    I hate to be a party /smasher/ but shouldn't all those helpers be joined into one?, something like:

    DateTime.now.ago(10, :minutes)

    and if i think about it:

    DateTime.now - 10.minutes

    is way more readable.

    October 14th, 2010 @ 03:16 PM

  21. Xavier Noria
    Xavier Noria

    Elad, yeah, that one already exists it is called Date#advance, see http://guides.rubyonrails.org/active_support_core_extensions.html#a....

    There is also DateTime#advance: http://guides.rubyonrails.org/active_support_core_extensions.html#d... (Note that DateTime does not do DST), and Time#advance (Time does, but its domain depends on the machine).

    These next* prev* are just convenience shortcuts. Well, in the case of next_week not really because it involves more computations.

    October 14th, 2010 @ 03:24 PM

  22. Xavier Noria
    Xavier Noria

    I've extracted the hash with offsets to avoid its duplication, also implemented the new methods for Time. Gonna push tonight.

    October 16th, 2010 @ 12:33 AM

  23. Repository
    Repository
    • State changed from open to committed

    (from [bc1bcddede0c300e9c88f76a66a152814b734981]) implements weeks_ago and prev_week for Date/DateTime/Time [#5122 state:committed] http://github.com/rails/rails/commit/bc1bcddede0c300e9c88f76a66a152...

    October 16th, 2010 @ 01:27 AM

  24. Xavier Noria
  25. Xavier Noria
    Xavier Noria
    • State changed from open to committed

    October 16th, 2010 @ 01:31 AM

  26. Ryan Bigg
    Ryan Bigg
    • Tag changed from sheepskin boots, activesupport, date, helpers, patch to activesupport date helpers patch

    Automatic cleanup of spam.

    October 16th, 2010 @ 02:23 AM