开发者

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜