I’m doing an app that has a very frequent need to compare dates. My
code is littered with things like:
return if foo.date < 10.days.ago
Each and every time I do one of these I have to stop and think it
through to get the sense of the comparison right. “Let’s see, recent
dates are bigger than older dates, so if I want this to happen when the
date on the left is longer ago than the date on the right…” You get
the idea.
I want to open up the date class and add a couple of methods to it like
“is_more_recent_than” and “is_longer_ago_than”, so I don’t have to think
about it anymore and so my code is more readable.
There are two things I’m not sure of with this, when I define the
method, what does the method def syntax look like? Maybe:
def is_more_recent_than(right_hand_value)
self > right_hand_value # just had to think about this for the last
time!
end
and the other thing is, where can I put this such that it will be
available from controllers, and from models and from views? do I have
to put it in more than one place to have it be automatically available
(without doing an include when I want to use it) everywhere? Maybe it
needs to be in one of my Rails files instead of in my app project?
thanks,
jp