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

View File

@ -1,8 +1,4 @@
class UserExerciseFeedbackPolicy < ApplicationPolicy class UserExerciseFeedbackPolicy < AdminOrAuthorPolicy
def author?
@user == @record.author
end
private :author?
def create? def create?
everyone everyone
@ -12,8 +8,4 @@ class UserExerciseFeedbackPolicy < ApplicationPolicy
everyone everyone
end end
[:show? ,:destroy?, :edit?, :update?].each do |action|
define_method(action) { admin? || author?}
end
end end

View File

@ -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)

View File

@ -116,6 +116,9 @@ de:
name: "Name" name: "Name"
updated_at: "Letzte Änderung" updated_at: "Letzte Änderung"
exercises: "Aufgaben" exercises: "Aufgaben"
user_exercise_feedback:
user: "Autor"
exercise: "Aufgabe"
models: models:
code_harbor_link: code_harbor_link:
one: CodeHarbor-Link one: CodeHarbor-Link

View File

@ -116,6 +116,9 @@ en:
name: "Name" name: "Name"
updated_at: "Last Update" updated_at: "Last Update"
exercises: "Exercises" exercises: "Exercises"
user_exercise_feedback:
user: "Author"
exercise: "Exercise"
models: models:
code_harbor_link: code_harbor_link:
one: CodeHarbor Link one: CodeHarbor Link