You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
to_create has either arity of one - to_create {|instance| do something } or two to_create {|instance, context| do something with instance and context }
For the context, we are using repository pattern in our app, the entities are immutable structs, so building them is simply initializing a class. Same time persistence is a create(**attrs) method in a repo instance, that returns an entity after saving it to a DB.
This is the snippet I've came up with after some experimenting:
moduleFactoryBotHelpers# no clean way (only chain of private methods and vars) to pass our repo class to both initialize_with and to_create# custom strategies also don't provide access to the class of the factory for some reason# hence, this helper methoddeffactory(name,repo_class:, &)# values that are mandatory to initialize our immutable entities,# but are set in most cases by ORM when persistingmissing_build_attributes={id: ->{GenerateULID.call},}FactoryBot.definedo# expectation was that class won't be required as we are overriding initialize_with# but FactoryBot still tries to figure out class name from factory namefactoryname,class: repo_classdoinitialize_withdorepo_class.constantize.new.entity_class.new(
**missing_build_attributes.transform_values(&:call),
**attributes,)endto_createdo |instance,context|
repo_class.constantize.new.create!(
**instance.attributes.except(# we need to remove default values that we supplied in initialize_with, but# we also need to keep the values that were set by the testmissing_build_attributes.keys - context.__override_names__,),)endinstance_eval(&)endendendmodule_function:factoryend
But in this case requires access to the names of the overridden attributes via __override_names__. Is it discouraged?
The text was updated successfully, but these errors were encountered:
jacob-s-son
changed the title
Question: Why Evaluator class is marked as a @api private if it's instance is part of to_create API?
Question: Why Evaluator class is marked as a @api private if its instance is part of to_create API?
Jun 16, 2024
to_create
has either arity of one -to_create {|instance| do something }
or twoto_create {|instance, context| do something with instance and context }
For the context, we are using repository pattern in our app, the entities are immutable structs, so building them is simply initializing a class. Same time persistence is a
create(**attrs)
method in a repo instance, that returns an entity after saving it to a DB.This is the snippet I've came up with after some experimenting:
But in this case requires access to the names of the overridden attributes via
__override_names__
. Is it discouraged?The text was updated successfully, but these errors were encountered: