I am writing an in-house stock control system. I have models for Product, Customer, and Supplier. Each of these have a method (stock_count) to look up the number of instances each appear in the stock list (Stock). So if I have: customer_a = Customer.find(1) customer_a.stock_count, returns the number of stock items assigned to this customer. This returns the same as customer_a.stocks.length, but I''ve added further functionality so that customer_a.stock_count("sold") returns the number of items in the stock list assigned to this customer, and whose status is sold. At the moment each model contains the code for a stock_count method. This isn''t DRY! I think I should create a module containing a single definition of stock_count, rewritten so that it can be mixed into the models. Then use "include module_name" in the models that use it. My problem is that I don''t know where to put the module. Do I create a file called something line stock_count_module.rb and put it in app/models? Is there a better place to put it (e.g. /compontents, or /lib)? -- 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 -~----------~----~----~----~------~----~------~--~---
Rob Nichols wrote:> My problem is that I don''t know where to put the module. Do I create a > file called something line stock_count_module.rb and put it in > app/models? Is there a better place to put it (e.g. /compontents, or > /lib)?OK - I''ve had a play. I created a module called StockOption by creating a new file called "stock_option.rb" and having the first line as: module StockOption The rest of the file contained the method definitions, rewritten so that they were not model specific. I then saved this into \components. Then in each model I replaced the methods with the single line: include StockOption I then ran all my unit tests to make sure this worked - it does. It also works if I put the file in \lib. I''m still not sure where I should be putting the file, but at least have a working solution now. -- 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 -~----------~----~----~----~------~----~------~--~---
Lib is the place Rob. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---