diff --git a/app/controllers/evaluator_submission_assignments_controller.rb b/app/controllers/evaluator_submission_assignments_controller.rb index e0bcfc80..ab89070b 100644 --- a/app/controllers/evaluator_submission_assignments_controller.rb +++ b/app/controllers/evaluator_submission_assignments_controller.rb @@ -6,32 +6,26 @@ class EvaluatorSubmissionAssignmentsController < ApplicationController before_action :set_assignment, only: [:update] def index - @evaluator_assignments = @phase.evaluator_submission_assignments - .includes(:submission) - .where(user_id: @evaluator.id) - @assigned_submissions = @evaluator_assignments.where(status: [:completed, :in_progress, :not_started, :recused]) - .ordered_by_status - @unassigned_submissions = @evaluator_assignments.where(status: [:unassigned, :recused_unassigned]) - .ordered_by_status - @submissions_count = @assigned_submissions.group(:status).count + @evaluator_assignments = @phase.evaluator_submission_assignments. + includes(:submission). + where(user_id: @evaluator.id) + @assigned_submissions = @evaluator_assignments. + where(status: %i[completed in_progress not_started recused]). + ordered_by_status + @unassigned_submissions = @evaluator_assignments. + where(status: %i[:unassigned, :recused_unassigned]). + ordered_by_status + @submissions_count = @assigned_submissions.group('evaluator_submission_assignments.status').count end # update only the status of the evaluation submission assignment to unassign or reassign an evaluator def update new_status = params[:status]&.to_sym - if @assignment.update(status: EvaluatorSubmissionAssignment.statuses[new_status]) - flash[:success] = t("evaluator_submission_assignments.#{new_status}.success") - respond_to do |format| - format.html { redirect_to phase_evaluator_submission_assignments_path(@phase, evaluator_id: params[:evaluator_id]) } - format.json { render json: { success: true, message: flash[:success] } } - end + if update_assignment_status(new_status) + handle_successful_update(new_status) else - flash[:error] = t("evaluator_submission_assignments.#{new_status}.failure") - respond_to do |format| - format.html { redirect_to phase_evaluator_submission_assignments_path(@phase, evaluator_id: params[:evaluator_id]) } - format.json { render json: { success: false, message: flash[:error] }, status: :unprocessable_entity } - end + handle_failed_update(new_status) end end @@ -47,6 +41,36 @@ def set_evaluator end def set_assignment - @assignment = EvaluatorSubmissionAssignment.find_by!(user_id: params[:evaluator_id], submission_id: params[:submission_id]) + @assignment = EvaluatorSubmissionAssignment.find_by!( + user_id: params[:evaluator_id], + submission_id: params[:submission_id] + ) + end + + def update_assignment_status(new_status) + @assignment.update(status: EvaluatorSubmissionAssignment.statuses[new_status]) + end + + def handle_successful_update(new_status) + flash[:success] = t("evaluator_submission_assignments.#{new_status}.success") + respond_to do |format| + format.html { redirect_to_assignment_path } + format.json { render json: { success: true, message: flash[:success] } } + end + end + + def handle_failed_update(new_status) + flash[:error] = t("evaluator_submission_assignments.#{new_status}.failure") + respond_to do |format| + format.html { redirect_to_assignment_path } + format.json { render json: { success: false, message: flash[:error] }, status: :unprocessable_entity } + end + end + + def redirect_to_assignment_path + redirect_to phase_evaluator_submission_assignments_path( + @phase, + evaluator_id: params[:evaluator_id] + ) end end diff --git a/app/javascript/controllers/unassign_evaluator_submission_modal_controller.js b/app/javascript/controllers/unassign_evaluator_submission_modal_controller.js index c7ac2fc2..526db01d 100644 --- a/app/javascript/controllers/unassign_evaluator_submission_modal_controller.js +++ b/app/javascript/controllers/unassign_evaluator_submission_modal_controller.js @@ -18,12 +18,11 @@ export default class extends Controller { open(event) { event.preventDefault(); - this.submissionIdValue = event.currentTarget.dataset.submissionId; - this.evaluatorIdValue = event.currentTarget.dataset.evaluatorId; - this.phaseIdValue = event.currentTarget.dataset.phaseId; + this.setValues(event.currentTarget.dataset); this.modalTarget.showModal(); } + close() { this.modalTarget.close() } @@ -38,6 +37,12 @@ export default class extends Controller { this.unassignEvaluatorSubmission() } + setValues(dataset) { + this.submissionIdValue = dataset.submissionId; + this.evaluatorIdValue = dataset.evaluatorId; + this.phaseIdValue = dataset.phaseId; + } + unassignEvaluatorSubmission() { const csrfToken = document.querySelector('meta[name="csrf-token"]').content; diff --git a/app/models/evaluator_submission_assignment.rb b/app/models/evaluator_submission_assignment.rb index 297a1171..ca54c0eb 100644 --- a/app/models/evaluator_submission_assignment.rb +++ b/app/models/evaluator_submission_assignment.rb @@ -16,15 +16,17 @@ class EvaluatorSubmissionAssignment < ApplicationRecord recused_unassigned: 6 } - STATUS_ORDER = [:recused, :unassigned, :recused_unassigned, :not_started, :in_progress, :completed] + STATUS_ORDER = %i[recused unassigned recused_unassigned not_started in_progress completed].freeze - scope :ordered_by_status, -> { - order(Arel.sql( - "CASE " + - STATUS_ORDER.map.with_index { |status, index| - "WHEN evaluator_submission_assignments.status = #{statuses[status]} THEN #{index + 1}" - }.join(" ") + - " ELSE #{STATUS_ORDER.length + 1} END" - )) + scope :ordered_by_status, lambda { + order( + Arel.sql( + [ + "CASE status", + *STATUS_ORDER.map.with_index { |status, index| "WHEN #{statuses[status]} THEN #{index}" }, + "ELSE #{STATUS_ORDER.length} END" + ].join(" ") + ) + ) } end