This project is archived and is in readonly mode.

#5122 ✓committed
Rob Zolkos

Add weeks_ago helper

Reported by Rob Zolkos | July 15th, 2010 @ 03:25 PM | in 3.x

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,,5,24).weeks_ago(2) will return,5,10)

And,5,17).prev_week will return,5,10)

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

Comments and changes to this ticket

  • Rob Zolkos

    Rob Zolkos July 19th, 2010 @ 11:15 AM

    • Title changed from “Add weeks_ago helper” to “[PATCH] Add weeks_ago helper”
  • Steven Hancock

    Steven Hancock July 19th, 2010 @ 12:18 PM

    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.


  • John Timms

    John Timms August 2nd, 2010 @ 05:18 PM

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

  • xds2000

    xds2000 August 14th, 2010 @ 11:34 AM

    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

  • xds2000

    xds2000 August 14th, 2010 @ 11:44 AM

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

  • Steven Hancock

    Steven Hancock August 18th, 2010 @ 12:18 AM

    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.

  • Rob Zolkos

    Rob Zolkos August 18th, 2010 @ 02:08 AM

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

  • Rob Zolkos

    Rob Zolkos August 19th, 2010 @ 10:33 AM

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

  • Jeff Kreeftmeijer

    Jeff Kreeftmeijer October 11th, 2010 @ 08:15 AM

    • 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”


    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. :)

  • Aditya Sanghi

    Aditya Sanghi October 11th, 2010 @ 01:40 PM

    • Milestone set to 3.x
    • Assigned user set to “Santiago Pastorino”

    +1 i think weeks_ago is fairly useful.

  • Santiago Pastorino

    Santiago Pastorino October 11th, 2010 @ 02:05 PM

    • Assigned user changed from “Santiago Pastorino” to “Xavier Noria”
  • Xavier Noria

    Xavier Noria October 11th, 2010 @ 02:11 PM

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

  • Rob Zolkos

    Rob Zolkos October 11th, 2010 @ 02:59 PM

    New patch that includes additions to the guide as requested.

  • Xavier Noria

    Xavier Noria October 13th, 2010 @ 08:27 PM

    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?

  • Rob Zolkos

    Rob Zolkos October 14th, 2010 @ 06:13 AM

    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 :)

  • Xavier Noria

    Xavier Noria October 14th, 2010 @ 09:01 AM

    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.

  • Rob Zolkos

    Rob Zolkos October 14th, 2010 @ 12:34 PM

    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 :)

  • Xavier Noria

    Xavier Noria October 14th, 2010 @ 12:47 PM


    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 :).

  • Rob Zolkos

    Rob Zolkos October 14th, 2010 @ 01:11 PM

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

  • Elad Meidar

    Elad Meidar October 14th, 2010 @ 03:16 PM

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

    and if i think about it: - 10.minutes

    is way more readable.

  • Xavier Noria

    Xavier Noria October 14th, 2010 @ 03:24 PM

    Elad, yeah, that one already exists it is called Date#advance, see

    There is also DateTime#advance: (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.

  • Xavier Noria

    Xavier Noria October 16th, 2010 @ 12:33 AM

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

  • Repository

    Repository October 16th, 2010 @ 01:27 AM

    • State changed from “open” to “committed”

    (from [bc1bcddede0c300e9c88f76a66a152814b734981]) implements weeks_ago and prev_week for Date/DateTime/Time [#5122 state:committed]

  • Xavier Noria
  • Xavier Noria

    Xavier Noria October 16th, 2010 @ 01:31 AM

    • State changed from “open” to “committed”
  • Ryan Bigg

    Ryan Bigg October 16th, 2010 @ 02:23 AM

    • Tag changed from sheepskin boots, activesupport, date, helpers, patch to activesupport date helpers patch

    Automatic cleanup of spam.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>


Referenced by