-
Notifications
You must be signed in to change notification settings - Fork 252
Use username as group name when creating individual groups via API #7481
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
Use username as group name when creating individual groups via API #7481
Conversation
Resolve N+1 query issue in creating members Wrap group creation in transaction block
… group-name-generation
… group-name-generation
Pull Request Test Coverage Report for Build 14286000465Details
💛 - Coveralls |
david-yz-liu
left a comment
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.
@Raine-Yang-UofT nice work!
app/models/assignment.rb
Outdated
| group = nil | ||
|
|
||
| Group.transaction do | ||
| group = if new_group_name.nil? |
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.
In general I'd like to avoid this style of variable assignment. Instead, keep the if statement at the outer level and initialize group in each branch. This also allows you to delete the group = nil line above.
app/models/assignment.rb
Outdated
| group = if new_group_name.nil? | ||
| if members.length == 1 && self.group_max == 1 | ||
| student_user_name = members.first | ||
| if self.is_timed |
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.
This is a great condition to check. Putting it here is a larger divergence from the original logic (which didn't check is_timed at all).
So instead of putting it here, add it to the condition on line 401 (&& !self.is_timed), so that for timed assessments the previous logic will execute. This also helps remove some of the code duplication.
app/models/assignment.rb
Outdated
| raise 'A group name was not provided' | ||
| end | ||
| else | ||
| existing_group = self.course.groups.find_by(group_name: new_group_name) |
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 don't need to change this code from the original version
app/models/assignment.rb
Outdated
|
|
||
| group.save! | ||
|
|
||
| if groupings.exists?(group_id: group.id) |
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.
In general, prefer using explicit self for all attributes/methods
app/models/assignment.rb
Outdated
| grouping = Grouping.create!(group: group, assignment: self) | ||
|
|
||
| unless members.empty? | ||
| students = Student.joins(:user) |
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.
use self.course.students (need to ensure only students from the current course are queried)
app/models/assignment.rb
Outdated
| unless members.empty? | ||
| students = Student.joins(:user) | ||
| .where(users: { user_name: members }) | ||
| .index_by { |s| s.user.user_name } |
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.
This is a good time to use pluck, as you really only need the user name and id here
app/models/assignment.rb
Outdated
|
|
||
| grouping.student_memberships.create!( | ||
| role_id: student.id, | ||
| membership_status: if index.zero? |
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.
I have the same stylistic comment as above. However, in this case you should be able to fit this on one line if you apply the ternary expression to just :inviter or :accepted, and not the other parts
spec/models/assignment_spec.rb
Outdated
| end | ||
| end | ||
|
|
||
| context 'when group already exists in assignment' do |
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.
reword to "when group already has a grouping for assignment"
spec/models/assignment_spec.rb
Outdated
| let(:student2) { create(:student, user: user2, course: course) } | ||
|
|
||
| shared_examples 'group persistence and members' do |expected_member_count: 0| | ||
| it 'creates persistent group and grouping' do |
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.
I would split this into two assertions, one for the first assertion and one for the third assertion (the second is just testing an association so I don't think it's necessary). Also for the third assertion I would use find_by rather than where
spec/models/assignment_spec.rb
Outdated
| if expected_member_count > 0 | ||
| expect(grouping.student_memberships.first.membership_status).to eq(StudentMembership::STATUSES[:inviter]) | ||
| if expected_member_count > 1 | ||
| expect(grouping.student_memberships.last.membership_status).to eq(StudentMembership::STATUSES[:accepted]) |
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.
Testing just last is a bit strange; it's more precise to use a loop for the rest.
I also recommend adding an additional test case for having more than two members.
|
Thank you, @Raine-Yang-UofT! |
Proposed Changes
(Describe your changes here. Also describe the motivation for your changes: what problem do they solve, or how do they improve the application or codebase? If this pull request fixes an open issue, use a keyword to link this pull request to the issue.)
...
membersparameter in/api/courses/:course_id/assignments/:assignment_id/groupsendpoint to create a group with members.group_max = 1), the group name created via API with one member is set to the member name.Screenshots of your changes (if applicable)
Associated documentation repository pull request (if applicable)
MarkUsProject/Wiki/pull/231Type of Change
(Write an
Xor a brief description next to the type or types that best describe your changes.)Checklist
(Complete each of the following items for your pull request. Indicate that you have completed an item by changing the
[ ]into a[x]in the raw text, or by clicking on the checkbox in the rendered description on GitHub.)Before opening your pull request:
After opening your pull request:
Questions and Comments
(Include any questions or comments you have regarding your changes.)