notahat (owner)

Revisions

gist: 124652 Download_button fork
public
Description:
Example of how Machinist handles model associations
Public Clone URL: git://gist.github.com/124652.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Let's say we have some really simple models for a blog. (I'm using
# DataMapper here, but Machinist works exactly the same way with ActiveRecord.)
 
  class Person
    include DataMapper::Resource
    property :id, Serial
    property :name, String
  end
 
  class Post
    include DataMapper::Resource
    property :id, Serial
    property :author_id, Integer
    property :body, String
 
    belongs_to :author, :class_name => "Person", :child_key => [:author_id]
    has n, :comments
  end
 
  class Comment
    include DataMapper::Resource
    property :id, Serial
    property :post_id, Integer
    property :body, String
 
    belongs_to :post
  end
 
# I can define Machinist blueprints for these like this:
 
  Person.blueprint do
    name "Fred" # In real life I'd use Sham to generate the attribute values.
                  # See the Machinist README for details!
  end
 
  Post.blueprint do
    author # This tells Machinist to make an author for the post.
    body "A post"
  end
 
  Comment.blueprint do
    post # This tells Machinist to make a post for the comment.
    body "A comment"
  end
 
# Then in my tests I can call:
 
  comment = Comment.make
 
# Machinist will introspect on the associations and create a Comment, a Post,
# and a Person, and set up all the relationships properly.
#
# Let's see Factory Girl do that! ;)
#
# Machinist lives at: http://github.com/notahat/machinist