Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the "Rails way" to deal with a large number of controllers?

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?

  • Just have a more or less flat controller structure?
  • Or is it better to generously bundle controllers into namespaces/modules?

Thanks in advance.

like image 436
Daniel Rikowski Avatar asked Oct 01 '11 12:10

Daniel Rikowski


2 Answers

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.

like image 100
bragboy Avatar answered Oct 05 '22 23:10

bragboy


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.

like image 39
Elliot Nelson Avatar answered Oct 05 '22 23:10

Elliot Nelson