Skip to content

feat: refactor group creation#7975

Merged
david-yz-liu merged 6 commits into
MarkUsProject:masterfrom
YheChen:feat/auto-group-id
Jun 5, 2026
Merged

feat: refactor group creation#7975
david-yz-liu merged 6 commits into
MarkUsProject:masterfrom
YheChen:feat/auto-group-id

Conversation

@YheChen

@YheChen YheChen commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

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.)

Refactors Group creation to assign the group's id, group_name, and repo_name up front in a before_validation callback, eliminating the previous "save with nil name → assign name from id → save again" pattern.

What changed:

  • Added before_validation :assign_id_and_default_names, on: :create to Group. The callback reserves the next value from groups_id_seq via nextval, then uses that id to populate id/group_name/repo_name only when the caller hasn't supplied them.
  • Removed the set_repo_name after_create callback. Its job is now done by the new before_validation callback.
  • Converted check_repo_uniqueness from an after_create callback that raised on conflict into a standard validate :check_repo_uniqueness, on: :create that adds an error. This avoids partway-through-creation exceptions.
  • Simplified the four call sites in Assignment#add_group, Assignment#add_group_api, Student#create_group_for_working_alone_student, and Student#create_autogenerated_name_group — each collapses from a four-line save/assign/save dance into a single Group.create / Group.new call.
  • Re-added the NOT NULL constraint on groups.group_name in a new migration, now that the model guarantees the column is set before save. Removed the corresponding missing_non_null_constraint exception from .active_record_doctor.rb.
  • Removed three tests in group_spec.rb (validate_presence_of, validate_uniqueness_of, belong_to(:course) for group_name) that are no longer testable on :create context as the callback fills nil values before validation runs. Added a comment noting that those invariants are now enforced by the callback, the unique index, and the foreign key respectively.
Screenshots of your changes (if applicable)
Associated documentation repository pull request (if applicable)

Type of Change

(Write an X or a brief description next to the type or types that best describe your changes.)

Type Applies?
🚨 Breaking change (fix or feature that would cause existing functionality to change)
New feature (non-breaking change that adds functionality)
🐛 Bug fix (non-breaking change that fixes an issue)
🎨 User interface change (change to user interface; provide screenshots)
♻️ Refactoring (internal change to codebase, without changing functionality) x
🚦 Test update (change that only adds or modifies tests)
📦 Dependency update (change that updates a dependency)
🔧 Internal (change that only affects developers or continuous integration)

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:

  • I have performed a self-review of my changes.
    • Check that all changed files included in this pull request are intentional changes.
    • Check that all changes are relevant to the purpose of this pull request, as described above.
  • I have added tests for my changes, if applicable.
    • This is required for all bug fixes and new features.
  • I have updated the project documentation, if applicable.
    • This is required for new features.
  • If this is my first contribution, I have added myself to the list of contributors.

After opening your pull request:

  • I have updated the project Changelog (this is required for all changes).
  • I have verified that the pre-commit.ci checks have passed.
  • I have verified that the CI tests have passed.
  • I have reviewed the test coverage changes reported by Coveralls.
  • I have requested a review from a project maintainer.

Questions and Comments

(Include any questions or comments you have regarding your changes.)

@coveralls

coveralls commented Jun 2, 2026

Copy link
Copy Markdown
Collaborator

Coverage Report for CI Build 27021988134

Coverage increased (+0.005%) to 90.195%

Details

  • Coverage increased (+0.005%) from the base build.
  • Patch coverage: 1 uncovered change across 1 file (29 of 30 lines covered, 96.67%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
app/models/group.rb 11 10 90.91%
Total (4 files) 30 29 96.67%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 50083
Covered Lines: 46157
Line Coverage: 92.16%
Relevant Branches: 2226
Covered Branches: 1023
Branch Coverage: 45.96%
Branches in Coverage %: Yes
Coverage Strength: 125.69 hits per line

💛 - Coveralls

@YheChen YheChen changed the title WIP: feat: refactor group creation feat: refactor group creation Jun 2, 2026
@YheChen YheChen requested a review from david-yz-liu June 2, 2026 18:09
Comment thread app/models/group.rb Outdated

next_id = self.class.connection.select_value("SELECT nextval('groups_id_seq')").to_i
self.id ||= next_id
self.group_name ||= AUTOGENERATED_PREFIX + next_id.to_s.rjust(4, '0')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use self.get_autogenerated_group_name for this and the one below

Comment thread Changelog.md Outdated
### 🐛 Bug fixes

### 🔧 Internal changes
- Refactored `Group` creation to reserve the next id from `groups_id_seq` in a `before_validation` callback that populates `id`, `group_name`, and `repo_name` up front (#7975)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"up front" -> "before validation"

Comment thread Changelog.md Outdated

### 🔧 Internal changes
- Refactored `Group` creation to reserve the next id from `groups_id_seq` in a `before_validation` callback that populates `id`, `group_name`, and `repo_name` up front (#7975)
- Re-enforced `NOT NULL` constraint on `groups.group_name` and removed the corresponding `active_record_doctor` exception, now that the new callback guarantees the column is set before save (#7975)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implications of time are not great for the Changelog, as this historical context distracts from the actual change itself.

I would reword to just "Added NOT NULL constraint on groups.group_name". The PR description can provide additional context

Comment thread spec/models/group_spec.rb Outdated

it { is_expected.to validate_presence_of(:group_name) }
it { is_expected.to validate_uniqueness_of(:group_name).scoped_to(:course_id) }
# NOTE: presence, uniqueness, and belongs_to(:course) for group_name are

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Group model still has all three of these validations (which is good). It is true that there are callbacks that ensure these validations pass, but that doesn't mean that the validations themselves aren't checked. So it's appropriate to keep the tests here.

@YheChen YheChen force-pushed the feat/auto-group-id branch from fcb0aaa to 22250e7 Compare June 5, 2026 13:10
@YheChen YheChen requested a review from david-yz-liu June 5, 2026 13:10

@david-yz-liu david-yz-liu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, @YheChen!

@david-yz-liu david-yz-liu merged commit 290cc41 into MarkUsProject:master Jun 5, 2026
7 checks passed
@YheChen YheChen deleted the feat/auto-group-id branch June 5, 2026 19:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants