-
Notifications
You must be signed in to change notification settings - Fork 245
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
Allow deletion of Assignment's with no groups #6880
Changes from 25 commits
22ad13b
e6fdc5e
5fd31f2
fe7dfd6
351a03a
fdef002
be27f1d
e74d214
3b89441
c6aa66d
d69dbb4
0aadea0
b183f66
d2052e5
83170fc
e539b81
0e1d8b3
742ba72
b2cb58b
23da554
ead7c57
8d8a896
70ab805
05f3734
b612a5a
3779d5e
23e21d3
90603ac
6540253
ae96585
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -299,6 +299,24 @@ def submit_file | |
upload_file(grouping, only_required_files: assignment.only_required_files) | ||
end | ||
|
||
def destroy | ||
assignment = record | ||
if assignment.nil? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check isn't necessary, you can remove it. The |
||
render 'shared/http_status', locals: { code: '404', message: | ||
I18n.t('assignments.assignment_not_found', | ||
invalid_id: assignment_id) }, status: :not_found | ||
else | ||
begin | ||
assignment.destroy | ||
render 'shared/http_status', | ||
locals: { code: '200', message: I18n.t('assignments.successful_deletion') }, status: :ok | ||
rescue ActiveRecord::DeleteRestrictionError | ||
render 'shared/http_status', | ||
locals: { code: :conflict, message: I18n.t('assignments.assignment_has_groupings') }, status: :conflict | ||
end | ||
end | ||
end | ||
|
||
protected | ||
|
||
def implicit_authorization_target | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -657,6 +657,17 @@ def lti_settings | |
render layout: 'assignment_content' | ||
end | ||
|
||
def destroy | ||
@assignment = @record | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
begin | ||
@assignment.destroy | ||
respond_with @assignment, location: -> { course_assignments_path(current_course, @assignment) } | ||
rescue ActiveRecord::DeleteRestrictionError | ||
flash_message(:error, I18n.t('assignments.assignment_has_groupings')) | ||
redirect_back fallback_location: { action: :edit, id: @assignment.id } | ||
end | ||
end | ||
|
||
private | ||
|
||
# Configures the automated test files and settings for an +assignment+ provided in the +zip_file+ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -489,4 +489,22 @@ | |
data: { disable_with: t('working') } %> | ||
</p> | ||
<% end %> | ||
|
||
<% if (action_name=="edit") %> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If statements don't need parentheses in Ruby. Also, put spaces around |
||
<% has_groups = @assignment.groups.length != 0 %> | ||
<%= button_to t(:delete), | ||
course_assignment_path(@current_course, @assignment), | ||
{ | ||
data: { confirm: "Are you sure you want to delete this assignment?" }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This string also needs to be internationalized |
||
method: 'delete', | ||
class: has_groups ? nil : 'button', | ||
id: has_groups ? nil : 'assignment-delete', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Following my above comment, don't use an |
||
form_class: 'display-inline-block', | ||
title: has_groups ? I18n.t('assignments.assignment_has_groupings') : | ||
I18n.t('helpers.submit.delete', model: Assignment.model_name.human), | ||
disabled: has_groups | ||
} | ||
%> | ||
<%end %> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a space missing after the |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. delete this line There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line hasn't been deleted |
||
<% end %> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -188,6 +188,11 @@ | |
put :update, params: { id: assignment.id, course_id: course.id } | ||
expect(response).to have_http_status(403) | ||
end | ||
|
||
it 'should fail to authenticate a DELETE destroy request' do | ||
delete :destroy, params: { id: assignment.id, course_id: course.id } | ||
expect(response).to have_http_status(403) | ||
end | ||
end | ||
|
||
context 'An authenticated instructor request requesting' do | ||
|
@@ -502,6 +507,35 @@ | |
expect(response).to have_http_status(403) | ||
end | ||
end | ||
context 'DELETE assignment' do | ||
it 'should successfully delete assignment because the assignment has no groups' do | ||
assignment # since lazy let is used for creating an assignment, I invoke it here to trigger its execution | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't necessary, as |
||
expect(assignment.groups).to be_empty | ||
delete :destroy, params: { id: assignment.id, course_id: course.id } | ||
expect(response).to have_http_status(200) | ||
expect(Assignment.exists?(assignment.id)).to eq(false) | ||
end | ||
it 'fails to delete assignment because assignment has groups' do | ||
assignment # since lazy let is used for creating an assignment, I invoke it here to trigger its execution | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above |
||
# creates a grouping (and thus a group) for the assignment | ||
create :grouping, assignment: assignment, start_time: nil | ||
expect(assignment.groups).to_not be_empty | ||
original_size = Assignment.all.length | ||
delete :destroy, params: { id: assignment.id, course_id: course.id } | ||
expect(response).to have_http_status(409) | ||
expect(Assignment.all.length).to eq(original_size) | ||
expect(assignment.persisted?).to eq(true) | ||
end | ||
it 'fails to delete assignment because of invalid id' do | ||
assignment # since lazy let is used for creating an assignment, I invoke it here to trigger its execution | ||
original_size = Assignment.all.length | ||
# Since we only have one assignment, it is guaranteed that assignment.id + 1 is an invalid id | ||
delete :destroy, params: { id: assignment.id + 1, course_id: course.id } | ||
expect(response).to have_http_status(404) | ||
expect(Assignment.all.length).to eq(original_size) | ||
expect(assignment.persisted?).to eq(true) | ||
end | ||
end | ||
end | ||
|
||
context 'An authenticated student request' do | ||
|
@@ -670,6 +704,13 @@ | |
end | ||
end | ||
end | ||
|
||
context 'DELETE destroy' do | ||
it 'should fail to authenticate a DELETE destroy request' do | ||
delete :destroy, params: { id: assignment.id, course_id: course.id } | ||
expect(response).to have_http_status(403) | ||
end | ||
end | ||
end | ||
|
||
context 'An authenticated ta request' do | ||
|
@@ -704,5 +745,12 @@ | |
end | ||
end | ||
end | ||
|
||
context 'DELETE destroy' do | ||
it 'should fail to authenticate a DELETE destroy request' do | ||
delete :destroy, params: { id: assignment.id, course_id: course.id } | ||
expect(response).to have_http_status(403) | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay this is better, but (1) make it a class, and (2) call the class
danger-button
, which is more general.