Skip to content

Commit

Permalink
Merge pull request #7 from meyerhoferc/user-profile
Browse files Browse the repository at this point in the history
User profile
  • Loading branch information
meyerhoferc authored Oct 23, 2018
2 parents c876508 + 8ea6dd8 commit 19246d4
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@

# Ignore master key for decrypting credentials and more.
/config/master.key

# Ignore rbenv version
.ruby-version
5 changes: 2 additions & 3 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ def create
user = User.find_by(email: params[:email])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
# flash[:notice] = "Login successful."
redirect_to root_url
redirect_to root_path
else
flash[:warning] = 'Email or password is invalid'
render 'new'
Expand All @@ -17,6 +16,6 @@ def create
def destroy
session[:user_id] = nil
flash[:notice] = "Log out successful."
redirect_to root_url
redirect_to root_path
end
end
18 changes: 14 additions & 4 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
class UsersController < ApplicationController
def new
@user = User.new
if current_user
redirect_to root_path
else
render 'new'
end
end

def create
@user = User.new(user_params)
if @user.save
redirect_to root_url
redirect_to login_path
else
render 'new'
end
end

def show
byebug
@user = User.find(params[:id]);
if current_user
@user = User.find(session[:user_id])
render 'show'
else
redirect_to root_url
flash[:warning] = 'You must be logged in first.'
end
end

private
Expand Down
1 change: 0 additions & 1 deletion app/controllers/welcome_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class WelcomeController < ApplicationController
def index
#@users = User.all
end
end
3 changes: 3 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Name: <%= "#{@user.first_name} #{@user.last_name}" %>
<br>
Email: <%= @user.email %>
8 changes: 5 additions & 3 deletions app/views/welcome/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

<% if current_user%>
<%= "Welcome, #{current_user.first_name || current_user.email}." %>
<%= link_to "Sign Out", "/sessions/#{session[:user_id]}", method: :delete %>
<%= link_to 'Sign Out', logout_path, method: :delete %>
<br>
<%= link_to 'Profile', profile_path %>
<% else %>
<%= link_to 'Log In', new_session_path %> or
<%= link_to 'Sign Up', new_user_path %>
<%= link_to 'Log In', login_path %> or
<%= link_to 'Sign Up', signup_path %>
<% end %>
8 changes: 5 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
# note Rails has a helper for the above "root" path: root to: 'welcome#index'
# to see all routes and connections to controllers, run `rails routes` Rake task

get '/new', to: 'users#new', as: 'signup'
get "logout", to: "sessions#destroy", as: 'logout'
get '/signup' => 'users#new', as: 'signup'
get '/profile' => 'users#show', as: 'profile'
get '/login' => 'sessions#new', as: 'login'
delete '/logout' => 'sessions#destroy', as: 'logout'

resources :users, only: [:new, :create, :show]
resources :users, only: [:create]
resources :sessions, except: [:edit, :update]
end
38 changes: 38 additions & 0 deletions spec/features/users/user_can_signup_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'rails_helper'

describe 'user signup' do
context 'with a new user' do
it 'they can create an account' do
visit root_path
click_on 'Sign Up'
expect(current_path).to eq signup_path
fill_in('user[first_name]', with: 'Ra')
fill_in('user[last_name]', with: 'Zz')
fill_in('user[email]', with: '[email protected]')
fill_in('user[password]', with: 'passpass')
fill_in('user[password_confirmation]', with: 'passpass')
click_on 'Create User'

expect(current_path).to eq login_path
fill_in('email', with: '[email protected]')
fill_in('password', with: 'passpass')
click_on 'Log In'

expect(current_path).to eq root_path
expect(page).to have_content 'Welcome, Ra.'
expect(page).to have_link 'Sign Out'
expect(page).to have_link 'Profile'
expect(page).not_to have_link 'Log In'
expect(page).not_to have_link 'Sign Up'
end
end

context 'as an existing, logged in user' do
it 'redirects from /signup to root' do
u = User.create(first_name: 'Raa', last_name: 'Zzz', email: '[email protected]', password: 'passpass')
allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(u)
visit signup_path
expect(current_path).to eq root_path
end
end
end
23 changes: 23 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,27 @@
expect(u.authenticate('notthepassword')).to be_falsey
expect(u.authenticate('password123')).to eq u
end

describe 'validations' do
it 'validates email is present' do
u = User.new(first_name: 'Raz', last_name: 'Z', email: '', password: 'passpass')
expect(u.valid?).to eq false
end

it 'validates first name is present' do
u = User.new(first_name: nil, last_name: 'Z', email: '[email protected]', password: 'passpass')
expect(u.valid?).to be_falsey
end

it 'validates last name is present' do
u = User.new(first_name: 'Razz', last_name: nil, email: '[email protected]', password: 'passpass')
expect(u.valid?).to be_falsey
end

it 'validates email is unique' do
User.create(first_name: 'Raz', last_name: 'Z', email: '[email protected]', password: 'passpass')
v = User.new(first_name: 'Raz', last_name: 'Z', email: '[email protected]', password: 'passpass')
expect(v.valid?).to eq false
end
end
end

0 comments on commit 19246d4

Please sign in to comment.