Skip to content

Instantly share code, notes, and snippets.

@notahat
Created October 6, 2008 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notahat/14984 to your computer and use it in GitHub Desktop.
Save notahat/14984 to your computer and use it in GitHub Desktop.
# This is a teaser for a plugin I'm working on to provide an alternative
# to ActiveRecord fixtures.
#
# It's similar to ThoughtBot's Factory Girl, but I think the syntax is
# a lot nicer.
#
# The models below are the canonical Rails blog examples, i.e. a Post
# has_may Comments.
#
# I'm using the very cool Faker gem to make up values for fields.
Post.blueprint do
title Faker::Lorem.sentence
body Faker::Lorem.paragraph
end
Comment.blueprint do
post
author_name Faker::Name.name
author_email Faker::Internet.email
end
# Make a post.
Post.make
# Make a post with a specific title.
Post.make(:title => 'A Particular Title')
# Make a comment and a post.
Comment.make
# Make a post with a few comments.
post = Post.make
3.times { Comment.make(:post => post) }
# Provide a method on Post to make a post with a few comments.
class Post
def self.make_with_comments(attributes = {})
returning make(attributes) do |post|
3.times { Comment.make(:post => post) }
end
end
end
post = Post.make_with_comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment