Require an id when exposing a record, not persistence - #79
Closed
ngan wants to merge 1 commit into
Closed
Conversation
The guard added alongside class/id capture used `persisted?`, which rejects
an unsaved instance built solely to carry the id of a real row:
expose(address: Addresses::Db::Address.new(id: company_address.id))
Only the class and the id are ever stored, so that is a legitimate way to
expose a row under a different model class than the one that created it --
and it worked before the guard existed. A zenpayroll fixture relies on it.
Check for a nil id instead. That still catches the case the guard was for,
an unsaved record silently resolving to nil in tests, and it no longer
rejects a reference that points at a real row. Records looked up through
`Repository` use `unscoped`, so an id is the only thing a lookup needs.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PQjkiuuGX2t3TSZKpzqpPv
Collaborator
Author
|
Closing in favour of fixing this fixture-side. |
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.
Fixes a false positive in the guard added by #77, found by running against zenpayroll on CI (zenpayroll#363546, build 1660069).
The failure
From
spec/fixture_kit/reporting_toolkit/fully_populated_payroll_custom_report.rb:That is not a mistake.
company_address_2is created withFactoryBot.createa few lines earlier — the bareAddress.new(id:)is a deliberate reference stub, used to expose that row under a different model class than the factory returns. Sinceexposeonly ever stores the class and the id, it worked fine before the guard.Fix
Check
record.id.nil?rather thanrecord.persisted?.That still catches what the guard was added for — an unsaved record captured with a nil id and silently resolving to
nilin tests — while accepting a reference that points at a real row. It's also consistent with how the value is used:Repository#load_recorddoesunscoped.find_by(id: …), so an id is the only thing a lookup needs.The tradeoff: a hard-destroyed record (id present, row gone) now slips through and resolves to
nilinstead of raising. That's the narrower case, and distinguishing it from the soft-deleted recordsunscopedexists to support isn't reliable, so I'd rather not guess.Blast radius
One line across 159 fixture files in zenpayroll. I checked every failed job in the build —
company_address_2is the onlyUnpersistedRecordError, and.new(id:appears in exactly one fixture.Testing
bundle exec rspec— 197 examples, 0 failuresFIXTURE_KIT_INTEGRATION_FRAMEWORK=minitest bundle exec rspec— 197 examples, 0 failuresNote
The
Address.new(id: …)stub is working around a missing feature — there's no first-class way to say "expose this row, looked up as class X." AFixtureKit.reference(Klass, id)helper would express that intent directly and stop the pattern looking like a bug. Out of scope here; worth considering separately.🤖 Generated with Claude Code
https://claude.ai/code/session_01PQjkiuuGX2t3TSZKpzqpPv