In Rails 6, the DateTime methods of before?
and after?
were introduced, which have been a really great way of improving the readability of our Rails source. However, two other beneficial methods would be on_or_before?
and on_or_after?
, which would serve as the <=
and >=
counterparts to before?
and after?
.
In active_support/time_with_zone.rb
, you could simply add:
alias_method :on_or_before?, :<=
alias_method :on_or_after?, :>=
In addition, in active_support/core_ext/date_and_time/calculations.rb
, you could add:
# Returns true if the date/time falls on or before <tt>date_or_time</tt>.
def on_or_before?(date_or_time)
self <= date_or_time
end
# Returns true if the date/time falls on after <tt>date_or_time</tt>.
def on_or_after?(date_or_time)
self >= date_or_time
end
Both of the proposed changes would follow the exact same pattern as introduced by before?
and after?
and would continue to improve the readability of source as it relates to comparing dates\times.
Another possible name for these methods would be at_or_before?
and at_or_after?
.