I want to have a module that I mixin to more than one model. What would be the most "rails like" way to do this? -- 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 -~----------~----~----~----~------~----~------~--~---
Perry Smith wrote:> I want to have a module that I mixin to more than one model. > > What would be the most "rails like" way to do this? > >I put it in a file (my_mixin_module.rb) in /lib and then include it in each model that needs it: class SomeModel < AR::B include MyMixinModule end Works for me. Hope this helps, Chris -- ---------------------------- Autopendium :: Stuff about old cars http://autopendium.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 -~----------~----~----~----~------~----~------~--~---
Chris Taggart wrote:> Perry Smith wrote: >> I want to have a module that I mixin to more than one model. >> >> What would be the most "rails like" way to do this? >> >> > I put it in a file (my_mixin_module.rb) in /lib and then include it in > each model that needs it: > > class SomeModel < AR::B > include MyMixinModule > end >Thanks. I was wondering if I need to add a require line at the top. I''ll try it without and see what happens. I''m still not 100% clear of all the load magic in Rails. Thank you again, Perry -- 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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2007-Jul-04 13:45 UTC
Re: mixin''s for modules
Hi -- On Wed, 4 Jul 2007, Perry Smith wrote:> > Chris Taggart wrote: >> Perry Smith wrote: >>> I want to have a module that I mixin to more than one model. >>> >>> What would be the most "rails like" way to do this? >>> >>> >> I put it in a file (my_mixin_module.rb) in /lib and then include it in >> each model that needs it: >> >> class SomeModel < AR::B >> include MyMixinModule >> end >> > > Thanks. I was wondering if I need to add a require line at the top. > I''ll try it without and see what happens. I''m still not 100% clear of > all the load magic in Rails.I believe you do have to require the file first, if it''s in lib. At least, I hope so, since automatically loading everything in lib would be a bit over-eager. David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org wrote:> Hi -- > > On Wed, 4 Jul 2007, Perry Smith wrote: > > >> Chris Taggart wrote: >> >>> Perry Smith wrote: >>> >>>> I want to have a module that I mixin to more than one model. >>>> >>>> What would be the most "rails like" way to do this? >>>> >>>> >>>> >>> I put it in a file (my_mixin_module.rb) in /lib and then include it in >>> each model that needs it: >>> >>> class SomeModel < AR::B >>> include MyMixinModule >>> end >>> >>> >> Thanks. I was wondering if I need to add a require line at the top. >> I''ll try it without and see what happens. I''m still not 100% clear of >> all the load magic in Rails. >> > > I believe you do have to require the file first, if it''s in lib. At > least, I hope so, since automatically loading everything in lib would > be a bit over-eager. > > > David > >Are you sure? I don''t require mine, and it''s loaded automatically. Also there''s this in the Rails API: lib Application specific libraries. Basically, any kind of custom code that doesn''t belong under controllers, models, or helpers. This directory is in the load path. Of course, I could be wrong. Chris -- ---------------------------- Autopendium :: Stuff about old cars http://autopendium.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 -~----------~----~----~----~------~----~------~--~---
Chris Taggart wrote:> dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org wrote: >>>>> >>>> >> >> David >> >> > Are you sure? I don''t require mine, and it''s loaded automatically. Also > there''s this in the Rails API: > > lib > > Application specific libraries. Basically, any kind of custom code > that doesn''t > belong under controllers, models, or helpers. This directory is in the > load path.Being in the load path is fine. Automatically loading all the modules in that directory is another matter. i have not tested this in my system yet so I don''t know. -- 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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2007-Jul-04 17:11 UTC
Re: mixin''s for modules
Hi -- On Wed, 4 Jul 2007, Chris T wrote:> > dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org wrote: >> Hi -- >> >> On Wed, 4 Jul 2007, Perry Smith wrote: >> >> >>> Chris Taggart wrote: >>> >>>> Perry Smith wrote: >>>> >>>>> I want to have a module that I mixin to more than one model. >>>>> >>>>> What would be the most "rails like" way to do this? >>>>> >>>>> >>>>> >>>> I put it in a file (my_mixin_module.rb) in /lib and then include it in >>>> each model that needs it: >>>> >>>> class SomeModel < AR::B >>>> include MyMixinModule >>>> end >>>> >>>> >>> Thanks. I was wondering if I need to add a require line at the top. >>> I''ll try it without and see what happens. I''m still not 100% clear of >>> all the load magic in Rails. >>> >> >> I believe you do have to require the file first, if it''s in lib. At >> least, I hope so, since automatically loading everything in lib would >> be a bit over-eager. >> >> >> David >> >> > Are you sure? I don''t require mine, and it''s loaded automatically. Also > there''s this in the Rails API: > > lib > > Application specific libraries. Basically, any kind of custom code that doesn''t > belong under controllers, models, or helpers. This directory is in the load path. > > Of course, I could be wrong.lib is definitely in the load path, but that means (or is supposed to mean) that it''s in the $LOAD_PATH array (a.k.a. $:). If everything in the load path were pre-loaded, Ruby programs would take a long time to start up :-) I''m definitely getting an error when I don''t require my little test lib file, and not when I do, with Rails 1.2.3. Which version are you using? David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org wrote:> Hi -- > > On Wed, 4 Jul 2007, Chris T wrote: > > >> dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org wrote: >> >>> Hi -- >>> >>> On Wed, 4 Jul 2007, Perry Smith wrote: >>> >>> >>> >>>> Chris Taggart wrote: >>>> >>>> >>>>> Perry Smith wrote: >>>>> >>>>> >>>>>> I want to have a module that I mixin to more than one model. >>>>>> >>>>>> What would be the most "rails like" way to do this? >>>>>> >>>>>> >>>>>> >>>>>> >>>>> I put it in a file (my_mixin_module.rb) in /lib and then include it in >>>>> each model that needs it: >>>>> >>>>> class SomeModel < AR::B >>>>> include MyMixinModule >>>>> end >>>>> >>>>> >>>>> >>>> Thanks. I was wondering if I need to add a require line at the top. >>>> I''ll try it without and see what happens. I''m still not 100% clear of >>>> all the load magic in Rails. >>>> >>>> >>> I believe you do have to require the file first, if it''s in lib. At >>> least, I hope so, since automatically loading everything in lib would >>> be a bit over-eager. >>> >>> >>> David >>> >>> >>> >> Are you sure? I don''t require mine, and it''s loaded automatically. Also >> there''s this in the Rails API: >> >> lib >> >> Application specific libraries. Basically, any kind of custom code that doesn''t >> belong under controllers, models, or helpers. This directory is in the load path. >> >> Of course, I could be wrong. >> > > lib is definitely in the load path, but that means (or is supposed to > mean) that it''s in the $LOAD_PATH array (a.k.a. $:). If everything in > the load path were pre-loaded, Ruby programs would take a long time to > start up :-) > > I''m definitely getting an error when I don''t require my little test > lib file, and not when I do, with Rails 1.2.3. Which version are you > using? > > > David > >1.2.3 also. I''ve never investigated the $LOAD_PATH array (and almost everything I know about the Rails internals I learned from your excellent book, David), but could it be one of those Rails-y-type things where if you name the file the underscored version of the mixin -- see my response to the OP -- it looks for it in the various paths in the array. Chris -- ---------------------------- Autopendium :: Stuff about old cars http://autopendium.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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2007-Jul-04 19:18 UTC
Re: mixin''s for modules
Hi -- On Wed, 4 Jul 2007, Chris T wrote:> > dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org wrote: >> Hi -- >> >> On Wed, 4 Jul 2007, Chris T wrote: >> >> >>> dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org wrote: >>> >>>> Hi -- >>>> >>>> On Wed, 4 Jul 2007, Perry Smith wrote: >>>> >>>> >>>> >>>>> Chris Taggart wrote: >>>>> >>>>> >>>>>> Perry Smith wrote: >>>>>> >>>>>> >>>>>>> I want to have a module that I mixin to more than one model. >>>>>>> >>>>>>> What would be the most "rails like" way to do this? >>>>>>> >>>>>>> >>>>>>> >>>>>>> >>>>>> I put it in a file (my_mixin_module.rb) in /lib and then include it in >>>>>> each model that needs it: >>>>>> >>>>>> class SomeModel < AR::B >>>>>> include MyMixinModule >>>>>> end >>>>>> >>>>>> >>>>>> >>>>> Thanks. I was wondering if I need to add a require line at the top. >>>>> I''ll try it without and see what happens. I''m still not 100% clear of >>>>> all the load magic in Rails. >>>>> >>>>> >>>> I believe you do have to require the file first, if it''s in lib. At >>>> least, I hope so, since automatically loading everything in lib would >>>> be a bit over-eager. >>>> >>>> >>>> David >>>> >>>> >>>> >>> Are you sure? I don''t require mine, and it''s loaded automatically. Also >>> there''s this in the Rails API: >>> >>> lib >>> >>> Application specific libraries. Basically, any kind of custom code that doesn''t >>> belong under controllers, models, or helpers. This directory is in the load path. >>> >>> Of course, I could be wrong. >>> >> >> lib is definitely in the load path, but that means (or is supposed to >> mean) that it''s in the $LOAD_PATH array (a.k.a. $:). If everything in >> the load path were pre-loaded, Ruby programs would take a long time to >> start up :-) >> >> I''m definitely getting an error when I don''t require my little test >> lib file, and not when I do, with Rails 1.2.3. Which version are you >> using? >> >> >> David >> >> > 1.2.3 also. > > I''ve never investigated the $LOAD_PATH array (and almost everything I > know about the Rails internals I learned from your excellent book,Thanks :-)> David), but could it be one of those Rails-y-type things where if you > name the file the underscored version of the mixin -- see my response to > the OP -- it looks for it in the various paths in the array.I believe you''re right; there''s a Rails "secret handshake" involved. That sounds familiar. It''s not one I keep in mind much because I prefer to ''require'' things, and stuff I put in lib is often developed outside of Rails and might not follow expected naming conventions. David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.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 -~----------~----~----~----~------~----~------~--~---
unknown wrote:> Hi -- > > On Wed, 4 Jul 2007, Chris T wrote: > >>>>> >>>>>>>> I want to have a module that I mixin to more than one model. >>>>>>> include MyMixinModule >>>>> least, I hope so, since automatically loading everything in lib would >>>> lib >>> start up :-) >> >> I''ve never investigated the $LOAD_PATH array (and almost everything I >> know about the Rails internals I learned from your excellent book, > > Thanks :-) > >> David), but could it be one of those Rails-y-type things where if you >> name the file the underscored version of the mixin -- see my response to >> the OP -- it looks for it in the various paths in the array. > > I believe you''re right; there''s a Rails "secret handshake" involved. > That sounds familiar. It''s not one I keep in mind much because I > prefer to ''require'' things, and stuff I put in lib is often developed > outside of Rails and might not follow expected naming conventions.Excellent. I thought about that too and was going to try it. I suppose it would be a teny tiny bit faster to do the require before the include since it would save the exception processing (which I''m guessing is what causes Rails to go looking for it in the first place). By the way David, I too learned most/all I know from your book. Very good book. -- 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 -~----------~----~----~----~------~----~------~--~---
> I believe you''re right; there''s a Rails "secret handshake" involved. > That sounds familiar. It''s not one I keep in mind much because I > prefer to ''require'' things, and stuff I put in lib is often developed > outside of Rails and might not follow expected naming conventions.The rails autoloader likes things simple and discoverable. If you want stuff to autoload in development mode, put your classes and modules in single files named after the full class name. Your definition for your awesome Foo::Bar::Baz class should go in: lib/foo/bar/baz.rb. Rails will create modules for Foo and Bar for you too. Using require means the Dependencies model won''t track it, and therefore won''t autoload it. I think you can use require_dependency instead, but that may have been deprecated. -- Rick Olson http://lighthouseapp.com http://weblog.techno-weenie.net http://mephistoblog.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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2007-Jul-06 13:01 UTC
Re: mixin''s for modules
Hi -- On Wed, 4 Jul 2007, Rick Olson wrote:> >> I believe you''re right; there''s a Rails "secret handshake" involved. >> That sounds familiar. It''s not one I keep in mind much because I >> prefer to ''require'' things, and stuff I put in lib is often developed >> outside of Rails and might not follow expected naming conventions. > > The rails autoloader likes things simple and discoverable. If you > want stuff to autoload in development mode, put your classes and > modules in single files named after the full class name. Your > definition for your awesome Foo::Bar::Baz class should go in: > lib/foo/bar/baz.rb. Rails will create modules for Foo and Bar for you > too. > > Using require means the Dependencies model won''t track it, and > therefore won''t autoload it. I think you can use require_dependency > instead, but that may have been deprecated.I''m more thinking about cases where I *don''t* want it to autoload :-) Or, more accurately, where I might not want (or might not be able) to align the filenames and the module nesting. David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.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 -~----------~----~----~----~------~----~------~--~---
On 7/6/07, dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org <dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org> wrote:> > Hi -- > > On Wed, 4 Jul 2007, Rick Olson wrote: > > > > >> I believe you''re right; there''s a Rails "secret handshake" involved. > >> That sounds familiar. It''s not one I keep in mind much because I > >> prefer to ''require'' things, and stuff I put in lib is often developed > >> outside of Rails and might not follow expected naming conventions. > > > > The rails autoloader likes things simple and discoverable. If you > > want stuff to autoload in development mode, put your classes and > > modules in single files named after the full class name. Your > > definition for your awesome Foo::Bar::Baz class should go in: > > lib/foo/bar/baz.rb. Rails will create modules for Foo and Bar for you > > too. > > > > Using require means the Dependencies model won''t track it, and > > therefore won''t autoload it. I think you can use require_dependency > > instead, but that may have been deprecated. > > I''m more thinking about cases where I *don''t* want it to autoload :-) > Or, more accurately, where I might not want (or might not be able) to > align the filenames and the module nesting.Oh, just use require then, it won''t autoload or reload. -- Rick Olson http://lighthouseapp.com http://weblog.techno-weenie.net http://mephistoblog.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 -~----------~----~----~----~------~----~------~--~---