Where do people suggest we create helper methods that are going to be used within the model to do things like string manipulation. For instance: Tag.name.make_url_friendly Does anyone have an examples where I would put this and how I would define it? It would be greatly appreciated. Your Friend, -- John Kopanas john-Iau1QiYlxLpBDgjK7y7TUQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
ljredpath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Sep-09 18:59 UTC
Re: Creating Helper Methods To Be Used Within Model
You have two options really: 1) Create some custom modules for your functionality then include them in your model. module MyStringUtilities def make_url_friendly(string) end end class Tag < AR::Base include MyStringUtilities end 2) Write direct extensions to built-in classes class String def make_url_friendly end end The above would let you use the syntax you use in your post (Tag.name.make_url_friendly). Both have their place. Choose whichever you feel more comfortable with. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Perfect! I put class String.... end at the end of my application_helper.rb. I am not sure if tha tis the best place to put it but right now it makes the most sense. Any better ideas? On 9/9/06, ljredpath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <ljredpath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > You have two options really: > > 1) Create some custom modules for your functionality then include them > in your model. > > module MyStringUtilities > def make_url_friendly(string) > end > end > > class Tag < AR::Base > include MyStringUtilities > end > > 2) Write direct extensions to built-in classes > > class String > def make_url_friendly > end > end > > The above would let you use the syntax you use in your post > (Tag.name.make_url_friendly). Both have their place. Choose whichever > you feel more comfortable with. > > > > >-- John Kopanas john-Iau1QiYlxLpBDgjK7y7TUQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
ljredpath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Sep-09 20:18 UTC
Re: Creating Helper Methods To Be Used Within Model
Yeah, stick it in its own file in lib, then require it in environment.rb. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
From my understanding, helper methods are meant to help with views only. For something like this, you should put it in the Tag model class (app/models/tag.rb) John Kopanas wrote:> Where do people suggest we create helper methods that are going to be > used within the model to do things like string manipulation. > > For instance: > > Tag.name.make_url_friendly > > Does anyone have an examples where I would put this and how I would > define it? It would be greatly appreciated. > > Your Friend, > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Good point. Problem is that I use this method in all three... models, views and controllers. :-) So putting it in the lib directory might be the best option. But then again their might be even better ones out their :-). People? On 9/9/06, Tamim Azizadah <taz-TOmwmVgPDaxWk0Htik3J/w@public.gmane.org> wrote:> > From my understanding, helper methods are meant to help with views > only. For something like this, you should put it in the Tag model class > (app/models/tag.rb) > > John Kopanas wrote: > > Where do people suggest we create helper methods that are going to be > > used within the model to do things like string manipulation. > > > > For instance: > > > > Tag.name.make_url_friendly > > > > Does anyone have an examples where I would put this and how I would > > define it? It would be greatly appreciated. > > > > Your Friend, > > > > > > > > >-- John Kopanas john-Iau1QiYlxLpBDgjK7y7TUQ@public.gmane.org http://www.kopanas.com http://www.cusec.net http://www.soen.info --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
ljredpath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Sep-09 21:20 UTC
Re: Creating Helper Methods To Be Used Within Model
I think this is merely a mix up of terminology. The word "helper" in the Rails sense derives from the ViewHelper pattern. I believe the OP was simply referring to common utility methods used throughout his code. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 9/9/06, ljredpath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <ljredpath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > You have two options really: > > 1) Create some custom modules for your functionality then include them > in your model. > > module MyStringUtilities > def make_url_friendly(string) > end > end > > class Tag < AR::Base > include MyStringUtilities > end > > 2) Write direct extensions to built-in classes > > class String > def make_url_friendly > end > end > > The above would let you use the syntax you use in your post > (Tag.name.make_url_friendly). Both have their place. Choose whichever > you feel more comfortable with. >I would say you should prefer the first method of using modules unless there is a very strong case for extending builtin objects. I think going for a module first, and then seeing just how useful certain methods are before going to a builtin. Extending String is a very powerful and useful thing, but it can also be dangerous as it clutters the namespace and could result in conflicts in the future (ex: you write String#to_url, and two months from now Rails includes a conflicting version). my 2 cents... - Rob -- http://www.robsanheim.com http://www.seekingalpha.com http://www.ajaxian.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 -~----------~----~----~----~------~----~------~--~---
Seth Thomas Rasmussen
2006-Sep-10 09:10 UTC
Re: Creating Helper Methods To Be Used Within Model
There is one implementation option that is surprisingly void from this conversation: plugins. One of the reasons plugins exist is a *better* form of "throw some shit in /lib and require it" This smells like a good case for an extension of AR::Base, perhaps. Look at plugins. They are easy and grand and way better than the hacky lib approach. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
ljredpath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Sep-10 12:19 UTC
Re: Creating Helper Methods To Be Used Within Model
Seth, I fail to see how putting utility, yet project-specific, library code in lib (where it belongs) is "hacky". Plugins would be completely overkill for this. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
hmmm... plugins does sound really sexy if I look longer term. Very good idea. Might sound overfill for one small method but surely it will grow! :-) On 9/10/06, ljredpath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <ljredpath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Seth, I fail to see how putting utility, yet project-specific, library > code in lib (where it belongs) is "hacky". Plugins would be completely > overkill for this. > > > > >-- John Kopanas john-Iau1QiYlxLpBDgjK7y7TUQ@public.gmane.org http://www.kopanas.com http://www.cusec.net http://www.soen.info --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---