Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
fakeout.rake - a simple/configurable rake task that generates some random fake data for the app (using faker) at various sizes
# a simple/configurable rake task that generates some random fake data for the app (using faker) at various sizes
# Either add this to your Gemfile's development, test group or both, or install the gems and require them before running this
# gem 'ffaker'
# gem 'randumb'
#
class Fakeout
# START Customizing
# e.g. this example fakes out, Users, Questions
MODELS = %w(User Question)
# 2. now define a build method for each model, returning a list of attributes for Model.create! calls
# check out the very excellent faker gem rdoc for faking out anything from emails, to full addresses; http://faker.rubyforge.org/rdoc
# NOTE: a build_??? method MUST exist for each model you specify above
def build_user(username = "#{Faker::Internet.user_name}_#{random_letters}", email = Faker::Internet.email, password = 'password')
{ :username => username,
:email => email,
:role => %(admin user whatever).sample, # Array#sample picks a random element from it
:password => password,
:password_confirmation => password }
end
def build_question
question_time = fake_time_from(1.year.ago)
{ :title => "#{Faker::Lorem.sentence(8+rand(8)).chop}?",
:information => Faker::Lorem.paragraph(rand(3)),
:notify_user => false,
:created_at => question_time,
:updated_at => question_time,
:spam_answer => '2',
:spam_question => '1+1 is?',
:possible_answers => [Digest::MD5.hexdigest('2')],
:user => User.random #Use the #random method from randumb to get a random object from that class
}
end
def post_fake
# called after faking out, use this method for additional updates or additions
end
# 3. optionally you can change these numbers, basically they are used to determine the number of models to create
# and also the size of the tags array to choose from. To check things work quickly use the tiny size (1 for everything)
def tiny
1
end
def small
25+rand(50)
end
def medium
250+rand(250)
end
def large
1000+rand(500)
end
# END Customizing
attr_accessor :size
def initialize(size)
self.size = size
end
def builder_for(model)
"build_#{model.downcase}".parameterize.underscore
end
def fakeout
puts "Faking it ... (#{size})"
Fakeout.disable_mailers
MODELS.each do |model|
if !respond_to?(builder_for(model))
puts " * #{model.pluralize}: **warning** I couldn't find a #{builder_for(model)} method"
next
end
1.upto(send(size)) do
attributes = send(builder_for(model))
model.constantize.create!(attributes) if attributes && !attributes.empty?
end
puts " * #{model.pluralize}: #{model.constantize.count(:all)}"
end
post_fake
puts "Done, I Faked it!"
end
def self.clean
puts "Cleaning all ..."
Fakeout.disable_mailers
MODELS.each do |model|
model.constantize.delete_all
end
end
# by default, all mailings are disabled on faking out
def self.disable_mailers
ActionMailer::Base.perform_deliveries = false
end
private
# useful for prepending to a string for getting a more unique string
def random_letters(length = 2)
Array.new(length) { (rand(122-97) + 97).chr }.join
end
# fake a time from: time ago + 1-8770 (a year) hours after
def fake_time_from(time_ago = 1.year.ago)
time_ago+(rand(8770)).hours
end
end
# the tasks, hook to class above - use like so;
# rake fakeout:clean
# rake fakeout:medium RAILS_ENV=bananas
#.. etc.
namespace :fakeout do
desc "clean away all data"
task :clean => :environment do |t, args|
Fakeout.clean
end
desc "fake out a tiny dataset"
task :tiny => :clean do |t, args|
Fakeout.new(:tiny).fakeout
end
desc "fake out a small dataset"
task :small => :clean do |t, args|
Fakeout.new(:small).fakeout
end
desc "fake out a medium dataset"
task :medium => :clean do |t, args|
Fakeout.new(:medium).fakeout
end
desc "fake out a large dataset"
task :large => :clean do |t, args|
Fakeout.new(:large).fakeout
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment