Proposal: DateTime related scopes and helpers

I’ve never made a contribution to rails itself, but over the years I’ve collected many improvements within my apps which I copy over from app to app. I think I have one which might make a good addition to rails. I’d love feedback to know if this would be welcome? And if no, any guidance on how to think about what feature improvements rails core is looking for? I might have others that could be good.

Context: When you add an enum attribute to a an ActiveRecord model, you automatically get scopes that correspond to the enum values. Similarly, when you have a boolean attribute you automatically get some helper methods like [boolean]?

I’d like to add some scopes and helpers that get automatically added whenever you have a datetime attribute and the name of that is of the form [past tense word]_at

For example, say I add an “expired_at” datetime column and I use it to mark when a User has expired, I also set it to the future to mark when someone will expire. My model gets:

Scopes:

expired_after()
expired_before()
expired_on(d)  # between d.beginning_of_day and d.end_of_day
expired_on_or_after()
expired_on_or_before()
expired  # where.not(expired_at: nil).where(“expired_at <= ?”, Time.current)

Helper methods:

expired! (sets created_at to Time.current)
expired?  # if expired_at not nil and expired_at is in the past
expired_after()
expired_before()
expired_on()
expired_on_or_after()
expired_on_or_before()

Would this make a good feature addition to rails?

I’m by no means an authority on this - but my suggestion would be to release this as a gem as a way to test the water.

My guess is that it is a little too ‘special-case-ish’ for the core api.

1 Like

Theres a gem that does some of that: time_for_a_boolean