Skip to content

Support table(false) for models that set their own table_name - #75

Merged
nertzy merged 1 commit into
masterfrom
gh-74/table-false-own-table-name
Oct 12, 2026
Merged

nertzy merged 1 commit into
masterfrom
gh-74/table-false-own-table-name

Conversation

@nertzy

@nertzy nertzy commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

A table(false) model whose superclass has no table (ActiveRecord::Base, or an abstract class) raised WithModel::InvalidSuperclass during setup, before its model block ran, so the model could never use an existing table.

  • Check for a missing table after the model block runs, and accept the model when its own table exists: the one the block names with self.table_name, or else the one Active Record names after the model
  • Name the missing table in the error, and suggest creating it before the model or setting self.table_name
  • Accept an abstract model with table(false), since it needs no table
  • Treat an abstract superclass as having nothing to inherit, and skip its inheritance-column check: Active Record never applies STI below an abstract class
  • Keep checking a concrete superclass's inheritance column before the block runs
  • Remove the constant when the new check fails, so a caller that skips teardown is not left with it
  • Delete rows by type in teardown only for STI subclasses; a model that is its own base class leaves its rows for the suite, and its table need not have an inheritance column
  • Document existing tables in the README and add coverage under RSpec, Minitest, and Test::Unit

The model's table must exist when the model is set up, unlike an STI superclass's table, which may still be created later. With nothing to inherit, a missing table is far more likely a mistaken attempt at STI than a table yet to come.

Fixes #74

Test plan

  • Reproduced the error from the issue's repro against 2.4.0, and confirmed the repro passes against this branch
  • Confirmed the new Minitest and Test::Unit tests fail against 2.4.0 with the reported error
  • Ran bundle exec rake (157 RSpec examples; 14 Minitest runs; 19 Test::Unit tests; StandardRB)
  • Built the RDoc guides without warnings

Copilot AI balanced review requested due to automatic review settings October 12, 2026 18:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The implementation matches the stated behavior and includes comprehensive regression coverage.

0 open findings

What changed in this PR

Adds support for table(false) models that map themselves to existing tables.

Changes:

  • Defers missing-table validation until after model configuration.
  • Preserves STI validation and limits teardown cleanup to STI subclasses.
  • Adds documentation and coverage across all supported test frameworks.
File Description
lib/​with_model/​model.rb Verifies configured models and cleans up failed constants.
lib/​with_model/​null_table.rb Supports explicitly named tables and adjusts teardown.
lib/​with_model/​table.rb Adds the shared verification interface.
lib/​with_model/​model/​dsl.rb Documents existing-table behavior.
spec/​table_false_spec.rb Covers validation, teardown, and custom table names.
spec/​readme_spec.rb Tests the documented example.
test/​own_table_name_test.rb Adds Minitest coverage.
test_unit/​own_table_name_test.rb Adds Test::Unit coverage.
README.md Documents existing-table usage and cleanup.
CHANGELOG.md Records the new capability.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@nertzy
nertzy force-pushed the gh-74/table-false-own-table-name branch from 82aeb84 to 1c8a8f9 Compare October 12, 2026 18:55
@nertzy
nertzy requested a balanced review from Copilot October 12, 2026 18:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Table verification can incorrectly accept a missing model-owned table when the superclass merely has a table name.

1 open finding

🧠 Review effort: Balanced

Comment thread lib/with_model/null_table.rb Outdated
@nertzy
nertzy force-pushed the gh-74/table-false-own-table-name branch 2 times, most recently from b1df951 to 82747a7 Compare October 12, 2026 19:25
A table(false) model whose superclass has no table, such as
ActiveRecord::Base or an abstract class, raised InvalidSuperclass before
its model block ran, so the model could never use an existing table.

Check for a missing table after the model block instead, and accept the
model when its own table exists: the one the block names with
self.table_name, or else the one Active Record names after the model.
Accept an abstract model outright, since it needs no table. Treat an
abstract superclass as having nothing to inherit even when it has a
table, and skip its inheritance-column check: Active Record never
applies single table inheritance below an abstract class.

Delete rows by type in teardown only for single table inheritance
subclasses: a model that is its own base class uses a table the suite
owns, which need not have an inheritance column.

Fixes #74
@nertzy
nertzy force-pushed the gh-74/table-false-own-table-name branch from 82747a7 to 1596f0e Compare October 12, 2026 20:42
@nertzy
nertzy merged commit 4831485 into master Oct 12, 2026
10 checks passed
@nertzy
nertzy deleted the gh-74/table-false-own-table-name branch October 12, 2026 23:11
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 table(false) for models that set their own table_name

2 participants