From f47fbdd4ed72fdcd3005c295bbe77deee58564c5 Mon Sep 17 00:00:00 2001 From: Marc Anguera Insa Date: Wed, 10 Mar 2021 00:18:35 +0100 Subject: [PATCH] Redirect to previous page --- app/controllers/application_controller.rb | 18 +++++++++--- app/controllers/sessions_controller.rb | 5 +--- app/helpers/application_helper.rb | 1 - spec/controllers/sessions_controller_spec.rb | 31 ++++++++++---------- 4 files changed, 31 insertions(+), 24 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 3c17d740f..a1dc69389 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -8,8 +8,9 @@ class ApplicationController < ActionController::Base append_before_action :check_for_terms_acceptance!, unless: :devise_controller? before_action :configure_permitted_parameters, if: :devise_controller? - before_action :set_locale - before_action :set_current_organization + before_action :set_locale, + :set_current_organization, + :store_user_location rescue_from MissingTOSAcceptance, OutadedTOSAcceptance do redirect_to terms_path @@ -38,8 +39,18 @@ def set_current_organization end end + def store_user_location + if request.get? && !request.xhr? && is_navigational_format? && !devise_controller? + store_location_for(:user, request.fullpath) + end + end + def after_sign_in_path_for(user) - if user.members.present? + stored_location = stored_location_for(user) + + if stored_location.present? + stored_location + elsif user.members.present? users_path else page_path("about") @@ -76,7 +87,6 @@ def admin? def superadmin? current_user.try :superuser? end - alias :superuser? :superadmin? def authenticate_superuser! diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 73e762c4f..d04279767 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,14 +1,11 @@ class SessionsController < Devise::SessionsController - - # POST /resource/sign_in def create super flash.delete(:notice) end - # DELETE /resource/sign_out def destroy super flash.delete(:notice) end -end \ No newline at end of file +end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 260f6da31..cbb832429 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -62,7 +62,6 @@ def show_error_messages!(resource) def markdown(content) RDiscount.new(content || ''.freeze).to_html.html_safe end - alias m markdown # Green or red CSS class depending on whether diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb index 57254daf2..aa52e9a52 100644 --- a/spec/controllers/sessions_controller_spec.rb +++ b/spec/controllers/sessions_controller_spec.rb @@ -1,33 +1,34 @@ RSpec.describe SessionsController do - let(:user) do - Fabricate(:user, password: 'papapa22', password_confirmation: 'papapa22') + let(:user) { Fabricate(:user, password: 'papapa22', password_confirmation: 'papapa22') } + + before do + request.env["devise.mapping"] = Devise.mappings[:user] end describe '#create' do - before do - request.env["devise.mapping"] = Devise.mappings[:user] - end - it 'does not show a notice flash message' do - post :create, params: { user: { - email: user.email, - password: user.password - } } + post :create, params: { user: { email: user.email, password: user.password } } + expect(flash[:notice]).to be_nil end + + it 'redirects to the previous page' do + session["user_return_to"] = offers_path + + post :create, params: { user: { email: user.email, password: user.password } } + + expect(response).to redirect_to(offers_path) + end end describe '#destroy' do before do - request.env["devise.mapping"] = Devise.mappings[:user] - post :create, params: { user: { - email: user.email, - password: user.password - } } + post :create, params: { user: { email: user.email, password: user.password } } end it 'does not show a notice flash message' do delete :destroy + expect(flash[:notice]).to be_nil end end