Ruby on Rails: where should I store modules? Ruby on Rails: where should I store modules? ruby-on-rails ruby-on-rails

Ruby on Rails: where should I store modules?


As you might know, modules are generally used either as namespaces or as mixins.

Where you place a module depends on how tightly coupled a module is with the app directory . A few patterns in storing modules :

  1. The /lib directory, if the module does not particularly 'interact' or concern the app/ and you treat the module as an internal plug-in.

  2. The app/models directory, would be an appropriate place if your module is central to your business logic. A popular use case here, is where you use a module as a mixin to DRY your models/controllers.

  3. 37 Signals introduced a pattern of treating them as 'concerns' and storing them in app/concerns.

If your module uses a gem, you may need to require the gem in the module (sometimes a require is not at all necessary).

Your 3rd question is not clear. Sorry about that. Not quite sure what you're trying to do.