Implement index action. Repair destroy

This commit is contained in:
Maximilian Grundke
2017-10-15 18:23:58 +02:00
parent e52c9213a1
commit d353dbaf5b
5 changed files with 52 additions and 20 deletions

View File

@ -1,7 +1,8 @@
class UserExerciseFeedbacksController < ApplicationController
include CommonBehavior
before_action :set_user_exercise_feedback, only: [:edit, :update]
before_action :set_user_exercise_feedback, only: [:show, :edit, :update]
before_action :set_user_exercise_feedback_by_id, only: [:destroy]
def comment_presets
[[0,t('user_exercise_feedback.difficulty_easy')],
@ -19,10 +20,14 @@ class UserExerciseFeedbacksController < ApplicationController
[4,t('user_exercise_feedback.estimated_time_more_30')]]
end
def authorize!
authorize(@uef)
def index
@uefs = UserExerciseFeedback.order(:id).paginate(page: params[:page])
authorize!
end
def show
authorize!
end
private :authorize!
def create
@exercise = Exercise.find(uef_params[:exercise_id])
@ -49,7 +54,8 @@ class UserExerciseFeedbacksController < ApplicationController
end
def destroy
destroy_and_respond(object: @tag)
authorize!
destroy_and_respond(object: @uef)
end
def edit
@ -58,11 +64,6 @@ class UserExerciseFeedbacksController < ApplicationController
authorize!
end
def uef_params
params[:user_exercise_feedback].permit(:feedback_text, :difficulty, :exercise_id, :user_estimated_worktime).merge(user_id: current_user.id, user_type: current_user.class.name)
end
private :uef_params
def new
@texts = comment_presets.to_a
@times = time_presets.to_a
@ -89,6 +90,12 @@ class UserExerciseFeedbacksController < ApplicationController
end
end
private
def authorize!
authorize(@uef || @uefs)
end
def to_s
name
end
@ -98,6 +105,14 @@ class UserExerciseFeedbacksController < ApplicationController
@uef = UserExerciseFeedback.find_by(exercise_id: params[:user_exercise_feedback][:exercise_id], user: current_user)
end
def set_user_exercise_feedback_by_id
@uef = UserExerciseFeedback.find(params[:id])
end
def uef_params
params[:user_exercise_feedback].permit(:feedback_text, :difficulty, :exercise_id, :user_estimated_worktime).merge(user_id: current_user.id, user_type: current_user.class.name)
end
def validate_inputs(uef_params)
begin
if uef_params[:difficulty].to_i < 0 || uef_params[:difficulty].to_i >= comment_presets.size
@ -112,4 +127,4 @@ class UserExerciseFeedbacksController < ApplicationController
end
end
end
end