Ruby on Rails:我应该在哪里存储模块? [英] Ruby on Rails: where should I store modules?

查看:84
本文介绍了Ruby on Rails:我应该在哪里存储模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Ruby on Rails的新手,我的问题是基于Rails 3的应用程序设计.在Rails上创建标准网站(例如博客)时,互联网上有很多数据,但是我的应用程序需要更多内容不只是发布并阅读",而且我不确定如何实现.

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.

想法:

  1. 航线"模型包括许多航空公司模块:瑞安航空","easyJet"等.
  2. "Route.Update"方法在每个航空公司模块上调用"UpdateRoutes"(例如,"Ryanair.UpdateRoutes","easyJet.UpdateRoutes")
  3. 对于更多模型(例如"Flight.find")和更多航空公司("Delta.FindFlights"),它应该以相同的方式工作

问题:

  1. 我应该将所有模块存储在哪里?我在Rails中看不到任何app/modules文件夹.
  2. 如果我的模块需要gem,我应该将它们包含在模块或模型(实际使用它们的位置)中吗?
  3. 我想使我的应用程序具有可伸缩性.例如,我要添加一个新的运行中的航空公司(模块),而不更改航线",航班"或任何其他模型中的任何代码.我想像类似"IncludeAirlines"的方法,它会通过modules/airlines/ name .rb,包含每个模块并调用所需的方法(例如 name ). UpdateRoutes).有没有更好的方法可以在Ruby on Rails中实现呢?
  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?

推荐答案

您可能知道,模块通常用作名称空间或mixin.

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

放置模块的位置取决于模块与app目录的耦合程度.存储模块中的几种模式:

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

  1. /lib目录,如果该模块不是特别交互"或与app/无关,并且您将该模块视为内部插件.

  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.

如果模块是业务逻辑的核心,那么app/models目录将是一个合适的位置.在这里,一个流行的用例是使用模块作为混合来干燥模型/控制器.

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.

37信号引入了一种将其视为关注点"的模式'并将它们存储在应用程序/关注点中.

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.

这篇关于Ruby on Rails:我应该在哪里存储模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆