GithubHelp home page GithubHelp logo

activejob-users's Introduction

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: '[email protected]')
# writes "Executed MyJob for [email protected]" 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

activejob-users's People

Contributors

markrebec avatar

Stargazers

François Bélanger avatar

Watchers

 avatar James Cloos avatar

Forkers

frank184

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.