Skip to content

Commit e2afa56

Browse files
committed
1 parent e014a0b commit e2afa56

File tree

3 files changed

+45
-39
lines changed

3 files changed

+45
-39
lines changed

Diff for: db/seeds.rb

+1-39
Original file line numberDiff line numberDiff line change
@@ -1,39 +1 @@
1-
# This file should contain all the record creation needed to seed the database with its default values.
2-
# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
3-
#
4-
# Examples:
5-
#
6-
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
7-
# Character.create(name: 'Luke', movie: movies.first)
8-
require 'factory_bot_rails'
9-
10-
11-
unless User.exists?(email: email)
12-
User.create!({name: 'Nick', username: 'hello1', email: email, password: 'hello1234', password_confirmation: 'hello1234'})
13-
end
14-
admin_email = '[email protected]'
15-
unless User.exists?(email: admin_email)
16-
admin = FactoryBot.create :admin, name: 'admin', username: 'admin', email: admin_email, password: 'admin1234', password_confirmation: 'admin1234'
17-
end
18-
19-
user_count = 20
20-
category_count = rand(10..20)
21-
post_count_max = 120
22-
comment_count_max = 300
23-
users = FactoryBot.create_list :user, user_count
24-
25-
threads = []
26-
category_count.times do
27-
threads << Thread.new do
28-
category = FactoryBot.create :category, user: admin
29-
rand(0..post_count_max).times do
30-
post = FactoryBot.create :post, user: users[rand(0...user_count)], category: category
31-
rand(0..comment_count_max).times do
32-
FactoryBot.create :comment, post: post, user: users[rand(0...user_count)]
33-
end
34-
end
35-
ActiveRecord::Base.connection_pool.clear_reloadable_connections!
36-
end
37-
end
38-
39-
threads.each{|t| t.join}
1+
load(Rails.root.join( 'db', 'seeds', "#{Rails.env.downcase}.rb"))

Diff for: db/seeds/development.rb

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require 'factory_bot_rails'
2+
3+
4+
unless User.exists?(email: email)
5+
User.create!({name: 'Nick', username: 'hello1', email: email, password: 'hello1234', password_confirmation: 'hello1234'})
6+
end
7+
8+
admin_email = '[email protected]'
9+
unless User.exists?(email: admin_email)
10+
admin = User.create!({name: 'admin', username: 'admin', email: admin_email, password: 'admin1234', password_confirmation: 'admin1234'})
11+
admin.admin_roles
12+
end
13+
14+
user_count = 20
15+
category_count = rand(10..20)
16+
post_count_max = 120
17+
comment_count_max = 300
18+
users = FactoryBot.create_list :user, user_count
19+
20+
threads = []
21+
category_count.times do
22+
threads << Thread.new do
23+
category = FactoryBot.create :category, user: admin
24+
rand(0..post_count_max).times do
25+
post = FactoryBot.create :post, user: users[rand(0...user_count)], category: category
26+
rand(0..comment_count_max).times do
27+
FactoryBot.create :comment, post: post, user: users[rand(0...user_count)]
28+
end
29+
end
30+
ActiveRecord::Base.connection_pool.clear_reloadable_connections!
31+
end
32+
end
33+
34+
threads.each{|t| t.join}

Diff for: db/seeds/production.rb

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
unless User.exists?(email: email)
3+
User.create!({name: 'Nick', username: 'hello1', email: email, password: 'hello1234', password_confirmation: 'hello1234'})
4+
end
5+
6+
admin_email = '[email protected]'
7+
unless User.exists?(email: admin_email)
8+
admin = User.create!({name: 'admin', username: 'admin', email: admin_email, password: 'admin1234', password_confirmation: 'admin1234'})
9+
admin.admin_roles
10+
end

0 commit comments

Comments
 (0)