简体   繁体   English

在Rails初始化程序中将模块包含到ruby类中

[英]Include Module into ruby class in a Rails initializer

Inside of an initializer in Rails i am trying to include a module in a rails Model 在Rails的初始化程序内部,我试图在Rails模型中包含一个模块

User.send :include, Something

This works for the first request but does not for the second/third/etc. 这适用于第一个请求,但不适用于第二个/第三个/等。 due to rails model reloading. 由于Rails模型的重新加载。 I tried using ActionDispatch::Callbacks.to_prepare ( ActionDispatch::Callbacks.to_prepare , i'm using Rails 3.0.9) 我尝试使用ActionDispatch::Callbacks.to_prepareActionDispatch :: Callbacks.to_prepare ,我正在使用Rails 3.0.9)

ActionDispatch::Callbacks.to_prepare do
  User.send :include, Something
end

but i keep getting a NoMethod error from a user instance when i try to call a method defined in my module module on the second/third/etc. 但是当我尝试在第二/第三/等模块模块中定义的方法时,我不断从用户实例中收到NoMethod错误。 request. 请求。

My question is this: Is there a way to reliably include a module into a Rails Model in an initializer without any development weirdness? 我的问题是:是否有一种方法可以可靠地将模块包含在初始化器的Rails模型中,而又不会引起开发怪异?

Update: Not Possible 更新:不可能

Apparently this is impossible to do without middleware. 显然,没有中间件是不可能做到的。 If you disagree, add an answer below. 如果您不同意,请在下面添加答案。 This wasn't an acceptable solution for my use case, so I actually didn't even try it. 对于我的用例来说,这不是可接受的解决方案,因此我什至没有尝试过。 Good luck. 祝好运。

Edit: Updated Debugging Info 编辑:更新了调试信息

I was playing around with the ActionDispatch::Callbacks.to_prepare a bit more and i noticed something strange when i put this in my initializer: 我在玩ActionDispatch::Callbacks.to_prepare了一点,当我将它放在初始化器中时,我注意到了一些奇怪的事情:

ActionDispatch::Callbacks.to_prepare do
  puts "to_prepare ==:#{User.object_id}"
end

and this in my controller 这在我的控制器中

puts "controller ==:#{User.object_id}"

on the first request I get this: 在第一个请求,我得到这个:

to_prepare ==: 2297196200
controller ==: 2297196200
to_prepare ==: 2297196200
to_prepare ==: 2324202920
to_prepare ==: 2318560780

on the second request i get this: 在第二个请求,我得到这个:

to_prepare ==: 2326823900
controller ==: 2326823900
to_prepare ==: 2317901920
to_prepare ==: 2326746040
to_prepare ==: 2314369160

The first thing i noticed was the multiple calls to to_prepare which is weird. 我注意到的第一件事是对to_prepare的多次调用,这很奇怪。 The second thing i noticed was in the first request (which works) the object_id directly before and after controller are called are the same, and they are not in any subsequent calls. 我注意到的第二件事是在第一个请求(该请求有效)中,直接在控制器被调用之前和之后的object_id是相同的,并且它们不在任何后续调用中。 If anyone could shed some light on why this is happening and how to get around it, it would be much appreciated. 如果有人能阐明为什么发生这种情况以及如何解决它,将不胜感激。

Does using a config.after_initialize hook help? 使用config.after_initialize挂钩是否有帮助? This is a development-environment-specific problem, due to class reloading taking place. 由于类重新加载,这是特定于开发环境的问题。

The one thing that would almost certainly work would be to inject your module via a middleware, though it's difficult to know if that's a suitable solution for whatever you're doing. 几乎可以肯定的一件事就是通过中间件注入模块,尽管很难知道这是否适合您的工作。

class AddYourModuleMiddleware
  def initialize(app)
    @app = app
  end

  def call(env)
    User.send(:include, YourModule) unless User < YourModule
    @app.call(env)
  end
end

Add it to the middleware chain with config.middleware.use AddYourModuleMiddleware , or alternatively by placing use AddYourMiddleware at the start of ApplicationController . 使用config.middleware.use AddYourModuleMiddleware将其添加到中间件链中,或者将use AddYourMiddleware放在ApplicationController的开头。

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

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