Skip to content

Instantly share code, notes, and snippets.

@sescobb27
Created May 17, 2015 23:36
Show Gist options
  • Save sescobb27/5c523dc57d9f5fb0d5f4 to your computer and use it in GitHub Desktop.
Save sescobb27/5c523dc57d9f5fb0d5f4 to your computer and use it in GitHub Desktop.
# spec/factories/categories.rb
require 'ffaker'
FactoryGirl.define do
factory :category do
name { Category::CATEGORIES.sample }
description FFaker::Lorem.sentences
end
end
# spec/factories/clients.rb
require 'ffaker'
require 'securerandom'
FactoryGirl.define do
factory :client do
categories do
tmp = Category::CATEGORIES.sample(2)
[Category.new(name: tmp[0]), Category.new(name: tmp[1])]
end
locations { (1..5).map { FactoryGirl.build(:location) } }
username do
"#{SecureRandom.base64(4)}#{FFaker::Internet.user_name}".downcase
end
email { "#{SecureRandom.base64(4)}#{FFaker::Internet.email}".downcase }
password 'qwertyqwerty'
name { "#{FFaker::Company.name}#{SecureRandom.base64(4)}" }
rates []
image_url { FFaker::Internet.http_url }
addresses { (1..5).map { FFaker::Address.street_address } }
token { [SecureRandom.base64] }
factory :client_with_discounts do
discounts { (1..5).map { FactoryGirl.build(:discount) } }
end
factory :client_with_plan do
client_plans do
plan = Plan.all.sample
[plan.to_client_plan]
end
end
factory :client_with_expired_plan do
client_plans do
plan = Plan.all.sample
plan = plan.to_client_plan
plan.expired_date = Time.zone.now - 1.minute
[plan]
end
end
end
end
# spec/factories/discounts.rb
require 'ffaker'
FactoryGirl.define do
factory :discount do
after(:build) { |discount| discount.categories.concat( (1..2).map { FactoryGirl.build(:category) }) }
discount_rate { rand(60) }
duration_term 'minutes'
title { FFaker::Product.product }
secret_key { "#{title}_secret_key" }
status true
duration { Discount::DURATIONS.sample }
created_at Time.now
hashtags { (1..5).map { "##{FFaker::Lorem.word}" }}
end
end
# rake db:seed
require 'ffaker'
require 'factory_girl_rails'
puts 'Creating 100 random clients with discounts'
threads = (1..10).map do
Thread.new do
client_with_discounts = (1..10).map do
FactoryGirl.attributes_for :client_with_discounts
end
Client.create! client_with_discounts
end
end
threads.map!(&:join)
puts 'End creating fixed clients'
time rake db:seed
Creating 100 random clients with discounts
Finished creating 100 random clients with discounts
real 3m24.685s
user 3m24.583s
sys 0m0.372s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment