Skip to content

feat: Add Mermaid namespace clustering support - #480

Merged
kerrizor merged 2 commits into
masterfrom
479-mermaid-namespace-clustering
Jul 10, 2026
Merged

feat: Add Mermaid namespace clustering support#480
kerrizor merged 2 commits into
masterfrom
479-mermaid-namespace-clustering

Conversation

@kerrizor

Copy link
Copy Markdown
Collaborator

Summary

Add cluster: true support for Mermaid classDiagram output, grouping entities by their Ruby namespace into Mermaid namespace { } blocks.

This brings Mermaid to parity with the existing Graphviz clustering feature.

Closes #479

Motivation

When working with large schemas that include multiple namespaces (e.g., Admin::, Billing::, SolidQueue::), it's helpful to visually group related models together. The Graphviz generator already supports this via the cluster option, but Mermaid output ignored it.

Changes

  • Add namespace grouping logic to lib/rails_erd/diagram/mermaid.rb
  • Entities grouped by namespace into Mermaid namespace { } blocks
  • Nested namespaces converted to dot notation (Admin::UsersAdmin.Users)
  • Entities without namespace appear outside any namespace block
  • Relationships render outside namespace blocks (Mermaid requirement)
  • Warning emitted when cluster: true used with erDiagram (not supported)
  • Add 7 new tests covering all clustering scenarios
  • Document the cluster option in README.md

Example Output

# .erdconfig
mermaid_style: classdiagram
cluster: true

Produces:

classDiagram
    namespace Admin {
        class User
        class Role
    }
    namespace Billing {
        class Invoice
        class Payment
    }
    User --> Role
    Invoice --> Payment
Loading

Limitations

Clustering is not supported with Mermaid's default erDiagram style (ER diagrams don't have a namespace concept). A warning is emitted if cluster: true is used with erDiagram.

Testing

  • 391 tests pass (0 failures)
  • 7 new tests added for clustering scenarios
  • Edge cases verified: nested namespaces, cross-namespace relationships, specializations

kerrizor added 2 commits July 10, 2026 12:58
Add `cluster: true` support for Mermaid `classDiagram` output, grouping
entities by their Ruby namespace into Mermaid `namespace { }` blocks.

This brings Mermaid to parity with the existing Graphviz clustering feature.
When clustering is enabled, models like `Admin::User` and `Admin::Role`
are grouped together in a namespace block, making large diagrams with many
namespaced models easier to understand.

Features:
- Entities grouped by namespace into Mermaid namespace blocks
- Nested namespaces converted to dot notation (Admin::Users -> Admin.Users)
- Entities without namespace appear outside any namespace block
- Relationships render outside namespace blocks (Mermaid requirement)
- Warning emitted when cluster: true used with erDiagram (not supported)

Implementation uses a two-phase approach: collect entities by namespace
during each_entity, then emit namespace blocks before relationships and
specializations are rendered.

Closes #479
The test was setting RailsERD.options.warn but the Diagram#warn method
checks options.warn (the diagram's own options hash). This meant the
warning was never actually being captured during the test.

Fixed by:
- Passing :warn => true directly to the Diagram constructor
- Creating the diagram explicitly instead of using the memoized helper
- Capturing the warning output during diagram generation
@kerrizor
kerrizor merged commit 312272f into master Jul 10, 2026
22 checks passed
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.

Support Mermaid subgraphs for namespace clustering

1 participant