简体   繁体   English

Ruby on Rails:我应该在哪里存储模块?

[英]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. 我是Ruby on Rails的新手,我的问题是关于应用程序设计,基于Rails 3.在Rails上创建标准网站(如博客)的互联网上有很多数据,但我的应用程序需要的不仅仅是“发布和阅读“我不知道如何实现这一点。

The idea: 想法:

  1. The model "Route" includes a number of airlines modules: "Ryanair", "easyJet", etc. “Route”模型包括许多航空公司模块:“Ryanair”,“easyJet”等。
  2. The "Route.Update" method calls the "UpdateRoutes" on each airline module (for example, "Ryanair.UpdateRoutes", "easyJet.UpdateRoutes") “Route.Update”方法调用每个航空​​公司模块上的“UpdateRoutes”(例如,“Ryanair.UpdateRoutes”,“easyJet.UpdateRoutes”)
  3. It should work the same way with more models (such as "Flight.find") and more airlines ("Delta.FindFlights") 对于更多模型(例如“Flight.find”)和更多航空公司(“Delta.FindFlights”),它应该以相同的方式工作

The questions: 问题:

  1. Where should I store all the modules? 我应该在哪里存储所有模块? I don't see any app/modules folder in Rails. 我在Rails中看不到任何app / modules文件夹。
  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). 我想象类似于“IncludeAirlines”的方法,它将通过模块/航空公司/ 名称 .rb,包括每个模块并调用它所需的方法(例如名称 .UpdateRoutes)。 Is there any better way to implement that in Ruby on Rails? 有没有更好的方法在Ruby on Rails中实现它?

As you might know, modules are generally used either as namespaces or as mixins. 您可能知道,模块通常用作命名空间或mixins。

Where you place a module depends on how tightly coupled a module is with the app directory . 放置模块的位置取决于模块与app目录的紧密耦合程度。 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. / lib目录,如果模块没有特别“交互”或关注应用程序/并且您将模块视为内部插件。

  2. The app/models directory, would be an appropriate place if your module is central to your business logic. 如果您的模块是业务逻辑的核心,那么app / models目录将是一个合适的位置。 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. 37信号引入了一种将其视为“关注点”并将其存储在app / Concer中的模式。

If your module uses a gem, you may need to require the gem in the module (sometimes a require is not at all necessary). 如果您的模块使用gem,您可能需要在模块中需要gem(有时候根本不需要)。

Your 3rd question is not clear. 你的第3个问题不明确。 Sorry about that. 对于那个很抱歉。 Not quite sure what you're trying to do. 不太清楚你想要做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM