Skip to content

Instantly share code, notes, and snippets.

@egoens
Forked from hopsoft/example_job.rb
Created April 1, 2017 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egoens/52c5a28d42f4799fd075f38300a46d89 to your computer and use it in GitHub Desktop.
Save egoens/52c5a28d42f4799fd075f38300a46d89 to your computer and use it in GitHub Desktop.
Render views outside of the standard request context (i.e. ActiveJob) with Devise/Warden
class ExampleJob < ApplicationJob
queue_as :default
def perform(user)
# do some work
# HACK: get around limitations in devise/warden when rendering
# views outside the context of a formal http request
renderer = ::ApplicationController.renderer.new
renderer_env = renderer.instance_eval { @env }
warden = ::Warden::Proxy.new(renderer_env, ::Warden::Manager.new(Rails.application))
renderer_env["warden"] = warden
html = renderer.render(partial: "path/to/partial")
ActionCable.server.broadcast "user-#{user.id}", message_type: "example", html: html
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment