简体   繁体   中英

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

This is my ApplicationController controller:

class ApplicationController < ActionController::Base
  helper_method :is_admin?

  def is_admin?
    ...
  end
end

I am trying to use this helper is_admin? in the Sidekiq worker. 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? into ApplicationHelper or any other module. 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 .

class ApplicationController < ActionController::Base
  include ApplicationHelper

end

class GenerateInvoiceWorker
  include Sidekiq::Worker
  include ApplicationHelper

  def perform(product_id)
  end
end 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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