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