option to group STI column annotation#1051
Open
dfl wants to merge 4 commits intoctran:developfrom
Open
Conversation
…ship When STI models share a table, every model gets identical annotations. This new option groups columns by which class "owns" them, determined via Rails introspection (validators, associations, enums, stored_attributes). Extracted STI logic into Annotate::StiColumns for clean separation of concerns. Also refactored the existing exclude_sti_subclasses check to use the new module. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…tion - Rename option from --classify-sti-columns to --group-sti-columns to avoid confusion with --classified-sort - Use `descendants` instead of `subclasses` to support multi-level STI hierarchies (e.g. ElectricCar < Car < Vehicle) - Eager-load models via Rails.application.eager_load! to ensure all STI siblings are visible before partitioning - Warn to stderr when the heuristic can't determine column ownership, guiding users to add validations/associations/enums - Add annotation removal test proving STI group headers are cleaned up - Add multi-level STI test (descendants across 3 levels) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
STI models share a single table, so today every model in an STI hierarchy gets an identical annotation listing all columns — including ones that are only relevant to a specific subclass. On a large hierarchy this creates noise on every schema change and makes it hard to tell at a glance which columns a given model actually uses.
The existing
--exclude-sti-subclassesoption sidesteps this by skipping subclass annotation entirely, but throws away useful context. This option takes the opposite approach: keep annotating all models, but group columns by which class owns them.Example output
Base class (
app/models/vehicle.rb):Subclass (
app/models/car.rb) — Truck columns omitted entirely:Summary
--group-sti-columnsCLI option that groups annotation columns by STI class ownership, so base class and subclass annotations show which columns belong where rather than a flat identical list across all modelsbelongs_toassociations, enums, and stored attributes — no configuration requiredAnnotate::StiColumnsmodule to keep it cleanly separated from annotation formatting; also reuse it to simplify the existingexclude_sti_subclassescheckdescendantsinstead ofsubclassesto support multi-level STI hierarchies (e.g.ElectricCar < Car < Vehicle)Rails.application.eager_load!before querying descendants to avoid loading-order issues where the base class is annotated before subclasses are loadedNotes
validates :my_column, absence: falseto the subclass to explicitly claim it.