Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should libraries go in Rails 3?

Where's the recommended location for libraries in Rails 3? Is it as simple as 'lib'?

I'm not sure because 'lib' seems more like a Rails 2 remnant, especially considering that it's no longer auto-loaded (and there was a lot of discussion about that, apparently).

Initializers are more for (obviously) initialization tasks such as overrides.

Specifically I have a small module for attachment handling (Paperclip doesn't fit here) that's too large and distinct to include in my model, but not generic or worthwhile enough to implement as a gem.

From a functionality standpoint it lives somewhere in the middle among the model, view, and controller. This makes it sound like a helper, but in Rails helpers are intended for views AFAIK.

Should I just put it in 'lib' and autoload it in application.rb? Or maybe I could create a custom form builder to handle the presentation (or both).

I know how to make it work, but I'm hoping to learn something new. :)

like image 539
TK-421 Avatar asked Jan 25 '11 14:01

TK-421


1 Answers

lib is still the right place to put these kind of things.

Autoloading lib was removed in Rails 3 because of the way engines work, but mainly because it's easy to just add it to the autoload_paths if you do want it automatically loaded and if not, you can require as needed. lib is still in the load path, so you don't need to specify where the module or class you're requiring is.

You're correct, helpers are intended for the view, and would not be the place to put any model-related logic.

I'd put the module in lib, and require and include it in your model as needed.

like image 154
idlefingers Avatar answered Sep 23 '22 16:09

idlefingers