Repository navigation
Fold AttributeAssigner into Evaluation - #1863
Draft
neilvcarvalho wants to merge 4 commits into
Draft
neilvcarvalho wants to merge 4 commits into
neilvcarvalho wants to merge 4 commits into
Conversation
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
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.
Step 2 of the refactoring plan says
AttributeAssignerkeepsobject,hashand the alias logic and shrinks toEvaluation. Since the compiled snapshot and the singleEvaluatorlanded, most of whatAttributeAssignercomputed on every run is already known when the snapshot is built.Commits
CompiledFactorycomputesassignable_namesandtransient_namesonce.AttributeAssignertakes the snapshot and drops the helpers that recomputed those lists and the association names per run.FactoryBot.aliases_for(override)instead of comparing every attribute with every override through three predicates.object,hashand two private methods, so it moves intoEvaluationand the class and itsrequireare removed.lib/factory_bot/attribute_assigner.rbgoes from 145 lines to 0.lib/factory_bot/evaluation.rbgoes from 25 to 72.Behavior kept
evaluation.object,evaluation.hash,evaluation.createandevaluation.notifyare unchanged for strategies.initialize_withare not assigned again.objectreturns the same instance without assigning again.FactoryBot.aliasesandFactoryBot.aliases_forare untouched. Passinguser:skips a declareduser_idand the reverse, an association override wins over a foreign key set in a trait, and a factory can still declare both<name>and<name>_idas 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.aliasessettings, with identical results. The script is not part of this pull request.Notes for review
Evaluation.newnow takes(compiled, evaluator). OnlyCompiledFactory#runbuilds one.AttributeAssignerto move.spec/factory_bot/attribute_assignment_spec.rbgoes throughFactory#runand is unchanged.requirefromlib/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 failuresbundle exec standardrb: no offensesBUNDLE_GEMFILE=gemfiles/7.0.gemfile bundle exec rake all_specs: 0 failuresBUNDLE_GEMFILE=gemfiles/main.gemfile bundle exec rake all_specs: 0 failures