简体   繁体   English

如何将一个助手从ApplicationController包含到Sidekiq worker中?

[英]How to include a helper from the ApplicationController into a Sidekiq worker?

This is my ApplicationController controller: 这是我的ApplicationController控制器:

class ApplicationController < ActionController::Base
  helper_method :is_admin?

  def is_admin?
    ...
  end
end

I am trying to use this helper is_admin? 我正在尝试使用此助手is_admin? in the Sidekiq worker. 在Sidekiq工人中。 So far, I am including there this: 到目前为止,我包括以下内容:

class GenerateInvoiceWorker
  include Sidekiq::Worker

  def perform(product_id)
    av = ActionView::Base.new()
    av.view_paths = ActionController::Base.view_paths

    av.class_eval do
      include Rails.application.routes.url_helpers
      include ApplicationHelper
    end

    ...
  end
end

How to make the helper method available in the worker? 如何使辅助方法在工作者中可用?

Thanks 谢谢

You're WAY over thinking it: 您正在思考:

class GenerateInvoiceWorker
  include Sidekiq::Worker
  include MyHelper

  def perform(product_id)
  end
end

Extract is_admin? 提取is_admin? into ApplicationHelper or any other module. 进入ApplicationHelper或任何其他模块。 Remember helpers are nothing but ruby modules. 记住,帮助者不过是红宝石模块。 Then include that module (be it ApplicationHelper or any other custom helper, both in ApplicationController and in GenerateInvoiceWorker . 然后在ApplicationControllerGenerateInvoiceWorker都包含该模块(可以是ApplicationHelper或任何其他自定义帮助器)。

class ApplicationController < ActionController::Base
  include ApplicationHelper

end

class GenerateInvoiceWorker
  include Sidekiq::Worker
  include ApplicationHelper

  def perform(product_id)
  end
end 

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

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