Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

destroy deployments feature #684

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions app/controllers/deployments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
class DeploymentsController < ApplicationController
include SelectionParams

before_action :authenticate_reader!, only: %i[index show new create]
before_action :authenticate_reader!, only: %i[index show new create destroy]
before_action :set_deployments, only: %i[index new create]
before_action :set_deployment, only: %i[show edit update]
before_action :set_deployment, only: %i[show edit update destroy]
after_action :clear_content_item_selection_params, only: %i[update]

layout 'admin'
Expand Down Expand Up @@ -63,6 +63,15 @@ def update
end
end

def destroy
authorize @deployment
if @deployment.destroy
redirect_to deployments_url, notice: 'Successfully deleted deployment'
else
redirect_to deployments_url, alert: 'Failed to delete deployment'
end
end

private

def set_deployments
Expand Down
20 changes: 17 additions & 3 deletions app/models/deployment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@
class Deployment < ApplicationRecord
default_scope { order created_at: :desc }

after_destroy :cleanup

attribute :answers_needed, :integer, default: 0
attribute :key, :string, default: -> { SecureRandom.urlsafe_base64 }

belongs_to :case
belongs_to :group
belongs_to :quiz, optional: true
belongs_to :quiz, optional: true, dependent: :destroy

has_many :readers, through: :group
has_many :enrollments, ->(this) { where(reader: this.readers) },
through: :case

has_one :community, through: :group
has_one :forum, ->(this) { where case: this.case },
through: :community, source: :forums
through: :community, source: :forums, dependent: :destroy

accepts_nested_attributes_for :group

Expand Down Expand Up @@ -68,4 +70,16 @@ def reader_needs_posttest?(reader)
return false unless quiz
answers_needed - quiz.number_of_responses_from(reader) >= 1
end
end

def cleanup
# Readers belong to an active community which is set at deployment creation with a db constraint
# against the community. This value must be nulled out first
Reader.where(active_community_id: self.community.id).update_all({active_community_id: nil})

Enrollment.where(case_id: self.case.id).destroy_all

if self.group.deployments.size == 0
self.group.destroy
end
end
end
4 changes: 4 additions & 0 deletions app/policies/deployment_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def update?
show?
end

def destroy?
show?
end

private

def selection_params_valid?
Expand Down
5 changes: 5 additions & 0 deletions app/views/deployments/_deployment.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
>
<%= t 'helpers.close' %>
</button>

<%= link_to deployment, class: "pt-button pt-small pt-minimal pt-intent-danger pt-icon-trash", method: :delete, :data => {:confirm => "#{t '.are_you_sure'}"} do %>
<%= t '.destroy' %>
<% end %>

</div>
</div>

Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,8 @@ en:
invite_link: |
Invite link for the deployment of the case “%{case_kicker}”
in the group “%{group_name}”
destroy: Delete
are_you_sure: Are you sure you want to permanently delete this deployment?
enrolled_learners:
case_completion: Percent of Case Completed
posttest: Posttest Score
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@

resources :comments, only: %i[update destroy]

resources :deployments, only: %i[index show new create edit update] do
resources :deployments do
resources :submissions, only: %i[index]
end

Expand Down
17 changes: 17 additions & 0 deletions spec/policies/deployment_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,21 @@
expect(subject).to permit editor, deployment
end
end

permissions :destroy? do
it 'does not allow an arbitrary reader to destroy a deployment' do
expect(subject).not_to permit reader_context, deployment
end

it 'allows an instructor to destroy her own deployment' do
create :group_membership, group: deployment.group,
reader: reader_context.reader,
status: :admin
expect(subject).to permit reader_context, deployment
end

it 'allows an editor to destroy any deployment' do
expect(subject).to permit editor, deployment
end
end
end
26 changes: 26 additions & 0 deletions spec/requests/deployment_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe "/deployments", type: :request do
it "destroys a deployment" do
kase = create :case
reader = create :reader, :editor
create :enrollment, case: kase, reader: reader
reader.my_cases << kase

my_group = create :group, name: 'My Group'
create :group_membership, :admin, group: my_group, reader: reader
@deployment = create :deployment, :with_quiz, group: my_group, case: kase

sign_in reader

expect{delete deployment_url(@deployment)}.to change(Deployment, :count).by(-1)
.and change(Group, :count).by(-1)
.and change(Enrollment, :count).by(-1)

expect(reader.active_community_id).to be_a_kind_of(Integer)
reader.reload
expect(reader.active_community_id).to be(nil)
end
end