Skip to content

Instantly share code, notes, and snippets.

@matthutchinson
Created May 10, 2012 16:59
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save matthutchinson/2654461 to your computer and use it in GitHub Desktop.
Save matthutchinson/2654461 to your computer and use it in GitHub Desktop.
fake.rake - a takeout approach and rake task that generates some random fake data for a rails app (using ffaker)
# place this in lib/fakeout.rb
require 'ffaker'
module Fakeout
class Builder
FAKEABLE = %w(User Product)
attr_accessor :report
def initialize
self.report = Reporter.new
clean!
end
# create users (can be admins)
def users(count = 1, options = {}, is_admin = false)
1.upto(count) do
user = User.new({ :email => random_unique_email,
:password => 'Testpass',
:password_confirmation => 'Testpass' }.merge(options))
user.save
if is_admin
user.update_attribute(:is_admin, true)
self.report.increment(:admins, 1)
end
end
self.report.increment(:users, count)
end
# create products (can be free)
def products(count = 1, options = {})
1.upto(count) do
attributes = { :name => Faker::Company.catch_phrase,
:price => 20+Random.rand(11),
:description => Faker::Lorem.paragraph(2) }.merge(options)
product = Product.new(attributes)
product.name = "Free #{product.name}" if product.free?
product.save
end
self.report.increment(:products, count)
end
# cleans all faked data away
def clean!
FAKEABLE.map(&:constantize).map(&:destroy_all)
end
private
def pick_random(model)
ids = ActiveRecord::Base.connection.select_all("SELECT id FROM #{model.to_s.tableize}")
model.find(ids[rand(ids.length)]['id'].to_i) if ids
end
def random_unique_email
Faker::Internet.email.gsub('@', "+#{User.count}@")
end
end
class Reporter < Hash
def initialize
super(0)
end
def increment(fakeable, number = 1)
self[fakeable.to_sym] ||= 0
self[fakeable.to_sym] += number
end
def to_s
report = ""
each do |fakeable, count|
report << "#{fakeable.to_s.classify.pluralize} (#{count})\n" if count > 0
end
report
end
end
end
# Now the rake task
# place this in lib/tasks/fake.rake
require 'fakeout'
desc "Fakeout data"
task :fake => :environment do
faker = Fakeout::Builder.new
# fake users
faker.users(9)
faker.users(1, { :email => 'matt@test.com' }, true)
# fake products
faker.products(12)
faker.products(4, { :price => 0 })
# report
puts "Faked!\n#{faker.report}"
end
@rmcsharry
Copy link

Thanks for creating this, it inspired me to add further flexibility - https://gist.github.com/rmcsharry/595bc130194087ff41135da9799b90ae

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment