Hi! If I have a method that is useful in both models and in views, where would be the appropriate place to put it? The method in question just takes two dates as strings, tries to parse them and returns all dates in the range between them. It also takes care of the issues when the dates are badly formatted etc etc.. This method is being used in several models (so a simple class method in one model doesn''t seem appropriate) and also in some view helpers. So, where does this method belong in rails? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/zjR_YZsmvXkJ. For more options, visit https://groups.google.com/groups/opt_out.
And actually, it is used in my controllers as well :) Den söndagen den 3:e februari 2013 kl. 13:23:28 UTC+1 skrev Linus Pettersson:> > Hi! > > If I have a method that is useful in both models and in views, where would > be the appropriate place to put it? > > The method in question just takes two dates as strings, tries to parse > them and returns all dates in the range between them. It also takes care of > the issues when the dates are badly formatted etc etc.. > > This method is being used in several models (so a simple class method in > one model doesn''t seem appropriate) and also in some view helpers. > > So, where does this method belong in rails? >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/2uIc97s6-nUJ. For more options, visit https://groups.google.com/groups/opt_out.
On Sun, Feb 3, 2013 at 6:23 AM, Linus Pettersson <linus.pettersson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> If I have a method that is useful in both models and in views, where would > be the appropriate place to put it?# config/initializer/preload_helpers.rb require "my_app/my_helper" # lib/my_app/my_helper.rb -> module MyHelper module_function def helper puts "win" end end # app/helpers/application_helpers.rb -> class ApplicationHelpers include MyHelper end # app/models/my_model.rb -> class MyModel < ActiveRecord::Base include Myhelper end> The method in question just takes two dates as strings, tries to parse them > and returns all dates in the range between them. It also takes care of the > issues when the dates are badly formatted etc etc.. > > This method is being used in several models (so a simple class method in one > model doesn''t seem appropriate) and also in some view helpers. > > So, where does this method belong in rails?Read Above. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Additionally, you can put it inside its own class and use that as necessary. It''d be similar to the previous answer, but instead of including that behaviour, you''d get an instance of that class and pass in the strings either in #initialize or the method you use to parse and call that method. ~Spaceghost On Sun, Feb 3, 2013 at 7:29 AM, Jordon Bedwell <envygeeks-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sun, Feb 3, 2013 at 6:23 AM, Linus Pettersson > <linus.pettersson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > If I have a method that is useful in both models and in views, where > would > > be the appropriate place to put it? > > # config/initializer/preload_helpers.rb > > require "my_app/my_helper" > > # lib/my_app/my_helper.rb -> > > module MyHelper > module_function > def helper > puts "win" > end > end > > # app/helpers/application_helpers.rb -> > > class ApplicationHelpers > include MyHelper > end > > # app/models/my_model.rb -> > > class MyModel < ActiveRecord::Base > include Myhelper > end > > > > The method in question just takes two dates as strings, tries to parse > them > > and returns all dates in the range between them. It also takes care of > the > > issues when the dates are badly formatted etc etc.. > > > > This method is being used in several models (so a simple class method in > one > > model doesn''t seem appropriate) and also in some view helpers. > > > > So, where does this method belong in rails? > > Read Above. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Sun, Feb 3, 2013 at 8:36 AM, Johnneylee Rollins <johnneylee.rollins-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Additionally, you can put it inside its own class and use that as necessary. > It''d be similar to the previous answer, but instead of including that > behaviour, you''d get an instance of that class and pass in the strings > either in #initialize or the method you use to parse and call that method. > > ~Spaceghost > > > On Sun, Feb 3, 2013 at 7:29 AM, Jordon Bedwell <envygeeks-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> On Sun, Feb 3, 2013 at 6:23 AM, Linus Pettersson >> <linus.pettersson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> > If I have a method that is useful in both models and in views, where >> > would >> > be the appropriate place to put it? >> >> # config/initializer/preload_helpers.rb >> >> require "my_app/my_helper" >> >> # lib/my_app/my_helper.rb -> >> >> module MyHelper >> module_function >> def helper >> puts "win" >> end >> end >> >> # app/helpers/application_helpers.rb -> >> >> class ApplicationHelpers >> include MyHelper >> end >> >> # app/models/my_model.rb -> >> >> class MyModel < ActiveRecord::Base >> include Myhelper >> end >> >> >> > The method in question just takes two dates as strings, tries to parse >> > them >> > and returns all dates in the range between them. It also takes care of >> > the >> > issues when the dates are badly formatted etc etc.. >> > >> > This method is being used in several models (so a simple class method in >> > one >> > model doesn''t seem appropriate) and also in some view helpers. >> > >> > So, where does this method belong in rails? >> >> Read Above.Sounds to me like this would be better as a gem, since it seems to be completely independent of the application (and probably have much broader use!) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.