Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails: where should I store modules?

I am new to Ruby on Rails and my questions are about the application design, based on Rails 3. There are many data on the internet on the creation of standard websites (such as blogs) on Rails, but my application requires more than just "post and read" and I am not sure how to implement that.

The idea:

  1. The model "Route" includes a number of airlines modules: "Ryanair", "easyJet", etc.
  2. The "Route.Update" method calls the "UpdateRoutes" on each airline module (for example, "Ryanair.UpdateRoutes", "easyJet.UpdateRoutes")
  3. It should work the same way with more models (such as "Flight.find") and more airlines ("Delta.FindFlights")

The questions:

  1. Where should I store all the modules? I don't see any app/modules folder in Rails.
  2. If my modules require gems, should I include them in the modules or in the models (where they are actually used)?
  3. I want to make my application scalable. For example, I want to add a new working airline (module) without changing any code in "Route", "Flight" or any other model. I imagine something like the method "IncludeAirlines" which would go through modules/airlines/name.rb, include every module and call the needed method of it (such as name.UpdateRoutes). Is there any better way to implement that in Ruby on Rails?
like image 224
krn Avatar asked Oct 25 '10 09:10

krn


People also ask

Where do modules go in Rails?

The truth is that you can put your modules anywhere. Personally, my main use for modules is to create namespaces for my Active Record models to help keep things organized. Those module definitions just end up in the same files as my Active Record models.

Where should Rails concerns be placed?

You should always extend your concerns module with the supplied concerns base from Rails. Pathing is usually app/models/concerns/file. rb for model concerns and app/controllers/file.

Can a Ruby module be inherited?

The Ruby class Class inherits from Module and adds things like instantiation, properties, etc – all things you would normally think a class would have. Because Module is literally an ancestor of Class , this means Modules can be treated like classes in some ways. As mentioned, you can find Module in the array Class.


1 Answers

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.

like image 59
Shreyas Avatar answered Oct 22 '22 20:10

Shreyas