Refactor user_exercise_feedbacks_controller

This commit is contained in:
Sebastian Serth
2020-10-28 20:02:01 +01:00
parent 9cd8ca2013
commit 4284645bfa

View File

@ -1,7 +1,10 @@
# frozen_string_literal: true
class UserExerciseFeedbacksController < ApplicationController class UserExerciseFeedbacksController < ApplicationController
include CommonBehavior include CommonBehavior
before_action :set_user_exercise_feedback, only: [:edit, :update, :destroy] before_action :set_user_exercise_feedback, only: %i[edit update destroy]
before_action :set_presets, only: %i[new edit create update]
def comment_presets def comment_presets
[[0, t('user_exercise_feedback.difficulty_easy')], [[0, t('user_exercise_feedback.difficulty_easy')],
@ -24,12 +27,17 @@ class UserExerciseFeedbacksController < ApplicationController
@exercise = Exercise.find(uef_params[:exercise_id]) @exercise = Exercise.find(uef_params[:exercise_id])
rfc = RequestForComment.unsolved.where(exercise_id: @exercise.id, user_id: current_user.id).first rfc = RequestForComment.unsolved.where(exercise_id: @exercise.id, user_id: current_user.id).first
submission = current_user.submissions.where(exercise_id: @exercise.id).order('created_at DESC').first rescue nil submission = begin
current_user.submissions.where(exercise_id: @exercise.id).order('created_at DESC').first
rescue StandardError
nil
end
if @exercise if @exercise
@uef = UserExerciseFeedback.new(uef_params) @uef = UserExerciseFeedback.find_or_initialize_by(user: current_user, exercise: @exercise)
if validate_inputs(uef_params) @uef.update_attributes(uef_params)
authorize! authorize!
if validate_inputs(uef_params)
path = path =
if rfc && submission && submission.normalized_score == 1.0 if rfc && submission && submission.normalized_score == 1.0
request_for_comment_path(rfc) request_for_comment_path(rfc)
@ -39,10 +47,9 @@ class UserExerciseFeedbacksController < ApplicationController
create_and_respond(object: @uef, path: proc { path }) create_and_respond(object: @uef, path: proc { path })
else else
flash[:danger] = t('shared.message_failure') flash[:danger] = t('shared.message_failure')
redirect_to(:back, id: uef_params[:exercise_id]) redirect_back fallback_location: user_exercise_feedback_path(@uef)
end end
end end
end end
def destroy def destroy
@ -51,22 +58,26 @@ class UserExerciseFeedbacksController < ApplicationController
end end
def edit def edit
@texts = comment_presets.to_a
@times = time_presets.to_a
authorize! authorize!
end end
def new def new
@texts = comment_presets.to_a exercise_id = if params[:user_exercise_feedback].nil?
@times = time_presets.to_a params[:exercise_id]
@uef = UserExerciseFeedback.new else
exercise_id = if params[:user_exercise_feedback].nil? then params[:exercise_id] else params[:user_exercise_feedback][:exercise_id] end params[:user_exercise_feedback][:exercise_id]
end
@exercise = Exercise.find(exercise_id) @exercise = Exercise.find(exercise_id)
@uef = UserExerciseFeedback.find_or_initialize_by(user: current_user, exercise: @exercise)
authorize! authorize!
end end
def update def update
submission = current_user.submissions.where(exercise_id: @exercise.id).order('created_at DESC').first rescue nil submission = begin
current_user.submissions.where(exercise_id: @exercise.id).order('created_at DESC').first
rescue StandardError
nil
end
rfc = RequestForComment.unsolved.where(exercise_id: @exercise.id, user_id: current_user.id).first rfc = RequestForComment.unsolved.where(exercise_id: @exercise.id, user_id: current_user.id).first
authorize! authorize!
if @exercise && validate_inputs(uef_params) if @exercise && validate_inputs(uef_params)
@ -79,7 +90,7 @@ class UserExerciseFeedbacksController < ApplicationController
update_and_respond(object: @uef, params: uef_params, path: path) update_and_respond(object: @uef, params: uef_params, path: path)
else else
flash[:danger] = t('shared.message_failure') flash[:danger] = t('shared.message_failure')
redirect_to(:back, id: uef_params[:exercise_id]) redirect_back fallback_location: user_exercise_feedback_path(@uef)
end end
end end
@ -98,6 +109,11 @@ class UserExerciseFeedbacksController < ApplicationController
@exercise = @uef.exercise @exercise = @uef.exercise
end end
def set_presets
@texts = comment_presets.to_a
@times = time_presets.to_a
end
def uef_params def uef_params
return unless params[:user_exercise_feedback].present? return unless params[:user_exercise_feedback].present?
@ -122,17 +138,14 @@ class UserExerciseFeedbacksController < ApplicationController
end end
def validate_inputs(uef_params) def validate_inputs(uef_params)
begin if uef_params[:difficulty].to_i.negative? || uef_params[:difficulty].to_i >= comment_presets.size
if uef_params[:difficulty].to_i < 0 || uef_params[:difficulty].to_i >= comment_presets.size false
return false elsif uef_params[:user_estimated_worktime].to_i.negative? || uef_params[:user_estimated_worktime].to_i >= time_presets.size
elsif uef_params[:user_estimated_worktime].to_i < 0 || uef_params[:user_estimated_worktime].to_i >= time_presets.size false
return false
else else
return true true
end end
rescue rescue StandardError
return false false
end end
end end
end