Skip to content

feat: create Students/Coaches groups on chapter creation - #2824

Merged
mroderick merged 5 commits into
masterfrom
feature/auto-create-groups-on-chapter-creation
Aug 31, 2026
Merged

feat: create Students/Coaches groups on chapter creation#2824
mroderick merged 5 commits into
masterfrom
feature/auto-create-groups-on-chapter-creation

Conversation

@mroderick

@mroderick mroderick commented Aug 30, 2026

Copy link
Copy Markdown
Collaborator

Problem

Creating a chapter via /admin/chapters/new did not automatically create the required Students and Coaches groups. Admins had to manually visit /admin/groups/new twice to create them.

This caused:

  • No subscription buttons on chapter pages (iterates @chapter.groups)
  • Workshop invitations silently sent to zero members
  • No group stats on admin chapter show page

Solution

Added ChapterCreationService that creates chapter + groups in a single transaction:

  • Creates chapter, then Students and Coaches groups
  • If groups fail, chapter is rolled back
  • Admin::ChaptersController#create authorizes the unsaved chapter before calling the service, so a denied request persists nothing
  • Controller stays thin, just coordinates

Changes

  • New: app/services/chapter_creation_service.rb — service object with transaction
  • New: spec/services/chapter_creation_service_spec.rb — unit tests, including a real group-failure rollback test
  • Updated: Admin::ChaptersController#create — authorizes first, then delegates to the service
  • Updated: feature spec — verifies groups are created; new scenario for the invalid-form failure branch
  • Updated: controller spec — new denial-path regression spec (a chapter-scoped organiser persists nothing)

Testing

All tests pass (51 examples, 0 failures). Code-reviewed with ce-code-review: the initial implementation persisted before authorizing (a chapter-scoped organiser would be denied after the commit, leaving phantom data) — fixed and covered by the denial regression spec.

Follow-up

Once merged, can remove the manual group creation UI (admin/groups/new, Admin::GroupsController#new/#create, and the admin menu link) as a separate PR.

Related

#2823 — Auto-create Students and Coaches groups when a new chapter is created (linked only — this PR intentionally does not close the issue; the follow-up that removes the manual group creation UI will)

mroderick added a commit that referenced this pull request Aug 30, 2026
Addresses ce-code-review findings on PR #2824:

- (P0) Admin::ChaptersController#create persisted the chapter and
  groups before Pundit authorize, so a chapter-scoped organiser was
  denied after commit, leaving phantom data. Authorize the unsaved
  chapter first, as #new and the pre-refactor code did, with a denial
  regression spec.
- The 'rolls back chapter if groups fail' spec never forced a group
  failure (a blank chapter name failed chapter.save! first). Force a
  real Group inclusion-validation failure and assert the saved
  chapter is rolled back; the chapter-validation example is kept
  separately under its true name.
- Added a feature scenario for the create failure branch: an invalid
  form re-renders with validation errors and persists nothing.

Verified: 51 examples, 0 failures; rubocop clean on changed files.
@mroderick
mroderick force-pushed the feature/auto-create-groups-on-chapter-creation branch from 8147e39 to cbbefdd Compare August 30, 2026 18:21
ChapterCreationService wraps chapter + groups creation in a single
transaction, so a new chapter always has its required Students and
Coaches groups (issue #2823). Missing groups meant no subscription
buttons on the chapter page and workshop invitations silently sent
to zero members.

- Admin::ChaptersController#create authorizes the unsaved chapter
  before calling the service, so a denied request persists nothing
- The service rolls back the chapter if group creation fails
- Service spec proves the rollback with a real group validation
  failure; controller spec covers the denial path; feature spec
  covers group creation and the invalid-form failure branch
@mroderick
mroderick force-pushed the feature/auto-create-groups-on-chapter-creation branch from cbbefdd to 2325fd7 Compare August 30, 2026 18:27
@mroderick mroderick changed the title feat: auto-create Students and Coaches groups on chapter creation feat: create Students/Coaches groups on chapter creation Aug 30, 2026
@mroderick
mroderick requested a review from olleolleolle August 30, 2026 18:30
Now that every chapter gets its Students and Coaches groups
automatically on creation, the manual group creation flow is dead
code (follow-up to #2823).

- Remove Admin::GroupsController#new/#create and the new.html.haml
  view (also drops the last Chosen-enhanced admin <select>)
- Remove the "New group" link from the admin menu
- Remove the new/create group routes, plus the already-stale index
  route (the controller has no index action)
- Remove GroupPolicy#create? and its spec coverage
- Remove the admin group creation feature spec

Kept: the group show page (members, eligible count, subscriptions).
Deferring the chapter_with_groups fabricator fold into the base
chapter fabricator - it touches ~63 Fabricate(:chapter) call sites
and belongs in its own cleanup.

Stacked on feature/auto-create-groups-on-chapter-creation (PR #2824).
@mroderick
mroderick marked this pull request as ready for review August 30, 2026 18:49
…ation

refactor: remove manual group creation UI
@@ -0,0 +1,17 @@
class ChapterCreationService
Result = Struct.new(:chapter, :success, :errors, keyword_init: true)

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.

Not a blocker, but can use Data.define rather than Struct.new to make this class, I guess.

@olleolleolle olleolleolle 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.

Looks good!

@mroderick
mroderick merged commit dc0efff into master Aug 31, 2026
10 checks passed
@mroderick
mroderick deleted the feature/auto-create-groups-on-chapter-creation branch August 31, 2026 07:04
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.

2 participants