Add flash notices for sign in, sign out, sign up
This commit is contained in:
parent
f91390baf6
commit
1abbdbac12
9 changed files with 84 additions and 6 deletions
|
@ -11,9 +11,9 @@ class UserSessionsController < ApplicationController
|
||||||
if authenticate_user?(user)
|
if authenticate_user?(user)
|
||||||
create_user_session(user)
|
create_user_session(user)
|
||||||
|
|
||||||
redirect_to root_path
|
redirect_to root_path, notice: 'You have been logged in!'
|
||||||
else
|
else
|
||||||
render :new
|
redirect_to signin_path, alert: 'Username or password was incorrect!'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ class UserSessionsController < ApplicationController
|
||||||
cookies.permanent[:user_session] = nil
|
cookies.permanent[:user_session] = nil
|
||||||
current_session.destroy if current_session
|
current_session.destroy if current_session
|
||||||
|
|
||||||
redirect_to root_path
|
redirect_to root_path, notice: 'You have been logged out!'
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -7,7 +7,7 @@ class UsersController < ApplicationController
|
||||||
@user = User.new(user_params)
|
@user = User.new(user_params)
|
||||||
|
|
||||||
if @user.save
|
if @user.save
|
||||||
redirect_to signin_path
|
redirect_to signin_path, notice: 'Your user account has been created'
|
||||||
else
|
else
|
||||||
render :new
|
render :new
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,2 +1,16 @@
|
||||||
module ApplicationHelper
|
module ApplicationHelper
|
||||||
|
def bootstrap_class_for(flash_type)
|
||||||
|
bootstrap_classes[flash_type] || flash_type.to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def bootstrap_classes
|
||||||
|
{
|
||||||
|
'alert' => 'alert-warning',
|
||||||
|
'error' => 'alert-danger',
|
||||||
|
'notice' => 'alert-info',
|
||||||
|
'success' => 'alert-success'
|
||||||
|
}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
5
app/views/application/_flash_message.html.slim
Normal file
5
app/views/application/_flash_message.html.slim
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
.alert.slide.up class=bootstrap_class_for(type)
|
||||||
|
button.close type='button' data-dismiss='alert'
|
||||||
|
span aria-hidden='true' ×
|
||||||
|
span.sr-only Close
|
||||||
|
= message
|
2
app/views/application/_flash_messages.html.slim
Normal file
2
app/views/application/_flash_messages.html.slim
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
- flash.each do |type, message|
|
||||||
|
= render 'flash_message', type: type, message: message
|
|
@ -8,6 +8,8 @@ html
|
||||||
|
|
||||||
body
|
body
|
||||||
== render 'navbar'
|
== render 'navbar'
|
||||||
|
.container
|
||||||
|
== render 'flash_messages'
|
||||||
|
|
||||||
.container-fluid
|
.container-fluid
|
||||||
= yield
|
= yield
|
||||||
|
|
|
@ -42,6 +42,12 @@ describe UserSessionsController, type: :controller do
|
||||||
|
|
||||||
expect(response).to redirect_to(root_path)
|
expect(response).to redirect_to(root_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'should send notice' do
|
||||||
|
post :create, user_session: data
|
||||||
|
|
||||||
|
expect(flash[:notice]).to be_present
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with invalid credentials' do
|
context 'with invalid credentials' do
|
||||||
|
@ -61,12 +67,20 @@ describe UserSessionsController, type: :controller do
|
||||||
end.to change(UserSession, :count).by(0)
|
end.to change(UserSession, :count).by(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should render :new' do
|
it 'should redirect to login page' do
|
||||||
data['username'] = ''
|
data['username'] = ''
|
||||||
|
|
||||||
post :create, user_session: data
|
post :create, user_session: data
|
||||||
|
|
||||||
expect(response).to render_template(:new)
|
expect(response).to redirect_to signin_path
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should send an error flash message' do
|
||||||
|
data['username'] = ''
|
||||||
|
|
||||||
|
post :create, user_session: data
|
||||||
|
|
||||||
|
expect(flash[:alert]).to be_present
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -104,6 +118,12 @@ describe UserSessionsController, type: :controller do
|
||||||
|
|
||||||
expect(response).to redirect_to(root_path)
|
expect(response).to redirect_to(root_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'should send a notice' do
|
||||||
|
delete :destroy
|
||||||
|
|
||||||
|
expect(flash[:notice]).to be_present
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -34,6 +34,12 @@ describe UsersController, type: :controller do
|
||||||
|
|
||||||
expect(response).to redirect_to signin_path
|
expect(response).to redirect_to signin_path
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'should send a notice flash message' do
|
||||||
|
post :create, user: data
|
||||||
|
|
||||||
|
expect(flash[:notice]).to be_present
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with invalid data' do
|
context 'with invalid data' do
|
||||||
|
|
29
spec/helpers/application_helper_spec.rb
Normal file
29
spec/helpers/application_helper_spec.rb
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
describe ApplicationHelper do
|
||||||
|
describe '#bootstrap_class_for' do
|
||||||
|
context 'when known flash_type' do
|
||||||
|
it 'should return the appropriate class for the alert flash_type' do
|
||||||
|
expect(helper.bootstrap_class_for('alert')).to eq('alert-warning')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should return the appropriate class for the error flash_type' do
|
||||||
|
expect(helper.bootstrap_class_for('error')).to eq('alert-danger')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should return the appropriate class for the notice flash_type' do
|
||||||
|
expect(helper.bootstrap_class_for('notice')).to eq('alert-info')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should return the appropriate class for the success flash_type' do
|
||||||
|
expect(helper.bootstrap_class_for('success')).to eq('alert-success')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when unknown flash_type' do
|
||||||
|
it 'should return the class as the flash_type that was given' do
|
||||||
|
expect(helper.bootstrap_class_for('test')).to eq('test')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue