diff --git a/app/controllers/user_exercise_feedbacks_controller.rb b/app/controllers/user_exercise_feedbacks_controller.rb index 0d1e1925..6ce02da7 100644 --- a/app/controllers/user_exercise_feedbacks_controller.rb +++ b/app/controllers/user_exercise_feedbacks_controller.rb @@ -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 \ No newline at end of file +end diff --git a/app/policies/user_exercise_feedback_policy.rb b/app/policies/user_exercise_feedback_policy.rb index 20a89a6e..a570a28c 100644 --- a/app/policies/user_exercise_feedback_policy.rb +++ b/app/policies/user_exercise_feedback_policy.rb @@ -1,8 +1,4 @@ -class UserExerciseFeedbackPolicy < ApplicationPolicy - def author? - @user == @record.author - end - private :author? +class UserExerciseFeedbackPolicy < AdminOrAuthorPolicy def create? everyone @@ -12,8 +8,4 @@ class UserExerciseFeedbackPolicy < ApplicationPolicy everyone end - [:show? ,:destroy?, :edit?, :update?].each do |action| - define_method(action) { admin? || author?} - end - end diff --git a/app/views/user_exercise_feedbacks/index.html.slim b/app/views/user_exercise_feedbacks/index.html.slim new file mode 100644 index 00000000..84f2a15f --- /dev/null +++ b/app/views/user_exercise_feedbacks/index.html.slim @@ -0,0 +1,19 @@ +h1 = UserExerciseFeedback.model_name.human(count: 2) + +.table-responsive + table.table + thead + tr + th colspan=2 = t('activerecord.attributes.user_exercise_feedback.user') + th = t('activerecord.attributes.user_exercise_feedback.exercise') + th colspan=2 = t('shared.actions') + tbody + - @uefs.each do |uef| + tr + td = uef.user.id + td = uef.user.name + td = link_to(uef.exercise.title, uef.exercise) + td = link_to(t('shared.show'), uef) + td = link_to(t('shared.destroy'), uef, data: {confirm: t('shared.confirm_destroy')}, method: :delete) + += render('shared/pagination', collection: @uefs) diff --git a/config/locales/de.yml b/config/locales/de.yml index 1e2b2337..523259e6 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -116,6 +116,9 @@ de: name: "Name" updated_at: "Letzte Änderung" exercises: "Aufgaben" + user_exercise_feedback: + user: "Autor" + exercise: "Aufgabe" models: code_harbor_link: one: CodeHarbor-Link diff --git a/config/locales/en.yml b/config/locales/en.yml index c55c90b0..4f4e0768 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -116,6 +116,9 @@ en: name: "Name" updated_at: "Last Update" exercises: "Exercises" + user_exercise_feedback: + user: "Author" + exercise: "Exercise" models: code_harbor_link: one: CodeHarbor Link