MonkeyPatching ActiveJobs [英] MonkeyPatching ActiveJobs

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

问题描述

我在 ActiveJobs 的猴子修补部分遇到了问题.我在 config/initializers/extensions/arguements.rb 中有以下代码

I am having an issue monkey-patching part of ActiveJobs. I have the following code in config/initializers/extensions/arguements.rb

module ActiveJob
  module Arguments
    TYPE_WHITELIST = [ Date, DateTime, Time, NilClass, Fixnum, Float, String, TrueClass, FalseClass, Bignum ]
  end
end

基本上,我正在尝试添加对日期/时间对象的基本支持,以便在 ActionMailer#deliver_later 创建的 ActiveJob 中使用

Basically, I am trying to add basic support for Date/Time objects for use in the ActiveJob created by ActionMailer#deliver_later

加载 rails 应用程序后,我可以看到我的白名单已加载,但是当我在邮件程序上调用 Deliver_later 方法时,原始白名单会覆盖我的补丁.

Upon loading the rails app I can see my whitelist is loaded, however when I call the deliver_later method on a mailer the original whitelist overrides my patch.

#List is correct when app loads
2.1.2 :002 > ActiveJob::Arguments::TYPE_WHITELIST
 => [Date, DateTime, Time, NilClass, Fixnum, Float, String, TrueClass, FalseClass, Bignum] 

#List is overridden by default list in ActiveJobs after running #deliver_later
2.1.2 :005 > ActiveJob::Arguments::TYPE_WHITELIST
 => [NilClass, Fixnum, Float, String, TrueClass, FalseClass, Bignum] 

如何使修改后的白名单保持不变?我很确定该错误源于原始 ActiveJob::Arguments 直到调用 Deliver_later 才加载,因此在我的补丁之后加载并覆盖它,尽管我不确定如何解决该问题.

How can I make my modified whitelist stick? I am pretty sure the error stems from the original ActiveJob::Arguments not loading until deliver_later is called, and therefore loads after my patch and overrides it, though I am not sure how to fix that.

推荐答案

请勿使用,请参阅 https://stackoverflow.com/a/50743819/3293310

这个怎么样?

module ActiveJob
  module Arguments 
    remove_const(:TYPE_WHITELIST)
    TYPE_WHITELIST = [ Date, DateTime, Time, NilClass, Fixnum, Float, String, TrueClass, FalseClass, Bignum ]
  end
end

然后,如评论中所述,您应该扩展此模块:

Then, as said in the comments you should extend this module :

module ActionMailer 
  class DeliveryJob < ActiveJob::Base 
    extend ActiveJob::Arguments 
  end
end

如果您使用 ruby​​ 2+,更好的方法是使用 Refinements.不幸的是,您无法通过改进来更改常量(在此处阅读 Matz 的评论)

A better way if you're using ruby 2+ would be tu use Refinements. Unfortunately, you can't change constants with refinements (read Matz' comment here)

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

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