Skip to content

Commit

Permalink
Add destroy route for programming groups
Browse files Browse the repository at this point in the history
  • Loading branch information
kiragrammel committed Sep 15, 2023
1 parent 20947b4 commit 45c640c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
10 changes: 10 additions & 0 deletions app/controllers/programming_groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -52,6 +53,11 @@ def create
end
end

def destroy
authorize!
destroy_and_respond(object: @programming_group)
end

private

def authorize!
Expand All @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions app/models/programming_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions app/policies/programming_group_policy.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 2 additions & 0 deletions app/views/programming_groups/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 45c640c

Please sign in to comment.