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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
A simple and clean way (and also the preffered way of doing this) is
by a Rails plugin.
Create a folder under vendor/plugins, maybe "date_extensions", create
an "init.rb" file. Your init.rb would look like this:
Date.class_eval do
def is_older_than( other_date )
self < other_date
end
end
And so on, adding the other methods you want. For more about ruby
method definition, read the metaprogramming chapter from ->
http://oreilly.com/catalog/9780596516178/toc.html
-
Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en)
On Sat, Feb 28, 2009 at 5:51 PM, Jeff Pritchard
<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:>
> 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
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Maurício Linhares wrote:> A simple and clean way (and also the preffered way of doing this) is > by a Rails plugin. > > Create a folder under vendor/plugins, maybe "date_extensions", create...> Maur�cio Linhares > http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ > (en)Thanks Mauricio, I had no idea it was that simple to make a plugin! thanks, jp -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---