Skip to content

Commit

Permalink
Small updates
Browse files Browse the repository at this point in the history
  • Loading branch information
emmabjj committed Nov 22, 2024
1 parent e7882bf commit 9c89936
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 32 deletions.
64 changes: 44 additions & 20 deletions app/controllers/evaluator_submission_assignments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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;

Expand Down
20 changes: 11 additions & 9 deletions app/models/evaluator_submission_assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9c89936

Please sign in to comment.