Skip to content

Fold AttributeAssigner into Evaluation - #1863

Draft
neilvcarvalho wants to merge 4 commits into
mainfrom
nc/shrink-attribute-assigner-092358
Draft

neilvcarvalho wants to merge 4 commits into
mainfrom
nc/shrink-attribute-assigner-092358

Conversation

@neilvcarvalho

Copy link
Copy Markdown
Member

Step 2 of the refactoring plan says AttributeAssigner keeps object, hash and the alias logic and shrinks to Evaluation. Since the compiled snapshot and the single Evaluator landed, most of what AttributeAssigner computed on every run is already known when the snapshot is built.

Commits

  1. Read assignable names from the compiled snapshot. CompiledFactory computes assignable_names and transient_names once. AttributeAssigner takes the snapshot and drops the helpers that recomputed those lists and the association names per run.
  2. Find ignored aliases from each override. The alias rule starts from FactoryBot.aliases_for(override) instead of comparing every attribute with every override through three predicates.
  3. Fold AttributeAssigner into Evaluation. What remains is object, hash and two private methods, so it moves into Evaluation and the class and its require are removed.
  4. Remove unused Attribute#alias_for?.

lib/factory_bot/attribute_assigner.rb goes from 145 lines to 0. lib/factory_bot/evaluation.rb goes from 25 to 72.

Behavior kept

  • evaluation.object, evaluation.hash, evaluation.create and evaluation.notify are unchanged for strategies.
  • Assignment order is declared attributes in declaration order, then undeclared overrides.
  • Attributes read inside initialize_with are not assigned again.
  • A second call to object returns the same instance without assigning again.
  • FactoryBot.aliases and FactoryBot.aliases_for are untouched. Passing user: skips a declared user_id and the reverse, an association override wins over a foreign key set in a trait, and a factory can still declare both <name> and <name>_id as plain attributes.

The alias rewrite was compared with the implementation on main over 200,000 random combinations of declared attributes (transient or not, association or not), inherited association names, overrides and four FactoryBot.aliases settings, with identical results. The script is not part of this pull request.

Notes for review

  • Evaluation.new now takes (compiled, evaluator). Only CompiledFactory#run builds one.
  • There were no unit specs for AttributeAssigner to move. spec/factory_bot/attribute_assignment_spec.rb goes through Factory#run and is unchanged.
  • Remove leftovers of the Compiler consolidation #1861 also removes a require from lib/factory_bot.rb, a few lines above the one removed here. Whichever merges second may need a trivial rebase.

Verification

  • bundle exec rake all_specs (Ruby 4.0.10, default Gemfile): 312 unit and 457 acceptance examples, 4 scenarios, 0 failures
  • bundle exec standardrb: no offenses
  • BUNDLE_GEMFILE=gemfiles/7.0.gemfile bundle exec rake all_specs: 0 failures
  • BUNDLE_GEMFILE=gemfiles/main.gemfile bundle exec rake all_specs: 0 failures

AttributeAssigner received the merged attribute Hash and, on every run,
walked it again to split transient from assignable names and to collect
association names. CompiledFactory is cached per factory and trait
list, and those lists only depend on its attributes.

This commit computes the assignable and transient names once when the
snapshot is built and hands the snapshot to AttributeAssigner. The
helpers that recomputed the lists are removed, and so is the union with
the associations of the merged attributes, because the Compiler already
includes them in association_names.

The record of assigned names becomes a memoized object. It only had an
effect when a strategy asked for the object twice, in which case the
same instance came back without a second assignment, and that is
unchanged.
To decide which declared attributes an override silences,
AttributeAssigner compared every assignable attribute with every
override, asked the attribute whether it was an alias, and then went
through two more predicates to decide whether the alias could be
ignored.

This commit starts from the override instead. FactoryBot.aliases_for
already lists the names an override stands for, so the declared ones
among them are the only candidates. All of them are ignored when the
override is an association or is not declared, and only the
associations among them otherwise, which keeps a factory free to
declare both <name> and <name>_id as plain attributes.

The result is the same for every combination of declared attributes,
association names, overrides and FactoryBot.aliases. Each run now calls
FactoryBot.aliases_for once per override instead of once per pair of
attribute and override.
Evaluation only forwarded object and hash to AttributeAssigner, and
AttributeAssigner is down to those two methods and the alias rule. A
run built both objects from the same snapshot and evaluator, so the
split added a class without separating any responsibility.

This commit moves object, hash and the alias rule into Evaluation,
which now takes the compiled snapshot and the evaluator and reads
to_create and the callbacks from the snapshot. AttributeAssigner and
its require are removed.

The four methods strategies call on an evaluation (object, hash,
create and notify) behave as before. Evaluation.new takes different
arguments, but only CompiledFactory builds one.
Evaluation now asks FactoryBot.aliases_for for the names an override
stands for, so nothing calls Attribute#alias_for? anymore. The method
is private API and has no spec of its own.

This commit removes it. FactoryBot.aliases and FactoryBot.aliases_for
are untouched.
@neilvcarvalho
neilvcarvalho marked this pull request as draft November 27, 2026 20:15

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