-
-
Save cloud-on-prem/2783959 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)
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
# 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 => '[email protected]' }, true) | |
# fake products | |
faker.products(12) | |
faker.products(4, { :price => 0 }) | |
# report | |
puts "Faked!\n#{faker.report}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment