Created
June 18, 2021 17:48
-
-
Save existentialmutt/7ae0254df615f35cc750f8f95f25f65c to your computer and use it in GitHub Desktop.
use Rails::CurrentAttributes with current_user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ApplicationHelper | |
def current_user | |
Current.user || super | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/current.rb | |
class Current < ActiveSupport::CurrentAttributes | |
attribute :user | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
note that when rendering outside a controller you'll have to manually set Current.user in your job/channel/etc