Refactor user_exercise_feedbacks_controller
This commit is contained in:
@ -1,22 +1,25 @@
|
|||||||
|
# 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')],
|
||||||
[1,t('user_exercise_feedback.difficulty_some_what_easy')],
|
[1, t('user_exercise_feedback.difficulty_some_what_easy')],
|
||||||
[2,t('user_exercise_feedback.difficulty_ok')],
|
[2, t('user_exercise_feedback.difficulty_ok')],
|
||||||
[3,t('user_exercise_feedback.difficulty_some_what_difficult')],
|
[3, t('user_exercise_feedback.difficulty_some_what_difficult')],
|
||||||
[4,t('user_exercise_feedback.difficult_too_difficult')]]
|
[4, t('user_exercise_feedback.difficult_too_difficult')]]
|
||||||
end
|
end
|
||||||
|
|
||||||
def time_presets
|
def time_presets
|
||||||
[[0,t('user_exercise_feedback.estimated_time_less_5')],
|
[[0, t('user_exercise_feedback.estimated_time_less_5')],
|
||||||
[1,t('user_exercise_feedback.estimated_time_5_to_10')],
|
[1, t('user_exercise_feedback.estimated_time_5_to_10')],
|
||||||
[2,t('user_exercise_feedback.estimated_time_10_to_20')],
|
[2, t('user_exercise_feedback.estimated_time_10_to_20')],
|
||||||
[3,t('user_exercise_feedback.estimated_time_20_to_30')],
|
[3, t('user_exercise_feedback.estimated_time_20_to_30')],
|
||||||
[4,t('user_exercise_feedback.estimated_time_more_30')]]
|
[4, t('user_exercise_feedback.estimated_time_more_30')]]
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@ -24,25 +27,29 @@ 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)
|
||||||
|
@uef.update_attributes(uef_params)
|
||||||
|
authorize!
|
||||||
if validate_inputs(uef_params)
|
if validate_inputs(uef_params)
|
||||||
authorize!
|
|
||||||
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)
|
||||||
else
|
else
|
||||||
implement_exercise_path(@exercise)
|
implement_exercise_path(@exercise)
|
||||||
end
|
end
|
||||||
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,35 +58,39 @@ 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)
|
||||||
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)
|
||||||
else
|
else
|
||||||
implement_exercise_path(@exercise)
|
implement_exercise_path(@exercise)
|
||||||
end
|
end
|
||||||
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
|
true
|
||||||
return true
|
|
||||||
end
|
|
||||||
rescue
|
|
||||||
return false
|
|
||||||
end
|
end
|
||||||
|
rescue StandardError
|
||||||
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user