Skip to content

Commit

Permalink
Consolidate mentor controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie authored and JamieCleare2525 committed Jun 27, 2024
1 parent 7fd5a31 commit a1c5f3f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 70 deletions.
9 changes: 7 additions & 2 deletions app/controllers/placements/schools/mentors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
71 changes: 3 additions & 68 deletions app/controllers/placements/support/schools/mentors_controller.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a1c5f3f

Please sign in to comment.