From 45c640c58f517b5e313d35e2cc4fd2a12369b795 Mon Sep 17 00:00:00 2001 From: kiragrammel Date: Thu, 14 Sep 2023 18:51:41 +0200 Subject: [PATCH] Add destroy route for programming groups --- app/controllers/programming_groups_controller.rb | 10 ++++++++++ app/models/programming_group.rb | 6 +++--- app/policies/programming_group_policy.rb | 12 ++++++++---- app/views/programming_groups/index.html.slim | 2 ++ config/routes.rb | 2 +- 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/app/controllers/programming_groups_controller.rb b/app/controllers/programming_groups_controller.rb index de793f60b..671b8ed46 100644 --- a/app/controllers/programming_groups_controller.rb +++ b/app/controllers/programming_groups_controller.rb @@ -5,6 +5,7 @@ class ProgrammingGroupsController < ApplicationController include LtiHelper before_action :set_exercise_and_authorize, only: %i[new create] + before_action :set_programming_group, only: %i[destroy] def index @programming_groups = ProgrammingGroup.all.paginate(page: params[:page], per_page: per_page_param) @@ -52,6 +53,11 @@ def create end end + def destroy + authorize! + destroy_and_respond(object: @programming_group) + end + private def authorize! @@ -67,6 +73,10 @@ def set_exercise_and_authorize authorize(@exercise, :implement?) end + def set_programming_group + @programming_group = ProgrammingGroup.find(params[:id]) + end + def redirect_to_exercise skip_authorization redirect_to(implement_exercise_path(@exercise), diff --git a/app/models/programming_group.rb b/app/models/programming_group.rb index fd0368176..efa0b2733 100644 --- a/app/models/programming_group.rb +++ b/app/models/programming_group.rb @@ -9,9 +9,9 @@ class ProgrammingGroup < ApplicationRecord has_many :internal_users, through: :programming_group_memberships, source_type: 'InternalUser', source: :user has_many :testruns, through: :submissions has_many :runners, as: :contributor, dependent: :destroy - has_many :events - has_many :events_synchronized_editor, class_name: 'Event::SynchronizedEditor' - has_many :pair_programming_exercise_feedbacks + has_many :events, dependent: :destroy + has_many :events_synchronized_editor, class_name: 'Event::SynchronizedEditor', dependent: :destroy + has_many :pair_programming_exercise_feedbacks, dependent: :destroy belongs_to :exercise validate :min_group_size diff --git a/app/policies/programming_group_policy.rb b/app/policies/programming_group_policy.rb index 2e14043e5..4c3b3efb5 100644 --- a/app/policies/programming_group_policy.rb +++ b/app/policies/programming_group_policy.rb @@ -1,15 +1,19 @@ # frozen_string_literal: true class ProgrammingGroupPolicy < ApplicationPolicy - def index? + def create? + everyone + end + + def destroy? admin? end - def new? - everyone + def index? + admin? end - def create? + def new? everyone end diff --git a/app/views/programming_groups/index.html.slim b/app/views/programming_groups/index.html.slim index 4ea45c768..725a2ebbe 100644 --- a/app/views/programming_groups/index.html.slim +++ b/app/views/programming_groups/index.html.slim @@ -12,10 +12,12 @@ h1 = ProgrammingGroup.model_name.human(count: 2) th = t('activerecord.attributes.programming_group.programming_group_id') th = t('activerecord.attributes.programming_group.exercise_title') th = t('activerecord.attributes.programming_group.participants') + th = t('shared.actions') tbody - @programming_groups.each do |programming_group| tr td = programming_group.displayname td = link_to_if(policy(programming_group.exercise).show?, programming_group.exercise.title, programming_group.exercise, 'data-turbolinks' => "false") td == programming_group.users.map { |user| link_to_if(policy(user).show?, user.name, user) }.join(', ') + td = link_to(t('shared.destroy'), programming_group, data: { confirm: t('shared.confirm_destroy') }, method: :delete) if policy(programming_group).destroy? = render('shared/pagination', collection: @programming_groups) diff --git a/config/routes.rb b/config/routes.rb index b0c51f66a..57d1a1c93 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -100,7 +100,7 @@ resources :programming_groups, only: %i[new create] end - resources :programming_groups, only: %i[index] + resources :programming_groups, only: %i[index destroy] resources :exercise_collections do member do