forked from activeadmin/demo.activeadmin.info
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseeds.rb
61 lines (51 loc) · 1.52 KB
/
seeds.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
# cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }])
# Mayor.create(:name => 'Daley', :city => cities.first)
#
# Create default admin user
AdminUser.create! do |a|
a.email = '[email protected]'
a.password = a.password_confirmation = 'password'
end
# Create default user
User.create! do |u|
u.username = 'user'
u.email = '[email protected]'
u.password = u.password_confirmation = 'password'
end
# Load each product from the yaml file
YAML.load_file(File.expand_path("../seeds/products.yml", __FILE__)).each do |product|
Product.create! product
end
NB_PRODUCTS = Product.count
# Create 100 users
NB_USERS = 100
NB_USERS.times do |n|
User.create! do |u|
u.username = Faker::Internet.user_name + n.to_s
u.email = Faker::Internet.email.gsub('@', "#{n}@")
u.password = u.password_confirmation = 'password'
end
end
# Create 300 Orders
NB_ORDERS = 300
NB_ORDERS.times do
user_id = rand(NB_USERS - 1) + 1
user = User.find(user_id)
order = user.orders.create!
nb_items = rand(9) + 1
nb_items.times do
product_id = rand(NB_PRODUCTS - 1) + 1
product = Product.find(product_id)
LineItem.create! do |l|
l.order = order
l.product = product
l.price = product.price
end
end
order.recalculate_price! and order.checkout! if rand(100) < 90
end