class User < ActiveRecord::Base
end
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
# ---- Basics ---- #
KathyLee::User do
property :username, fake(:username)
property :email, fake(:email_address)
property :home_page_url, fake(:url)
property :fullname, fake(:first_name, ' ', :last_name)
property :login_count, 0
end
# Creates a new User:
user = KathyLee::User.new
user.username # => 'bill.smith'
user.email # => 'bill.smith@example.org'
user.home_page_url # => 'http://www.example.com'
user.fullname # => 'Bill Smith'
user.login_count # => 0
user.new_record? # => true
# Creates and saves a new User:
user = KathyLee::User.create
user.username # => 'bill.smith'
user.email # => 'bill.smith@example.org'
user.home_page_url # => 'http://www.example.com'
user.fullname # => 'Bill Smith'
user.login_count # => 0
user.new_record? # => false
# Creates a new User:
user = KathyLee::User.new(:username => 'mark.bates', :login_count => 5)
user.username # => 'mark.bates'
user.email # => 'bill.smith@example.org'
user.home_page_url # => 'http://www.example.com'
user.fullname # => 'Bill Smith'
user.login_count # => 5
user.new_record? # => true
# ---- Bulk creating ---- #
# Creates 10 new users:
users = KathyLee::User.sweatshop(10) do |user, index|
user.login_count = index
end
# Creates and saves 10 new users:
users = KathyLee::User.sweatshop!(10) do |user, index|
user.login_count = index
end
# ---- Associations ---- #
KathyLee::Post do
property :title, fake(:title)
property :body, fake(:lorem)
has_many :comments, :size => 10
end
KathyLee::Comment do
property :post_id
property :body, fake(:lorem)
belongs_to :post
end
post = KathyLee::Post.new
post.title # => 'my title'
post.body # => 'Lorem ipsum....'
post.comments # => [<comment>, ...]
post.comments.size # => 10
comment = KathyLee::Comment.create(:post => KathyLee::create(Post))
comment.body # => 'Lorem ipsum...'
comment.post.title # => 'my title'
# ---- Creating new generators ---- #
KathyLee.fake(:yes_or_no) do
return ['yes', 'no'].sort_by({rand}).first
end
KathyLee::SomeModel do
property :state, fake(:yes_or_no)
end