-
-
Save meleyal/851316 to your computer and use it in GitHub Desktop.
Fakeout.rake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# a simple/configurable rake task that generates some random fake data for the app (using faker) at various sizes | |
# NOTE: requires the faker or ffaker gem | |
require 'faker' | |
class Fakeout | |
# START Customizing | |
# 1. first these are the model names we're going to fake out | |
MODELS = %w(Person Question Answer) | |
IMAGES = %w(bqy5 brjg brjh brji brjk brjp brjo brjn brjm) | |
# 2. now define a build method for each model, returning a list of attributes for Model.create! calls | |
def build_person | |
{ :name => "#{Faker::Name.first_name} #{Faker::Name.last_name}", | |
:about => Faker::Lorem.paragraph, | |
:image_url => "http://example.com/#{IMAGES.sample(1).first}_512.jpg"}} } | |
end | |
def build_question | |
question_time = fake_time_from(3.years.ago) | |
avatar = "http://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(Faker::Internet.email)}?d=identicon&s=48" | |
{ :author_name => Faker::Name.name, | |
:author_url => Faker::Internet.domain_name, | |
:author_avatar_url => avatar, | |
:body => Faker::Lorem.paragraph, | |
:votes => rand(100), | |
:disqus_post_id => '', | |
:created_at => question_time, | |
:updated_at => question_time, | |
:person => pick_random(Person) } | |
end | |
def build_answer | |
question = pick_random(Question) | |
answer_time = question.created_at+rand(168).hours | |
{ :video => 'http://www.youtube.com/watch?v=8AOfbnGkuGc', | |
:published => true, | |
:created_at => answer_time, | |
:updated_at => answer_time, | |
:question => question } | |
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 :all_tags, :size | |
def initialize(size, prompt=true) | |
self.size = size | |
end | |
def fakeout | |
puts "Faking it ... (#{size})" | |
Fakeout.disable_mailers | |
MODELS.each do |model| | |
if !respond_to?("build_#{model.downcase}") | |
puts " * #{model.pluralize}: **warning** I couldn't find a build_#{model.downcase} method" | |
next | |
end | |
1.upto(send(size)) do | |
attributes = send("build_#{model.downcase}") | |
model.constantize.create!(attributes) if attributes && !attributes.empty? | |
end | |
puts " * #{model.pluralize}: #{model.constantize.count(:all)}" | |
end | |
puts "Done, I Faked it!" | |
end | |
def self.prompt | |
puts "Really? This will clean all #{MODELS.map(&:pluralize).join(', ')} from your #{RAILS_ENV} database y/n? " | |
STDOUT.flush | |
(STDIN.gets =~ /^y|^Y/) ? true : exit(0) | |
end | |
def self.clean(prompt = true) | |
self.prompt if prompt | |
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 | |
# pick a random model from the db, done this way to avoid differences in mySQL rand() and postgres random() | |
def pick_random(model, optional = false) | |
return nil if optional && (rand(2) > 0) | |
ids = ActiveRecord::Base.connection.select_all("SELECT id FROM #{model.to_s.tableize}") | |
model.find(ids[rand(ids.length)]["id"].to_i) unless ids.blank? | |
end | |
# fake a time from: time ago + 1-8770 (a year) hours after | |
def fake_time_from(time_ago = 1.years.ago) | |
now = Time.now | |
range = now.to_i-time_ago.to_i | |
sample = rand(range) | |
time = Time.at(now-sample) | |
end | |
end | |
# the tasks, hook to class above - use like so; | |
# rake fakeout:clean | |
# rake fakeout:small[noprompt] - no confirm prompt asked, useful for heroku or non-interactive use | |
# rake fakeout:medium RAILS_ENV=bananas | |
#.. etc. | |
namespace :fakeout do | |
desc "clean away all data" | |
task :clean, [:no_prompt] => :environment do |t, args| | |
Fakeout.clean(args.no_prompt.nil?) | |
end | |
desc "fake out a tiny dataset" | |
task :tiny, [:no_prompt] => :clean do |t, args| | |
Fakeout.new(:tiny).fakeout | |
end | |
desc "fake out a small dataset" | |
task :small, [:no_prompt] => :clean do |t, args| | |
Fakeout.new(:small).fakeout | |
end | |
desc "fake out a medium dataset" | |
task :medium, [:no_prompt] => :clean do |t, args| | |
Fakeout.new(:medium).fakeout | |
end | |
desc "fake out a large dataset" | |
task :large, [:no_prompt] => :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