-
Notifications
You must be signed in to change notification settings - Fork 134
Blueprints
A blueprint specifies how to construct an object of a class by providing values for its attributes.
Post.blueprint do
title { "A Post" }
body { "Lorem ipsum..." }
end
If you’ve got an attribute that needs to be unique, Machinist gives you a serial number for the object being created that you can insert into the attribute.
User.blueprint do
username { "user#{sn}" }
end
Learn more about Serial Numbers.
You can access the object being constructed from within your blueprints, so you can construct one attribute from another.
User.blueprint do
username { "user#{sn}" }
email { "#{object.username}@example.com" }
end
Machinist knows about ActiveRecord associations, and will automatically generate associated objects.
Comment.blueprint do
post # This will look at the post association, and make the right kind of object.
end
For has_many
and has_and_belongs_to_many
associations, you can create multiple associated objects by passing a number to the attribute.
Post.blueprint do
comments(3) # This will make 3 Comments.
end
Learn more about Associations.
Machinist knows about object inheritance, and will apply blueprints from parent classes.
ParentClass.blueprint do
name { "Fred" }
age { 97 }
end
ChildClass.blueprint do
# The blueprint will take the name attribute from ParentClass's blueprint.
age { 36 } # This will override the age from ParentClass.
end
Learn more about Inheritance.
You can create different, named blueprints for a class.
Comment.blueprint(:spam) do
spam { true }
body { "Send me money!" }
end
Comment.make(:spam)
Learn more about Named Blueprints.