-
Notifications
You must be signed in to change notification settings - Fork 210
Expand file tree
/
Copy pathseeds.rb
More file actions
260 lines (232 loc) · 7.58 KB
/
seeds.rb
File metadata and controls
260 lines (232 loc) · 7.58 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# frozen_string_literal: true
# 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).
require 'csv'
def load_data
Crop.transaction do
# for all Growstuff sites, including production ones
load_roles
create_cropbot
load_crops
load_plant_parts
# We don't load these in an environment except development to
# prevent creating users in the wild - especially admins - with
# known passwords.
if Rails.env.development?
load_test_users
load_admin_users
end
end
puts "Done!"
end
def load_crops
source_path = Rails.root.join("db/seeds")
Dir.glob("#{source_path}/crops*.csv").each do |crop_file|
puts "Loading crops from #{crop_file}..."
CSV.foreach(crop_file) do |row|
CsvImporter.new.import_crop(row)
end
end
puts "Finished loading crops"
end
def load_roles
puts "Creating admin role..."
@admin = Role.create(name: 'Admin')
puts "Creating crop wrangler role..."
@wrangler = Role.create(name: 'Crop Wrangler')
end
def load_test_users
require "faker"
puts "Loading test users..."
# Open suburb csv
source_path = Rails.root.join("db/seeds")
begin
suburb_file = File.open("#{source_path}/suburbs.csv")
rescue StandardError
puts "Warning: unable to open suburbs.csv"
end
# rake parameter (eg. 'rake db:seed member_size=10')
member_size = ENV['member_size'] ? ENV['member_size'].to_i : 3
(1..member_size).each do |i|
@user = Member.new(
login_name: "test#{i}",
email: "test#{i}@example.com",
password: "password#{i}",
tos_agreement: true
)
@user.skip_confirmation!
@user.save!
# Populate member location and garden location
if suburb_file
suburb_file.pos = 0 if suburb_file.eof?
row = CSV.parse(suburb_file.readline)
suburb, _country, _state, latitude, longitude = row[0]
# Using 'update_column' method instead of 'update' so that
# it avoids accessing Geocoding service for faster processing
@user.gardens.first.update_columns(location: suburb, latitude:, longitude:)
@user.update_columns(location: suburb, latitude:, longitude:)
end
# Create a planting by the member
10.times do |n|
planting = Planting.create!(
owner_id: @user.id,
garden_id: @user.gardens.first.id,
planted_at: (n * 7).days.ago,
crop_id: Crop.find(((i + n) % Crop.all.size) + 1).id,
sunniness: select_random_item(Planting::SUNNINESS_VALUES),
planted_from: select_random_item(Planting::PLANTED_FROM_VALUES)
)
photo = Photo.create!(
owner: @user,
source: 'flickr',
source_id: 1,
title: Faker::Movies::HarryPotter.quote,
license_name: "CC-BY",
license_url: "http://example.com/license.html",
thumbnail_url: "https://picsum.photos/200?planting-#{planting.id}",
fullsize_url: "https://picsum.photos/600?planting-#{planting.id}",
link_url: Faker::Internet.url
)
planting.photos << photo
end
# Create an activity by the member
3.times do |_n|
Activity.create!(
owner_id: @user.id,
garden_id: @user.gardens.first.id,
due_date: Time.zone.today,
name: Faker::Book.title,
category: "Weeding",
description: Faker::Lorem.paragraphs.join("\n")
)
end
# Create a post by the member
post = Post.create!(
author_id: @user.id,
subject: Faker::Book.title,
body: Faker::Lorem.paragraphs.join("\n")
)
photo = Photo.create!(
owner: @user,
source: 'flickr',
source_id: 1,
title: Faker::Movies::HarryPotter.quote,
license_name: "CC-BY",
license_url: "http://example.com/license.html",
thumbnail_url: "https://picsum.photos/200?post-#{post.id}",
fullsize_url: "https://picsum.photos/600?post-#{post.id}",
link_url: Faker::Internet.url
)
post.photos << photo
2.times do
harvest = Harvest.create!(
crop: @user.plantings.last.crop,
planting: @user.plantings.last,
plant_part: select_random_item(PlantPart.all.to_a),
owner: @user,
harvested_at: 1.day.ago,
quantity: "3",
unit: "individual",
weight_quantity: 6,
weight_unit: "kg",
description: Faker::Book.title
)
photo = Photo.create!(
owner: @user,
source: 'flickr',
source_id: 1,
title: Faker::Movies::HarryPotter.quote,
license_name: "CC-BY",
license_url: "http://example.com/license.html",
thumbnail_url: "https://picsum.photos/200?harvest-#{harvest.id}",
fullsize_url: "https://picsum.photos/600?harvest-#{harvest.id}",
link_url: Faker::Internet.url
)
harvest.photos << photo
end
5.times do
seed = Seed.create!(
owner: @user,
crop: @user.plantings.first.crop,
description: Faker::Book.title,
quantity: Faker::Number.number(digits: 3),
tradable_to: select_random_item(Seed::TRADABLE_TO_VALUES),
organic: select_random_item(Seed::ORGANIC_VALUES),
gmo: select_random_item(['certified GMO-free', 'non-certified GMO-free', 'GMO', 'unknown']), # Strangely, this doesn't want to work as Seed:GMO_VALUES
heirloom: select_random_item(Seed::HEIRLOOM_VALUES),
parent_planting: @user.plantings.first,
finished: false
)
photo = Photo.create!(
owner: @user,
source: 'flickr',
source_id: 1,
title: Faker::Movies::HarryPotter.quote,
license_name: "CC-BY",
license_url: "http://example.com/license.html",
thumbnail_url: "https://picsum.photos/200?seed-#{seed.id}",
fullsize_url: "https://picsum.photos/600?seed-#{seed.id}",
link_url: Faker::Internet.url
)
seed.photos << photo
end
end
puts "Finished loading test users"
end
def load_admin_users
puts "Adding admin and crop wrangler members..."
@admin_user = Member.new(
login_name: "admin1",
email: "[email protected]",
password: "password1",
tos_agreement: true
)
@admin_user.skip_confirmation!
@admin_user.roles << @admin
@admin_user.save!
@wrangler_user = Member.new(
login_name: "wrangler1",
email: "[email protected]",
password: "password1",
tos_agreement: true
)
@wrangler_user.skip_confirmation!
@wrangler_user.roles << @wrangler
@wrangler_user.save!
end
def create_cropbot
return if Member.find_by(login_name: 'cropbot')
@cropbot_user = Member.new(
login_name: "cropbot",
email: Rails.application.config.bot_email,
password: SecureRandom.urlsafe_base64(64),
tos_agreement: true
)
@cropbot_user.skip_confirmation!
@cropbot_user.roles << @wrangler
@cropbot_user.save!
end
def load_plant_parts
puts "Loading plant parts..."
plant_parts = [
'fruit',
'flower',
'seed',
'pod',
'leaf',
'stem',
'bark',
'bulb',
'root',
'tuber',
'whole plant',
'other'
]
plant_parts.each do |pp|
PlantPart.find_or_create_by!(name: pp)
end
end
def select_random_item(array)
array[rand(0..array.size - 1) % array.size]
end
load_data