4.0 的 Rails Observer 替代品 [英] Rails Observer Alternatives for 4.0

查看:29
本文介绍了4.0 的 Rails Observer 替代品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着 Observers 正式从 Rails 4.0 中删除,我很好奇其他开发人员正在使用什么来代替它们.(除了使用提取的 gem 之外.)虽然 Observers 肯定会被滥用并且有时很容易变得笨拙,但除了缓存清除之外,还有许多用例是有益的.

With Observers officially removed from Rails 4.0, I'm curious what other developers are using in their place. (Other than using the extracted gem.) While Observers were certainly abused and could easily become unwieldily at times, there were many use-cases outside of just cache-clearing where they were beneficial.

以需要跟踪模型更改的应用程序为例.观察者可以轻松地观察模型 A 的变化,并在数据库中记录模型 B 的这些变化.如果您想观察多个模型之间的变化,那么一个观察者就可以处理.

Take, for example, an application that needs to track changes to a model. An Observer could easily watch for changes on Model A and record those changes with Model B in the database. If you wanted to watch for changes across several models, then a single observer could handle that.

在 Rails 4 中,我很好奇其他开发人员使用什么策略来代替 Observers 来重新创建该功能.

In Rails 4, I'm curious what strategies other developers are using in place of Observers to recreate that functionality.

就我个人而言,我倾向于一种胖控制器"实现,其中在每个模型控制器的创建/更新/删除方法中跟踪这些更改.虽然它稍微增加了每个控制器的行为,但它确实有助于提高可读性和理解性,因为所有代码都在一个地方.缺点是现在有非常相似的代码分散在几个控制器中.将该代码提取到辅助方法中是一种选择,但您仍然需要调用那些随处可见的方法.不是世界末日,但也不完全符合瘦控制器"的精神.

Personally, I'm leaning towards a sort of "fat controller" implementation, where these changes are tracked in each models controller's create/update/delete method. While it bloats the behavior of each controller slightly, it does help in readability and understanding as all the code is in one place. The downside is that there's now code that is very similar scattered throughout several controllers. Extracting that code into helper methods is an option, but you're still left with calls to those methods littered everywhere. Not the end of the world, but not quite in the spirit of "skinny controllers" either.

ActiveRecord 回调是另一种可能的选择,尽管我个人并不喜欢它,因为在我看来它倾向于将两个不同的模型过于紧密地结合在一起.

ActiveRecord callbacks are another possible option, though one I don't personally like as it tends to couple two different models too closely together in my opinion.

因此,在 Rails 4 的无观察者世界中,如果您必须在创建/更新/销毁另一条记录后创建一条新记录,您会使用什么设计模式?胖控制器、ActiveRecord 回调或其他完全不同的东西?

So in the Rails 4, no-Observers world, if you had to create a new record after another record was created/updated/destroyed, what design pattern would you use? Fat controllers, ActiveRecord callbacks, or something else entirely?

谢谢.

推荐答案

看看 在您的模型目录中创建一个名为关注的文件夹.在那里添加一个模块:

Create a folder in your models directory called concerns. Add a module there:

module MyConcernModule
  extend ActiveSupport::Concern

  included do
    after_save :do_something
  end

  def do_something
     ...
  end
end

接下来,将其包含在您希望在其中运行 after_save 的模型中:

Next, include that in the models you wish to run the after_save in:

class MyModel < ActiveRecord::Base
  include MyConcernModule
end

根据你在做什么,这可能会让你在没有观察者的情况下接近.

Depending on what you're doing, this might get you close without observers.

这篇关于4.0 的 Rails Observer 替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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