-
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
association-improvement #6989
association-improvement #6989
Conversation
3fdde26
to
f7308c6
Compare
Pull Request Test Coverage Report for Build 8486266995Details
💛 - Coveralls |
app/models/assignment.rb
Outdated
has_many :current_submissions_used, | ||
through: :groupings, | ||
source: :current_submission_used, | ||
dependent: :destroy |
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.
Don't add this to through
associations (it's either redundant or misleading: in this case, for example, groupings
can't be destroyed, and so current_submissions_used
will also never be destroyed)
@@ -2065,5 +2075,28 @@ | |||
expect(flash.to_hash.length).to eq(1) | |||
expect(response).to have_http_status(302) | |||
end | |||
it 'should remove associated entities on destroy' do | |||
# These were lazily initialized, so in order to actually be created we need to "use" them | |||
checkbox_criterion |
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.
Create a separate test case for each of these
@@ -667,6 +667,9 @@ def destroy | |||
rescue ActiveRecord::DeleteRestrictionError | |||
flash_message(:error, I18n.t('assignments.assignment_has_groupings')) | |||
redirect_back fallback_location: { action: :edit, id: @assignment.id } | |||
rescue StandardError => e | |||
flash_message(:error, "Problem with deletion: #{e.message}") |
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.
make sure to add a test case that covers these two lines of code
Also, make sure to internationalize this string
app/models/assignment.rb
Outdated
|
||
has_many :notes, as: :noteable, dependent: :destroy | ||
|
||
has_many :exam_templates, dependent: :destroy, inverse_of: :assignment, foreign_key: :assessment_id | ||
|
||
has_many :starter_file_groups, dependent: :destroy, inverse_of: :assignment, foreign_key: :assessment_id | ||
|
||
has_many :tas, -> { distinct }, through: :ta_memberships, source: :role | ||
has_many :tas, -> { distinct }, through: :ta_memberships, source: :role, dependent: :destroy |
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.
There shouldn't be a dependent: :destroy
here
app/models/assignment.rb
Outdated
has_many :peer_reviews, through: :groupings | ||
has_many :pr_peer_reviews, through: :parent_assignment, source: :peer_reviews | ||
|
||
has_many :current_submissions_used, through: :groupings, | ||
source: :current_submission_used | ||
has_many :current_submissions_used, |
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.
you can revert the changes here (they are now purely stylistic since you removed the dependent: :destroy
# Deleting the assignment - should be successful since there are not groupings | ||
delete_as instructor, :destroy, params: { course_id: course.id, id: assignment.id } | ||
expect(Assignment.exists?(assignment.id)).to be(false) | ||
# Ensure that the associated checkbox_criterion was also removed |
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.
"checkbox_criterion" -> "entity"
|
||
shared_examples 'handling associated entities upon destroy' do |entity| | ||
it "should remove associated #{entity}" do | ||
# NOTE: the next line assume that an `assignment` is sufficient for the factory of `entity` |
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.
"assume" -> "assumes"
it "should remove associated #{entity}" do | ||
# NOTE: the next line assume that an `assignment` is sufficient for the factory of `entity` | ||
assoc_entity = create entity, assignment: assignment | ||
# Deleting the assignment - should be successful since there are not groupings |
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.
"not" -> "no"
Added dependent: :destroy to all the associations that needed it (in the Assignment and Assessment models), changed the order of associations in Assignment to make groups-and-groupings first (this is the first thing that should be checked when deleting an assignment), created a new locale string for use in the AnnotationCategory model, and added another rescue block in the #destroy action of assignments controller to handle other potential complications that could arise in assignment deletion. This last one is temporary, since we would ideally like to perform a more targetted handling of different exceptions (but for now this prevents crashing and gives an idea of the problem).
(1) An rspec test for the assignments controller that, upon assignment destruction, all associated entities are also destroyed appropriately, and (2) add a dependent: :destroy to the template_divisions association in the AssignmentFile model
Remove options from indirect associations (i.e. associations) because they are redundant/misleading
DRY up the tests by introducing a shared example. Test the case where destroying an assignment leads to a StandardError. Internationalize strings.
a68ce2a
to
441d3c8
Compare
Added
dependent: :destroy
to all the associations that needed it (in theAssignment
andAssessment
models), changed the order of associations in Assignment to make groups-and-groupings first (this is the first thing that should be checked when deleting an assignment), created new locale string for use in theAnnotationCategory
andAssignment
models, and added another rescue block in the#destroy
action of assignments controller to handle other potential complications that could arise in assignment deletion. This last one is temporary, since we would ideally like to perform a more targetted handling of different exceptions (but for now this prevents crashing and gives an idea of the problem).Also, wrote thorough RSPEC tests for
assignments_controller
to ensure that, upon assignment destruction, all associated entities are also destroyed appropriately, and also tested for the case that (the attempt of) destroying an Assignment raises aStandardError
.Motivation and Context
Your Changes
Description:
Type of change (select all that apply):
Testing
RSPEC
(inannotations_controller_spec
)Questions and Comments (if applicable)
Checklist
Pull request to make documentation changes (if applicable)