Skip to content

Commit

Permalink
Create Current model as attributes singleton
Browse files Browse the repository at this point in the history
This uses `ActiveSupport::CurrentAttributes` to track the current user
across the request.
  • Loading branch information
csutter committed Jan 24, 2025
1 parent a2a8541 commit 49615eb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
11 changes: 9 additions & 2 deletions app/controllers/concerns/authentication.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
# Requires a user to be logged in through GDS SSO for any action.
# Requires a user to be logged in through GDS SSO for any action, and keeps track of the current
# user through `Current`.
module Authentication
extend ActiveSupport::Concern

included do
include GDS::SSO::ControllerMethods

before_action :authenticate_user!
before_action :authenticate_user!, :set_current
end

private

def set_current
Current.user = current_user
end
end
2 changes: 1 addition & 1 deletion app/controllers/recommended_links_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ def set_recommended_link
def recommended_link_params
params
.expect(recommended_link: %i[link title description keywords comment])
.merge(user_id: current_user.id)
.merge(user_id: Current.user.id)
end
end
4 changes: 2 additions & 2 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ApplicationHelper
def navigation_items
return [] unless current_user
return [] unless Current.user

[
{
Expand All @@ -9,7 +9,7 @@ def navigation_items
active: controller.controller_name == "recommended_links",
},
{
text: current_user.name,
text: Current.user.name,
href: Plek.new.external_url_for("signon"),
},
{
Expand Down
3 changes: 3 additions & 0 deletions app/models/current.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Current < ActiveSupport::CurrentAttributes
attribute :user
end

0 comments on commit 49615eb

Please sign in to comment.