Skip to content

Commit

Permalink
rename ShowUser to FindUser and add error handling, start authenticat…
Browse files Browse the repository at this point in the history
…ion_controller

Not currently passing spec, our log_in before_action currently must
take a User.
  • Loading branch information
npauzenga committed Nov 9, 2015
1 parent 3af23b8 commit c86f141
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 25 deletions.
3 changes: 3 additions & 0 deletions app/controllers/authenticated_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class AuthenticatedController < ApplicationController
before_action :log_in
end
8 changes: 4 additions & 4 deletions app/controllers/password_resets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class PasswordResetsController < ApplicationController
before_action :find_user, only: %i(edit update)
before_action :check_expiration, only: %i(edit update)
class PasswordResetsController < AuthenticatedController
before_action :find_user, only: %i(edit update)
before_action :check_expiration, only: %i(edit update)
skip_before_action :log_in, only: %i(new edit create)

def new
end
Expand All @@ -25,7 +26,6 @@ def update
update_password = UpdatePassword.call(user_params: user_params, user: @user)

if update_password.success?
log_in update_password.user
flash[:success] = "Password has been reset"
redirect_to update_password.user
else
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class SessionsController < ApplicationController
class SessionsController < AuthenticatedController
def new
end

Expand All @@ -7,7 +7,6 @@ def create
password: params[:session][:password])

if valid_user.success?
log_in valid_user.user
redirect_to valid_user.user
else
flash.now[:error] = "There was a problem signing in"
Expand Down
5 changes: 1 addition & 4 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ def create
end

def show
result = ShowUser.call(id: params[:id])
@user = result.user
@todos = result.todos
@todo = result.todo
@user = FindUser.call(id: params[:id], current_id: session[:user_id]).user
end

private
Expand Down
11 changes: 11 additions & 0 deletions app/interactors/find_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class FindUser
include Interactor

def call
begin
context.user = User.find(context.id)
rescue ActiveRecord::RecordNotFound
context.user = User.find(context.current_id)
end
end
end
9 changes: 0 additions & 9 deletions app/interactors/show_user.rb

This file was deleted.

10 changes: 6 additions & 4 deletions app/interactors/validate_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ class ValidateUser

def call
context.user = User.find_by(email: context.email.downcase)
user = context.user
password = context.password
errors = context.user.errors
context.fail!(errors: errors) unless user && user.authenticate(password)
authenticate_user(context.user, context.password)
end

def authenticate_user(user, password)
return if user && user.authenticate(password)
context.fail!(errors: user.errors)
end
end
4 changes: 2 additions & 2 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<h1><%= @user.name %></h1>
<h3><%= @user.email %></h3>
<p>Test subject since <%= @user.created_at.strftime("%B %d, %Y") %></p>
<%= render @todos %>
<%= render partial: "todos/form", locals: { user: @user, todo: @todo } %>
<%= render @user.todos %>
<%= render partial: "todos/form", locals: { user: @user, todo: @user.todos.new } %>
</div>

1 comment on commit c86f141

@npauzenga
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, thanks @enriikke! This is a work-in-progress (not passing spec). I've made the changes except for the log_in modifications (and I haven't added the Encryptor model, I'll do that in a separate branch).

On log_in, I've started to reorganize things. The authenticated_controller is, I think, in good shape and I've got it calling log_in as a before_action where appropriate. I think though that we need to change the way log_in or current_user works. I don't have a User to give it as an argument (if it's happening before a User is found). Currently, current_user relies on us already having logged someone in. There are likely a few ways to handle this but do you have a suggestion for which method I should be looking to modify? Also, these methods live in sessions_helper.rb. That should be fine if we're calling them from a controller but perhaps they could be moved to our Authenticated_controller? Or perhaps that's not super RESTful....

Please sign in to comment.