In my Rails application the controllers are starting to pile up (> 30).
Would it be Java, I'd started to create sub-packages long ago, but I'm a little hesitant here.
I already have a User
and an Admin
namespace but I'm not sure if it is good to create a finer namespace structure, especially considering maintainability.
What's the "Rails way" in this case?
Thanks in advance.
Don't know whether there is an ideal way, but specific to the project I deal, I have grouped it under a folder structure. Initially, again specific to my project, we were having to deal with just a couple of controllers called coach and manager. But as time went by, the size of them started bulging and we had to create few more controllers that could be grouped under a broad category. This was resulting in a flat growth.
More time went and we started grouping it in folders, example in a folder called coach all the related functionalities for a coach would go and the controller names started looking like class Coach::SchedulesController < ApplicationController
.
This way of grouping would also help in writing the functional tests. You do not want to have your functional test to have insane amount of lines as well.
But the gist as always Rails suggests is to have a skinny controller and a fat model. At times, it may not be that easy to follow that up and yeah these are some ways you could overcome the difficulties.
In Rails, namespacing controllers (or even, ugh, models) is undesirable. Yeah, sometimes it's necessary, or just plain easiest, but I'd say it's never desirable.
As long as your controllers are skinny and represent 1 model each, I wouldn't mind even 100 controllers in a flat folder. It's when you have significantly more controllers than models that I would start worrying.
Of course this is all IMHO.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With