在 Rails 应用程序中使用模块的模型

发布于 2024-09-24 12:50:57 字数 516 浏览 8 评论 0原文

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

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

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

class Data < ActiveRecord::Base
  include Synchronize
end

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

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).

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) 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万水千山粽是情ミ 2024-10-01 12:50:57

您是正确的,如果模块与特定模型紧密耦合,那么它不是 gem/插件的良好候选者。

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

可以放置该模块的地方之一是 lib/ 中。这是针对那些并不真正适合 app/ 中任何地方的代码,并且通常是松散耦合代码在移动到插件之前的初始位置(但这不是一个硬性规定)。但是,由于您的模块与模型紧密耦合,因此 lib/ 可能不是它的最佳位置。

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

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

然后可以像平常一样将模块混合到模型中。

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

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

希望有所帮助。

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/ is for view helper methods and shouldn't contain modules that are solely for mixing into models.

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.

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.

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

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

Hope that helps.

放血 2024-10-01 12:50:57

这个链接帮助我解决了这个问题。

http://ander.heroku.com/2010/12/ 14/concerns-in-rails-3/

我一直把它放在 model/extensions 目录中。关注目录很有意义,但“关注”这个词对我来说并不明显。也许它会在我身上成长。

我还在 application.rb 中添加了扩展路径

config.autoload_paths += %W(#{config.root}/app/models/extensions)

This link has helped me out around this.

http://ander.heroku.com/2010/12/14/concerns-in-rails-3/

I have been sticking it in a model/extensions directory. The concerns directory makes sense but the word 'concerns' doesn't feel obvious to me. Maybe it will grow on me.

I also added the extensions path in the application.rb

config.autoload_paths += %W(#{config.root}/app/models/extensions)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文