Skip to content

Fold duplicate check into Registry - #1862

Draft
neilvcarvalho wants to merge 5 commits into
mainfrom
nc/fold-duplicate-check-into-registry-37f6a3
Draft

neilvcarvalho wants to merge 5 commits into
mainfrom
nc/fold-duplicate-check-into-registry-37f6a3

Conversation

@neilvcarvalho

@neilvcarvalho neilvcarvalho commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

First part of step 6 of the refactoring plan: the duplicate-name check moves
into Registry, and the Decorator classes are deleted.

What changed

  • Registry#register raises DuplicateDefinitionError when the name is
    already 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.
  • Configuration builds plain Registry objects, with replace: false for
    factories, sequences and traits. Strategies use the default.
  • Registry#register_all(names, item) registers an item under all of its
    names or none of them. Internal uses it for factories, sequences and
    traits.

What was removed

  • FactoryBot::Decorator (lib/factory_bot/decorator.rb), a BasicObject
    proxy built on method_missing.
  • FactoryBot::Decorator::DisallowsDuplicatesRegistry, its last subclass.
  • Their two require lines in lib/factory_bot.rb.
  • spec/factory_bot/disallows_duplicates_registry_spec.rb. Its two examples
    are rewritten against Registry in spec/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.rb gains examples for duplicate traits
    and sequences.

Behavior

spec/acceptance is untouched. Public behavior is unchanged except for the
one 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 (including
aliases), sequences and traits, re-registering a strategy, FactoryBot.modify
and FactoryBot.reload.

Two differences, both recorded in the Unreleased section of NEWS.md:

  • A factory, sequence or trait whose alias is already registered used to stay
    registered under the names that came before the duplicate. It now registers
    under none of them. The error and its message are the same.
  • Code that referenced the undocumented FactoryBot::Decorator constant will
    get a NameError. factory_bot_rails 6.5.1 does not reference it.

Commits

  1. Reject duplicate names in Registry itself
  2. Remove the Decorator classes
  3. Keep replacement as the Registry default
  4. Register all names of an item or none
  5. Note the Decorator removal in NEWS

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 scenarios
    passed
  • bundle exec standardrb: no offenses
  • BUNDLE_GEMFILE=gemfiles/7.0.gemfile bundle exec rake all_specs: green
  • BUNDLE_GEMFILE=gemfiles/main.gemfile bundle exec rake all_specs: green

Ruby 3.1 was not available locally, so that part of the matrix relies on CI.
The change uses no syntax newer than 3.1.

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.

This branch has not been deployed

No deployments
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.

1 participant