design modeling question

controllers ManufacturersController ProductsController ClassifiedsController UsersController

models (parent models) Manufacuturer has_one Logo Product has_one product_picture Classified has_many classified_photos User has_one avatar

more models (STI) Logo Product_Picture Classified_photos Avatar

1- Do I create one Controller called ImageUploadController and put all the custom methods (each type of picture has its own characteristics), where I will also need to have custom routes such as: add_prodcut_logo, delete_product_logo, etc..

2- Add custom methods, and custom named routes in the parent models

3- Or, just create a separate controller for each such as LogoController, ProductPhotoController, ClassifiedPhotoController and use nested routes, each of the pictures will be operated on within their parent model.

mabed wrote:

  > 1- Do I create one Controller called ImageUploadController and put all

the custom methods (each type of picture has its own characteristics), where I will also need to have custom routes such as: add_prodcut_logo, delete_product_logo, etc..

2- Add custom methods, and custom named routes in the parent models

The non-default route system exists so you don't need to name URI paths after controllers. This lets you put the logic wherever you like, independent from the controllers.

3- Or, just create a separate controller for each such as LogoController, ProductPhotoController, ClassifiedPhotoController and use nested routes, each of the pictures will be operated on within their parent model.

Which solution leads to the least code, and with the most similar code grouped together? I suspect that a controller in charge of images would have a few private methods - maybe a few helper methods - for the heavy lifting.

Also, remember to write unit tests before you write the tested code. This (generally) forces you to test all your code, and it allows you to move code around. You should never be stuck with one pattern. You should be free to start with something that sucks but at least works, so you can improve that over time.

The way I handled it is to think about it this way:

A Product Logo is really just part of a Product, so it should be handled by the create/update on the products controller.

Then, within the create method on products_controller, I'd do something like:

@product = Product.new(params[:product]) @product.build_product_logo(params[:product_logo])

That will create the logo and assign it to product via the association.

For my app, the logo uploads are all within the context of something else. I.e. there isn't ever a form _just_ for logos. They are always part of a Product or User or whatever. This also then puts the logic of what type of logo into the realm of the "thing" that cares... i.e. a Product needs ProductLogo so it's create method uses "build_product_logo".

-Danimal