From 1abbdbac121a27d5e99d71fcf57431dfaa974aa9 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Fri, 10 Jul 2015 15:20:46 -0400 Subject: [PATCH] Add flash notices for sign in, sign out, sign up --- app/controllers/user_sessions_controller.rb | 6 ++-- app/controllers/users_controller.rb | 2 +- app/helpers/application_helper.rb | 14 +++++++++ .../application/_flash_message.html.slim | 5 ++++ .../application/_flash_messages.html.slim | 2 ++ app/views/layouts/application.html.slim | 2 ++ .../user_sessions_controller_spec.rb | 24 +++++++++++++-- spec/controllers/users_controller_spec.rb | 6 ++++ spec/helpers/application_helper_spec.rb | 29 +++++++++++++++++++ 9 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 app/views/application/_flash_message.html.slim create mode 100644 app/views/application/_flash_messages.html.slim create mode 100644 spec/helpers/application_helper_spec.rb diff --git a/app/controllers/user_sessions_controller.rb b/app/controllers/user_sessions_controller.rb index 90a3384..3973f08 100644 --- a/app/controllers/user_sessions_controller.rb +++ b/app/controllers/user_sessions_controller.rb @@ -11,9 +11,9 @@ class UserSessionsController < ApplicationController if authenticate_user?(user) create_user_session(user) - redirect_to root_path + redirect_to root_path, notice: 'You have been logged in!' else - render :new + redirect_to signin_path, alert: 'Username or password was incorrect!' end end @@ -21,7 +21,7 @@ class UserSessionsController < ApplicationController cookies.permanent[:user_session] = nil current_session.destroy if current_session - redirect_to root_path + redirect_to root_path, notice: 'You have been logged out!' end private diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 2058216..b1e6401 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -7,7 +7,7 @@ class UsersController < ApplicationController @user = User.new(user_params) if @user.save - redirect_to signin_path + redirect_to signin_path, notice: 'Your user account has been created' else render :new end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index de6be79..16b0f55 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,2 +1,16 @@ 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 diff --git a/app/views/application/_flash_message.html.slim b/app/views/application/_flash_message.html.slim new file mode 100644 index 0000000..e604085 --- /dev/null +++ b/app/views/application/_flash_message.html.slim @@ -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 diff --git a/app/views/application/_flash_messages.html.slim b/app/views/application/_flash_messages.html.slim new file mode 100644 index 0000000..57f1eea --- /dev/null +++ b/app/views/application/_flash_messages.html.slim @@ -0,0 +1,2 @@ +- flash.each do |type, message| + = render 'flash_message', type: type, message: message diff --git a/app/views/layouts/application.html.slim b/app/views/layouts/application.html.slim index c0935bf..2d96faa 100644 --- a/app/views/layouts/application.html.slim +++ b/app/views/layouts/application.html.slim @@ -8,6 +8,8 @@ html body == render 'navbar' + .container + == render 'flash_messages' .container-fluid = yield diff --git a/spec/controllers/user_sessions_controller_spec.rb b/spec/controllers/user_sessions_controller_spec.rb index deaa1e9..05a8ae6 100644 --- a/spec/controllers/user_sessions_controller_spec.rb +++ b/spec/controllers/user_sessions_controller_spec.rb @@ -42,6 +42,12 @@ describe UserSessionsController, type: :controller do expect(response).to redirect_to(root_path) end + + it 'should send notice' do + post :create, user_session: data + + expect(flash[:notice]).to be_present + end end context 'with invalid credentials' do @@ -61,12 +67,20 @@ describe UserSessionsController, type: :controller do end.to change(UserSession, :count).by(0) end - it 'should render :new' do + it 'should redirect to login page' do data['username'] = '' 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 @@ -104,6 +118,12 @@ describe UserSessionsController, type: :controller do expect(response).to redirect_to(root_path) end + + it 'should send a notice' do + delete :destroy + + expect(flash[:notice]).to be_present + end end end end diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 9182884..b01b3c4 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -34,6 +34,12 @@ describe UsersController, type: :controller do expect(response).to redirect_to signin_path end + + it 'should send a notice flash message' do + post :create, user: data + + expect(flash[:notice]).to be_present + end end context 'with invalid data' do diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb new file mode 100644 index 0000000..59b4ce6 --- /dev/null +++ b/spec/helpers/application_helper_spec.rb @@ -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