Repository navigation
Fold duplicate check into Registry - #1862
Draft
neilvcarvalho wants to merge 5 commits into
Draft
neilvcarvalho wants to merge 5 commits into
neilvcarvalho wants to merge 5 commits into
Conversation
Rejecting a duplicate factory, sequence or trait name was done by Decorator::DisallowsDuplicatesRegistry, a BasicObject proxy that wrapped a Registry and forwarded every other call through method_missing. The rule is a property of the registry, and the proxy hid the real class of the objects Configuration exposes. Registry now raises DuplicateDefinitionError with the same message when a name is already registered. Registry.new takes replace: true for the strategies registry, which has always allowed a strategy to be registered again. Configuration builds plain Registry objects. Registry.new without replace: now rejects duplicates, where it used to overwrite. Registry is private API and Configuration is its only caller. The decorator classes are unused after this commit and are removed in the next one.
Decorator::DisallowsDuplicatesRegistry was the last subclass of Decorator, and nothing uses it now that Registry rejects duplicate names itself. This commit deletes both classes, their require lines and the decorator spec. The examples that spec held are covered by the duplicate examples in the Registry spec. FactoryBot::Decorator was never documented as public API, but code that referenced the constant will now get a NameError.
Folding the duplicate check into Registry made a bare Registry.new reject a name that is already registered, where it used to overwrite. Code outside this gem that builds its own Registry would start raising DuplicateDefinitionError. Registry.new now defaults to replace: true, which is what it did before the check moved in. Configuration passes replace: false for factories, sequences and traits, and strategies use the default. The choice of which registry rejects duplicates lives in Configuration, so this commit also adds examples that register a duplicate trait and a duplicate sequence through Internal. Only the factory message was covered before.
Currently, a factory whose alias is already taken is left registered under the names that came before the duplicate. Defining factory(:other, aliases: [:author]) when :author exists raises, but :other stays in the registry, so defining :other again raises a second, misleading duplicate error. Sequences and traits behave the same way. This commit adds Registry#register_all, which registers an item under several names and removes the ones it added if any name is rejected. Internal uses it for factories, sequences and traits. The error raised and its message are unchanged. The only difference is that a rejected definition no longer leaves partial entries behind.
FactoryBot::Decorator and its DisallowsDuplicatesRegistry subclass were internal, but the constants were reachable, and code that referenced them now gets a NameError. This commit records the removal in the Unreleased section of NEWS.md so that it reaches the release notes.
neilvcarvalho
marked this pull request as draft
November 27, 2026 20:15
This branch has not been deployed
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.
First part of step 6 of the refactoring plan: the duplicate-name check moves
into
Registry, and theDecoratorclasses are deleted.What changed
Registry#registerraisesDuplicateDefinitionErrorwhen the name isalready registered, with the same message as before
(
Factory already registered: user).Registry.new(name)still allows a name to be registered again, as before.Registry.new(name, replace: false)rejects duplicates.Configurationbuilds plainRegistryobjects, withreplace: falseforfactories, sequences and traits. Strategies use the default.
Registry#register_all(names, item)registers an item under all of itsnames or none of them.
Internaluses it for factories, sequences andtraits.
What was removed
FactoryBot::Decorator(lib/factory_bot/decorator.rb), aBasicObjectproxy built on
method_missing.FactoryBot::Decorator::DisallowsDuplicatesRegistry, its last subclass.requirelines inlib/factory_bot.rb.spec/factory_bot/disallows_duplicates_registry_spec.rb. Its two examplesare rewritten against
Registryinspec/factory_bot/registry_spec.rb,with more covering that a rejected duplicate leaves the original item in
place, that the default overwrites, and
register_all.spec/factory_bot/internal_spec.rbgains examples for duplicate traitsand sequences.
Behavior
spec/acceptanceis untouched. Public behavior is unchanged except for theone fix listed below. A script
exercising the registries through the public API printed identical output
before and after: the registry names, enumeration,
find,[],registered?,clear, the duplicate errors for factories (includingaliases), sequences and traits, re-registering a strategy,
FactoryBot.modifyand
FactoryBot.reload.Two differences, both recorded in the Unreleased section of
NEWS.md:registered under the names that came before the duplicate. It now registers
under none of them. The error and its message are the same.
FactoryBot::Decoratorconstant willget a
NameError. factory_bot_rails 6.5.1 does not reference it.Commits
Each commit leaves the suite green.
Verification
All on Ruby 4.0.10, run one after the other:
bundle exec rake all_specs: 318 and 457 examples, 0 failures; 4 scenariospassed
bundle exec standardrb: no offensesBUNDLE_GEMFILE=gemfiles/7.0.gemfile bundle exec rake all_specs: greenBUNDLE_GEMFILE=gemfiles/main.gemfile bundle exec rake all_specs: greenRuby 3.1 was not available locally, so that part of the matrix relies on CI.
The change uses no syntax newer than 3.1.