Building using strategies
Blueprints boy allows defining blueprint to be built using different strategies. These three are the most common:
Name | Description |
---|---|
:create |
Default strategy, initializes object and saves it in database |
:update |
Default strategy for already build blueprints, updates object with new options that are passed |
:new |
Only initialize object, don’t save it to database |
:attributes |
Only return attributes that would be used to build blueprint |
Defining strategies
Each blueprint can define multiple strategies on how it can be built. For example this blueprint
will have 2 strategies: :create
and :twice
. Depending on what strategy you choose when building blueprint you will
get different result. You can choose strategy by using build_with :strategy, :blueprint
.
Updating blueprint
Building same blueprint twice with different options will result in :update strategy being used the second time.
Building multiple times
If you want to build same blueprint multiple times (creating object that blueprint creates multiple times). You can use
build!
method.
Factories
When adding factories you can define class and strategy that particular factory is used for.
Summary of build methods
Method | Strategy |
---|---|
build | :create or :update |
build! | :create |
build_new | :new |
build_attributes | :attributes |
build_with | Passed as first argument |