views:

153

answers:

3

I have several methods I call from my controllers that feel like they should be pulled out and put into a reusable class, outside of the controller. Where do people usually put this stuff? I know I can put them into my ApplicationController, but that doesn't seem to be a great solution if I think I can use these methods later in other applications.

Also, I have a bunch of utility methods in my controllers that likely won't be used in other controllers, or in the future at all, but I feel like they just bloat my controller a bit. Do people usually move these out someplace for cleanliness or just end up with a huge controller?

I'm coming from Java and Actionscript where I'd just create new util classes for this stuff.

+3  A: 

The lib directory is a place you can put modules/classes which can be mixed in or used by controllers (and anything else, really). This can be a place to put code that doesn't fall into other areas (but be careful about making sure lib doesn't turn into a mess itself). Side comments just to keep in mind:

  • If you know you have a large amount of related functionality that could, or will, be used in other applications, that might be a plugin.

  • Its also worth keeping in mind that there's nothing wrong with creating a model that is not an Active Record object. So again, depending on what you have, this might make sense as well.

Pete
Pete's two additional points are very good. I am of the opinion that `lib` is for "generic library" logic, not domain logic. Thus, if you have some logic that is specific to this application but common to several controllers, the right place might just be in a separate module in the `app/controllers` directory.
James A. Rosen
+1  A: 

Create a module file in lib directory:

module ControllerUtil
  def foo
  end

  def bar
  end
end

Include the module in the controller:

class UsersController < ApplicationController
  include ControllerUtil
end
KandadaBoggu
A: 

Controllers should be very minimal - basically ingesting arguments and making some very high level decisions. If you have some helper functions that are doing just those kinds of things but aren't going to be reused, then keeping them in the controller is the right thing to do. Just make sure to mark them as private.

For more commonly shared things, you can either back them into your ApplicationController (if they're used in all controllers) or into a class in your app/models directory. I recommend the models directory over lib because Rails in development mode is much better about spotting changes to those files and reloading them. Changes to files in /lib tend to require restarting the webserver, which slows down your development effort. This is unfortunate, because controller helpers really should not be mixed with models.

In general, though, if you have more than a handful of these helpers, you're probably doing too much in your controllers. Take a hard look at them and see if maybe some of them shouldn't be part of your models instead.

edebill