Skip to content

Commit

Permalink
[#1548] Use CurrentAttributes to replace current_user with Current.us…
Browse files Browse the repository at this point in the history
…er (#1618)

* Add Current model

* Assign Current.user in #current_user

* Replace current_user with Current.user in /settings

* Replace current_user with Current.user

* Remove current_user as a helper method, no longer used in views

* Delete #current_user
  • Loading branch information
veganstraightedge authored May 24, 2020
1 parent c26a5ed commit e3e06a7
Show file tree
Hide file tree
Showing 13 changed files with 21 additions and 42 deletions.
6 changes: 3 additions & 3 deletions app/controllers/admin/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def update
end

def destroy
return redirect_to [:admin, @article] unless current_user.can_delete?
return redirect_to [:admin, @article] unless Current.user.can_delete?

@article.destroy
redirect_to %i[admin articles], notice: 'Article was successfully destroyed.'
Expand Down Expand Up @@ -148,15 +148,15 @@ def article_params

handle_published_without_datetime permitted_params

return permitted_params if current_user.can_publish? || @article&.published?
return permitted_params if Current.user.can_publish? || @article&.published?

# Override publication_status from the submitted for,
# to prevent authors and editors from publishing a draft article
permitted_params.merge(publication_status: 'draft') if @article.blank? || @article.draft?
end

def handle_publish_now_situation permitted_params, time: Time.zone.now, zone: Time.zone.name
return permitted_params unless current_user.can_publish?
return permitted_params unless Current.user.can_publish?
return permitted_params if @article&.published?

permitted_params.merge!(
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def user_params
end

def authorize_admin_role
return redirect_to [:admin] unless current_user.can_admin_users?
return redirect_to [:admin] unless Current.user.can_admin_users?
end
end
end
7 changes: 1 addition & 6 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,10 @@ def render_content article
helper_method :render_content

def signed_in?
current_user
Current.user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :signed_in?

def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user

def authorize
redirect_to [:signin], alert: 'You need to sign in to view that page.' unless signed_in?
end
Expand Down
7 changes: 1 addition & 6 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class UsersController < Admin::AdminController
before_action :authorize
before_action :set_user

layout 'admin'

Expand All @@ -9,7 +8,7 @@ def edit; end

# /settings
def update
if @user.update(user_params)
if Current.user.update(user_params)
redirect_to [:admin], notice: 'User was successfully updated.'
else
render :edit
Expand All @@ -18,10 +17,6 @@ def update

private

def set_user
@user = current_user
end

def user_params
params.require(:user).permit(:username, :password, :password_confirmation)
end
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
2 changes: 1 addition & 1 deletion app/views/admin/_publication_status.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div id="publication-status" class="form-group">
<%= form.label :publication_status, "Publication Status" %><br>

<% Publishable.publication_statuses_for(user: current_user).each do |state| %>
<% Publishable.publication_statuses_for(user: Current.user).each do |state| %>
<%= form.radio_button :publication_status, state, id: "publication_status_#{state}", class: "sr-only" %>
<%= form.label "publication_status_#{state}", state.capitalize, for: "publication_status_#{state}" %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/articles/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

<!-- TEMP: move to partial -->
<%# TEMP: enable for alpha tolerant users, only on unpublished articles %>
<% if (1..4).include?(current_user.id) && @article.draft? %>
<% if Current.user.id == 1 && @article.draft? %>
<fieldset class="card mb-3">
<div class="card-body">
<%= form.label :word_doc, "Upload a Word Doc file", class: "card-title m-0" %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/articles/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
<%= render "admin/danger_zone",
thing: @article,
label: "article",
path: [:admin, @article] if current_user.can_delete? %>
path: [:admin, @article] if Current.user.can_delete? %>
</div>
</div>
2 changes: 1 addition & 1 deletion app/views/admin/articles/form/_publication_status.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% if current_user.can_publish? || @article.draft? %>
<% if Current.user.can_publish? || @article.draft? %>
<%= render 'admin/publication_status', form: form %>
<% elsif @article.published? %>
<b>Publication Status</b><br>
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/articles/form/_published_at.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<hr>

<% if current_user.can_publish? %>
<% if Current.user.can_publish? %>
<p class="m-0 font-weight-bold">To Publish Now…</p>

<p>
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/admin.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<footer class="my-5 text-center" style="padding-top: 10rem">
<hr class="my-5">

<% if current_user.can_admin_users? %>
<% if Current.user.can_admin_users? %>
<%= link_to "Users", [:admin, :users], class: "text-muted mx-2" %>
<% end %>
Expand Down
6 changes: 3 additions & 3 deletions app/views/users/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<h1>Settings for @<%= @user.username %></h1>
<h1>Settings for @<%= Current.user.username %></h1>

<%= form_with model: [@user], local: true do |form| %>
<%= render "admin/form_errors", thing: @user %>
<%= form_with model: [Current.user], local: true do |form| %>
<%= render "admin/form_errors", thing: Current.user %>

<div class="row">
<div class="col-12">
Expand Down
20 changes: 3 additions & 17 deletions spec/controllers/application_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,13 @@ def set_title
end
end

describe '#current_user' do
describe '#signed_in?' do
let(:user) { User.create(username: 'example', password: 'x' * 30) }

it 'loads from session' do
session[:user_id] = user.id

get :index

expect(assigns[:current_user]).to eq(user)
after do
Current.user = nil
end

it 'doesn’t break with no session' do
get :index

expect(assigns[:current_user]).to be_nil
end
end

describe '#signed_in?' do
let(:user) { User.create(username: 'example', password: 'x' * 30) }

it 'is true with a user' do
session[:user_id] = user.id

Expand Down

0 comments on commit e3e06a7

Please sign in to comment.