I have about 20 different Active Jobs which I now realise are each going to need a before_perform
method in which to set PaperTrail content outside the context of the controller.
I was planning on putting this before_perform
method in a helper and then including the helper in each of the jobs but I am getting an error:
undefined method `before_perform' for MyApp:JobHelpers:Module
I am thinking that this is because the module in question is just that, a module and not an Active Job. How can I avoid repeating the same 4 line before_perform
method in each of my Active Jobs?
Job_helper:
module MyApp
module JobHelpers
before_perform do |job|
# stuff to do
end
end
end
The_job:
require 'my_app/job_helpers'
class TheJob < ActiveJob::Base
include MyApp::JobHelpers
# Do more stuff
end
I used an included
callback to achieve my desired goal. I found a better description of the included callback than I could ever give in another answer here.
While other answers were similar, please find the solution that worked for me below:
module MyApp
module JobHelpers
def self.included(job_class)
job_class.before_perform do |job|
# work to be completed
end
end
end
end