-
Notifications
You must be signed in to change notification settings - Fork 25
/
games.rb
45 lines (39 loc) · 1.09 KB
/
games.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
# Fetches a cover image from LoremPixel, or uses an image from the test suite
# if in CI, to avoid external HTTP requests.
def cover_fetcher
if ENV['CI']
File.open('./spec/factories/images/crysis.jpg')
else
# TODO: Make the dimensions more random.
URI.open("#{Faker::LoremPixel.image('560x800', false)}/")
end
end
puts "Creating Games..."
# Create 50 random Games.
50.times do |n|
genres = []
rand(0..3).times.each do
genres << Genre.find(rand(1..Genre.count))
end
genres.uniq!
engines = []
rand(0..3).times.each do
engines << Engine.find(rand(1..Engine.count))
end
engines.uniq!
# The name needs a randomly generated number appended to make sure it
# stays unique and doesn't run out of options.
game = Game.create!(
name: "#{Faker::Game.name}#{Faker::Number.number(10)}",
description: Faker::Lorem.sentence,
genres: genres,
engines: engines,
series: Series.find(rand(1..Series.count))
)
next unless rand(0..4) != 0
# Add a cover for most games.
game.cover.attach(
io: cover_fetcher,
filename: "#{n}_faker_cover.jpg"
)
end