From a1c5f3ff7c8bf44e387544fd0e5f2541b4570628 Mon Sep 17 00:00:00 2001 From: Jamie Date: Wed, 26 Jun 2024 16:30:05 +0100 Subject: [PATCH] Consolidate mentor controllers --- .../placements/schools/mentors_controller.rb | 9 ++- .../support/schools/mentors_controller.rb | 71 +------------------ 2 files changed, 10 insertions(+), 70 deletions(-) diff --git a/app/controllers/placements/schools/mentors_controller.rb b/app/controllers/placements/schools/mentors_controller.rb index 3f44c904f..09441caa7 100644 --- a/app/controllers/placements/schools/mentors_controller.rb +++ b/app/controllers/placements/schools/mentors_controller.rb @@ -21,8 +21,8 @@ def destroy authorize @mentor_membership @mentor_membership.destroy! - redirect_to placements_school_mentors_path(@school) flash[:success] = t(".mentor_removed") + redirect_to_index_path end def new @@ -37,8 +37,9 @@ def check def create mentor_form.save! + flash[:success] = t(".mentor_added") - redirect_to placements_school_mentors_path(@school), flash: { success: t(".mentor_added") } + redirect_to_index_path end private @@ -73,4 +74,8 @@ def set_mentor def set_mentor_membership @mentor_membership = @mentor.mentor_memberships.find_by(school: @school) end + + def redirect_to_index_path + redirect_to placements_school_mentors_path(@school) + end end diff --git a/app/controllers/placements/support/schools/mentors_controller.rb b/app/controllers/placements/support/schools/mentors_controller.rb index 74cfdcfec..d7f9410e6 100644 --- a/app/controllers/placements/support/schools/mentors_controller.rb +++ b/app/controllers/placements/support/schools/mentors_controller.rb @@ -1,76 +1,11 @@ -class Placements::Support::Schools::MentorsController < Placements::ApplicationController - before_action :set_school - before_action :set_mentor, only: %i[show remove destroy] - before_action :set_mentor_membership, only: %i[remove destroy] - - def index - @pagy, @mentors = pagy(@school.mentors.order_by_full_name) - end - - def show; end - - def new - mentor_form - end - - def check - render :new unless mentor_form.valid? - rescue TeachingRecord::RestClient::TeacherNotFoundError - render :no_results - end - - def create - mentor_form.save! - - redirect_to placements_support_school_mentors_path(@school), flash: { success: t(".mentor_added") } - end - - def remove - if policy(@mentor_membership).destroy? - render "confirm_remove" - else - render "can_not_remove" - end - end - - def destroy - authorize @mentor_membership - @mentor_membership.destroy! - - redirect_to placements_support_school_mentors_path(@school) - flash[:success] = t(".mentor_removed") - end - +class Placements::Support::Schools::MentorsController < Placements::Schools::MentorsController private - def mentor_params - params.require(:placements_mentor_form) - .permit(:first_name, :last_name, :trn, :date_of_birth) - .merge(default_params) - end - - def default_params - { school: @school } - end - - def mentor_form - @mentor_form ||= - if params[:placements_mentor_form].present? - Placements::MentorForm.new(mentor_params) - else - Placements::MentorForm.new(default_params) - end - end - def set_school @school = Placements::School.find(params.fetch(:school_id)) end - def set_mentor - @mentor = @school.mentors.find(params.fetch(:id)) - end - - def set_mentor_membership - @mentor_membership = @mentor.mentor_memberships.find_by(school: @school) + def redirect_to_index_path + redirect_to placements_support_school_mentors_path(@school) end end