Skip to content

Instantly share code, notes, and snippets.

@existentialmutt
Created June 18, 2021 17:48
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 existentialmutt/7ae0254df615f35cc750f8f95f25f65c to your computer and use it in GitHub Desktop.
Save existentialmutt/7ae0254df615f35cc750f8f95f25f65c to your computer and use it in GitHub Desktop.
use Rails::CurrentAttributes with current_user
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
before_action :set_current_user
private def set_current_user
Current.user = current_user
end
end
module ApplicationHelper
def current_user
Current.user || super
end
end
# app/models/current.rb
class Current < ActiveSupport::CurrentAttributes
attribute :user
end
@existentialmutt
Copy link
Author

note that when rendering outside a controller you'll have to manually set Current.user in your job/channel/etc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment