0.0
No commit activity in last 3 years
No release in over 3 years
Pass a user through to your active_jobs for context
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0

Runtime

 Project Readme

ActiveJob::Users

Build Status Coverage Status Code Climate Gem Version Dependency Status

ActiveJob::Users allows you to pass a job_user argument through to your jobs for context and makes it accessible within your MyJob#perform method. This can be useful when you have jobs that are triggered by users through some interface (i.e. an admin portal) and you want to preserve that current_user-style context within your job execution.

It's used by audited-activejob to set the user association for audits generated in background jobs.

Getting Started

Just add the gem to your Gemfile and run bundle install:

gem 'activejob-users'

Usage

The gem provides a job_user method to any jobs that include the ActiveJob::Users mixin, which you can use however you like within your jobs. You can populate this when queueing up your job by passing a job_user: user keyword argument. Note: You do not need to modify your job's MyJob#perform method to accept this extra argument.

class MyJob < ActiveJob::Base
  include ActiveJob::Users
  queue_as :default

  def perform
    Rails.logger.info "Executed MyJob for #{job_user.try(:email) || 'unknown'}"
  end
end

# without a user
MyJob.perform_later
# writes "Executed MyJob for unknown" to the rails log

# with a user
MyJob.perform_later job_user: User.find_by(email: 'mark@markrebec.com')
# writes "Executed MyJob for mark@markrebec.com" to the rails log

# if you're in a controller or some other context where you already have a current_user
# you'll probably just want to pass that through.
MyJob.perform_later job_user: current_user

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request