在Rails应用程序中使用模块建模 [英] Model Using Modules in Rails Application

查看:90
本文介绍了在Rails应用程序中使用模块建模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,要求从辅助源加载外部数据。存在许多Web服务,我的模型可以从中获取数据(可交换),但是我不想创建使更改服务变得困难的代码(成本因可变和固定使用而异,并且可能会发生变化

I have a model that requires loading external data from an auxiliary source. A number of web services exist that my model can fetch the data from (swappable), but I don't want to create code that will make it difficult to change services (costs significantly differ based on variable and fixed usage and it is likely changing will be required).

我想创建一个驱动程序来执行交互(然后在服务需要切换的情况下再创建自定义驱动程序)。不幸的是,由于驱动程序和模型的紧密耦合,将代码提取到插件或gem中没有意义。我已将所有代码提取到模块中(请参见示例),并且当前已在模型上方声明了代码。

I would like to create a driver to perform the interaction (and then create further custom drivers if the service requires switching). Unfortunately, due to the tight coupling of the driver and model, it does not makes sense to extract the code into a plugin or gem. I have extracted all the code into a module (see example), and currently have the code declared above my model.

module Synchronize
  def refresh
    self.attributes = ...
    self.save
  end
end

class Data < ActiveRecord::Base
  include Synchronize
end

Does Rails(3.0.0 )是否有用于存储与模型紧密耦合的模块的约定?我应该使用插件来执行此操作吗?这与 app / helpers目录相关联吗?如果不是,最合适的代码存储位置在哪里?谢谢!

Does Rails (3.0.0) have a convention for storing modules tightly coupled with models? Should I be using a plugin to do this? Is this associated with the 'app/helpers' directory? If not, where is the most appropriate place to store the code? Thanks!

推荐答案

您是正确的,如果模块与特定模型紧密耦合,那么它不是宝石的理想选择/ plugin。

You are correct that if the module is tightly coupled to that specific model then it's not a good candidate for a gem/plugin.

app / helpers /用于视图助手方法,不应包含仅用于混合到模型中的模块。

app/helpers/ is for view helper methods and shouldn't contain modules that are solely for mixing into models.

您可以将模块放置在lib /中。这是针对在app /内的任何地方都不适合的代码,并且通常是松散耦合的代码在移至插件之前的最初来源(但这不是一成不变的规则)。但是,由于您的模块与模型紧密耦合,因此lib /可能不是最佳选择。

One place you could put the module is within lib/. This is for code that doesn't really fit anywhere within app/ and is often the initial home of loosely coupled code before it is moved to a plugin (but that isn't a hard and fast rule). However, since your module is tightly coupled to your model, lib/ may not be the best place for it.

我知道37signals(和其他信号)使用了关注是将相关模型代码保持在模块中的一种方式。这是通过创建app / concerns /并将模块放在其中来实现的。然后将该目录添加到config / application.rb(Rails 2的config / environment.rb)中的应用程序的加载路径中:

I know that 37signals (and others) use the concept of 'concerns' as a way of keeping related model code organised in modules. This is implemented by creating app/concerns/ and putting the modules in there. That directory is then added to the app's load path in config/application.rb (config/environment.rb for Rails 2) with:

config.load_paths += %W(#{Rails.root}/app/concerns)

然后可以将该模块正常混合到模型中。

The module can then be mixed into the model as normal.

这是Jamis Buck的原始博客文章- http://weblog.jamisbuck.org/2007/1/17/concerns-in-activerecord

Here's the original blog post about this by Jamis Buck - http://weblog.jamisbuck.org/2007/1/17/concerns-in-activerecord

我个人更喜欢的另一种变体,尽管它不涉及模块,但使用了这个插件:
http://github.com/jakehow/concerned_with

Another variation of this which I personally prefer, although it doesn't involve modules, uses this plugin: http://github.com/jakehow/concerned_with

希望有帮助。

这篇关于在Rails应用程序中使用模块建模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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