From 7a1330323c5f39555c159f29069e066cb498c95d Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Fri, 22 Dec 2023 00:39:39 +0100 Subject: [PATCH] Fix Sorcery conflict for redirect_back_or_to See https://github.com/Sorcery/sorcery/issues/296 --- app/controllers/exercises_controller.rb | 6 +++--- app/controllers/live_streams_controller.rb | 4 ++-- app/controllers/sessions_controller.rb | 2 +- app/controllers/study_groups_controller.rb | 2 +- config/initializers/monkey_patches.rb | 11 +++++++++++ 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/app/controllers/exercises_controller.rb b/app/controllers/exercises_controller.rb index 2fc49897..c889906d 100644 --- a/app/controllers/exercises_controller.rb +++ b/app/controllers/exercises_controller.rb @@ -313,8 +313,8 @@ class ExercisesController < ApplicationController session.delete(:pair_programming) @current_contributor = current_user else - return redirect_back( - fallback_location: implement_exercise_path(current_contributor.exercise), + return redirect_back_or_to( + implement_exercise_path(current_contributor.exercise), alert: t('exercises.implement.existing_programming_group', exercise: current_contributor.exercise.title) ) end @@ -323,7 +323,7 @@ class ExercisesController < ApplicationController session[:pg_id] = pg.id @current_contributor = pg elsif session[:pg_id].blank? && session[:pair_programming] == 'mandatory' - return redirect_back(fallback_location: new_exercise_programming_group_path(@exercise)) + return redirect_back_or_to(new_exercise_programming_group_path(@exercise)) elsif session[:pg_id].blank? && session[:pair_programming] == 'optional' && current_user.submissions.where(study_group_id: current_user.current_study_group_id, exercise: @exercise).none? Event.find_or_create_by(category: 'pp_work_alone', user: current_user, exercise: @exercise, data: nil, file_id: nil) current_user.pair_programming_waiting_users&.find_by(exercise: @exercise)&.update(status: :worked_alone) diff --git a/app/controllers/live_streams_controller.rb b/app/controllers/live_streams_controller.rb index 6f0d0a6d..ec8c0c47 100644 --- a/app/controllers/live_streams_controller.rb +++ b/app/controllers/live_streams_controller.rb @@ -21,7 +21,7 @@ class LiveStreamsController < ApplicationController # Using the submission ID parameter would allow looking up the corresponding exercise ID # Therefore, we just redirect to the root_path, but actually expect to redirect back (that should work!) skip_authorization - redirect_back(fallback_location: root_path, allow_other_host: true, alert: t('exercises.download_file_tree.gone')) + redirect_back_or_to(root_path, allow_other_host: true, alert: t('exercises.download_file_tree.gone')) else desired_file = params[:filename].to_s runner = Runner.for(current_contributor, @submission.exercise.execution_environment) @@ -65,7 +65,7 @@ class LiveStreamsController < ApplicationController end end rescue Runner::Error - redirect_back(fallback_location: redirect_fallback, alert: t('exercises.download_file_tree.gone')) + redirect_back_or_to(redirect_fallback, alert: t('exercises.download_file_tree.gone')) end end end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 7d9e104b..3b2323c7 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -40,7 +40,7 @@ class SessionsController < ApplicationController if login(params[:email], params[:password], params[:remember_me]) # We set the user's default study group to the "internal" group (no external id) for the given consumer. session[:study_group_id] = current_user.study_groups.find_by(external_id: nil)&.id - redirect_back_or_to(:root, notice: t('.success')) + sorcery_redirect_back_or_to(:root, notice: t('.success')) else flash.now[:danger] = t('.failure') render(:new) diff --git a/app/controllers/study_groups_controller.rb b/app/controllers/study_groups_controller.rb index 26cb7615..de845bae 100644 --- a/app/controllers/study_groups_controller.rb +++ b/app/controllers/study_groups_controller.rb @@ -39,7 +39,7 @@ class StudyGroupsController < ApplicationController def set_as_current session[:study_group_id] = @study_group.id current_user.store_current_study_group_id(@study_group.id) - redirect_back(fallback_location: root_path, notice: t('study_groups.set_as_current.success')) + redirect_back_or_to(root_path, notice: t('study_groups.set_as_current.success')) end def set_group diff --git a/config/initializers/monkey_patches.rb b/config/initializers/monkey_patches.rb index 61c405d5..86878bd1 100644 --- a/config/initializers/monkey_patches.rb +++ b/config/initializers/monkey_patches.rb @@ -17,3 +17,14 @@ module WillPaginate end end end + +# Sorcery is currently overwriting the redirect_back_or_to method, which has been introduced in Rails 7.0+ +# See https://github.com/Sorcery/sorcery/issues/296 +module Sorcery + module Controller + module InstanceMethods + define_method :sorcery_redirect_back_or_to, instance_method(:redirect_back_or_to) + remove_method :redirect_back_or_to + end + end +end