Skip to content

Commit c995266

Browse files
committed
Added verbose (failure) logging to the seeding process
1 parent b029f49 commit c995266

File tree

7 files changed

+147
-111
lines changed

7 files changed

+147
-111
lines changed

db/seeds.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,4 @@
9494
require Rails.root.join('db', 'seeds', 'payments.rb')
9595
require Rails.root.join('db', 'seeds', 'posts.rb')
9696

97-
puts '-- done'
97+
puts '-- Done'

db/seeds/activities.rb

+40-37
Original file line numberDiff line numberDiff line change
@@ -16,50 +16,53 @@
1616
# 2: Single day, all day
1717
# 3: Multiple days, with start and end time
1818
# 4: Single day, with start and end time
19-
@multiple = Faker::Boolean.boolean
20-
@single = !@multiple
21-
@entire = Faker::Boolean.boolean
22-
@part = !@entire
19+
@multiple = Faker::Boolean.boolean
20+
@single = !@multiple
21+
@entire = Faker::Boolean.boolean
22+
@part = !@entire
2323

24-
viewable = Faker::Boolean.boolean(true_ratio: 0.9)
24+
viewable = Faker::Boolean.boolean(true_ratio: 0.9)
2525
enrollable = viewable ? Faker::Boolean.boolean(true_ratio: 0.9) : false
2626
notes = Faker::Boolean.boolean(true_ratio: 0.2) ? Faker::Lorem.question : nil
2727

2828
activity = Activity.create(
29-
name: Faker::Hacker.ingverb.capitalize,
30-
price: Faker::Commerce.price / 5,
29+
name: Faker::Hacker.ingverb.capitalize,
30+
price: Faker::Commerce.price / 5,
3131

3232
# TODO: use date as timefield as well in model, perhaps add boolean for all day
33-
start_date: start_date,
34-
start_time: @part ? start_date.to_time : nil,
35-
36-
end_date: @multiple ? Faker::Date.between(from: start_date + 1.day, to: start_date + 7.days) : nil,
37-
end_time: @part ? Faker::Time.between_dates(from: start_date, to: Date.today, period: :evening) : nil,
38-
39-
location: Faker::TvShows::FamilyGuy.location,
40-
organized_by: Faker::Boolean.boolean(true_ratio: 0.8) ? Group.all.sample.id : nil,
41-
description_nl: Faker::Lorem.paragraph(sentence_count: 5),
42-
description_en: Faker::Lorem.paragraph(sentence_count: 5),
43-
44-
is_enrollable: enrollable,
45-
is_masters: Faker::Boolean.boolean(true_ratio: 0.2),
46-
is_viewable: viewable,
47-
is_alcoholic: Faker::Boolean.boolean(true_ratio: 0.2),
48-
is_freshmans: Faker::Boolean.boolean(true_ratio: 0.2),
49-
is_sophomores: Faker::Boolean.boolean(true_ratio: 0.2),
50-
is_seniors: Faker::Boolean.boolean(true_ratio: 0.2),
51-
is_payable: Faker::Boolean.boolean(true_ratio: 0.8),
33+
start_date: start_date,
34+
start_time: @part ? start_date.to_time : nil,
35+
36+
end_date: @multiple ? Faker::Date.between(from: start_date + 1.day, to: start_date + 7.days) : nil,
37+
end_time: @part ? Faker::Time.between_dates(from: start_date, to: Date.today, period: :evening) : nil,
38+
39+
location: Faker::TvShows::FamilyGuy.location,
40+
organized_by: Faker::Boolean.boolean(true_ratio: 0.8) ? Group.all.sample.id : nil,
41+
description_nl: Faker::Lorem.paragraph(sentence_count: 5),
42+
description_en: Faker::Lorem.paragraph(sentence_count: 5),
43+
44+
is_enrollable: enrollable,
45+
is_masters: Faker::Boolean.boolean(true_ratio: 0.2),
46+
is_viewable: viewable,
47+
is_alcoholic: Faker::Boolean.boolean(true_ratio: 0.2),
48+
is_freshmans: Faker::Boolean.boolean(true_ratio: 0.2),
49+
is_sophomores: Faker::Boolean.boolean(true_ratio: 0.2),
50+
is_seniors: Faker::Boolean.boolean(true_ratio: 0.2),
51+
is_payable: Faker::Boolean.boolean(true_ratio: 0.8),
5252

5353
participant_limit: enrollable && Faker::Boolean.boolean(true_ratio: 0.5) ? Faker::Number.within(range: 2..18) : nil,
5454

55-
VAT: ["0","9","21"].sample,
55+
VAT: ["0", "9", "21"].sample,
5656

57-
notes: notes,
58-
notes_mandatory: !notes.nil? ? Faker::Boolean.boolean(true_ratio: 0.2) : false,
59-
notes_public: !notes.nil? ? Faker::Boolean.boolean(true_ratio: 0.6) : false
57+
notes: notes,
58+
notes_mandatory: !notes.nil? ? Faker::Boolean.boolean(true_ratio: 0.2) : false,
59+
notes_public: !notes.nil? ? Faker::Boolean.boolean(true_ratio: 0.6) : false
6060
)
6161

62-
puts(" -> #{ activity.name } (#{ start_date })#{', enrollable' if enrollable}" )
62+
puts " [#{ activity.valid? ? ' Ok ' : 'Fail' }] #{ activity.name } (#{ start_date })#{ ', enrollable' if enrollable }"
63+
activity.errors.objects.each do |error|
64+
puts " > #{error.full_message}"
65+
end
6366

6467
activity.poster.attach(io: File.open('public/poster-example.pdf'), filename: 'poster-example.pdf', content_type: 'application/pdf')
6568

@@ -77,12 +80,12 @@
7780

7881
eligible.sample(Faker::Number.within(range: 18..22)).each do |member|
7982
Participant.create(
80-
member: member,
81-
activity: activity,
82-
reservist: true,
83-
price: (Faker::Boolean.boolean(true_ratio: 0.2) ? Faker::Commerce.price / 5 : nil),
84-
paid: Faker::Boolean.boolean(true_ratio: 0.4), # if price is 0 then the paid attribute is not used
85-
notes: response
83+
member: member,
84+
activity: activity,
85+
reservist: true,
86+
price: (Faker::Boolean.boolean(true_ratio: 0.2) ? Faker::Commerce.price / 5 : nil),
87+
paid: Faker::Boolean.boolean(true_ratio: 0.4), # if price is 0 then the paid attribute is not used
88+
notes: response
8689
)
8790
end
8891

db/seeds/groups.rb

+16-8
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,27 @@
44
puts '-- Creating committees'
55
8.times do
66
group = Group.create(
7-
name: Faker::Team.unique.name,
8-
category: 2,
7+
name: Faker::Team.unique.name,
8+
category: 2,
99
created_at: Faker::Date.between(from: 3.years.ago, to: Date.today - 1.year)
1010
)
1111

12+
puts " [#{ group.valid? ? ' Ok ' : 'Fail' }] #{ group.name } (#{ group.created_at.study_year }-#{ Date.today.study_year })"
13+
group.errors.objects.each do |error|
14+
puts " > #{error.full_message}"
15+
end
16+
1217
Member.all.sample(10).each do |member|
13-
GroupMember.create(
14-
member: member,
15-
group: group,
16-
year: Faker::Date.between(from: [member.join_date, group.created_at].max, to: Date.today).study_year,
18+
group_member = GroupMember.create(
19+
member: member,
20+
group: group,
21+
year: Faker::Date.between(from: [member.join_date, group.created_at].max, to: Date.today).study_year,
1722
position: Faker::Boolean.boolean(true_ratio: 0.5) ? group.positions.sample : nil
1823
)
19-
end
2024

21-
puts(" -> #{ group.name } (#{ group.created_at.study_year }-#{ Date.today.study_year })")
25+
puts " [#{ group_member.valid? ? ' Ok ' : 'Fail' }] + #{ group_member.member.first_name } #{ group_member.member.last_name }"
26+
group_member.errors.objects.each do |error|
27+
puts " > #{error.full_message}"
28+
end
29+
end
2230
end

db/seeds/members.rb

+28-20
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,52 @@
22
puts '-- Creating members'
33
60.times do
44
first_name = Faker::Name.first_name
5-
last_name = Faker::Name.last_name
5+
last_name = Faker::Name.last_name
66

77
member = Member.create(
8-
first_name: first_name,
9-
infix: (Faker::Number.within(range: 1..10) > 7 ? Faker::Name.tussenvoegsel : ' '),
10-
last_name: last_name,
11-
address: Faker::Address.street_name,
8+
first_name: first_name,
9+
infix: (Faker::Number.within(range: 1..10) > 7 ? Faker::Name.tussenvoegsel : ' '),
10+
last_name: last_name,
11+
address: Faker::Address.street_name,
1212
house_number: Faker::Address.building_number,
13-
postal_code: Faker::Address.postcode,
14-
city: Faker::Address.city,
13+
postal_code: Faker::Address.postcode,
14+
city: Faker::Address.city,
1515
phone_number: Faker::Base.numerify('+316########'),
1616
emergency_phone_number: Faker::Base.numerify('+316########'),
17-
email: Faker::Internet.safe_email(name: (first_name + '.' + last_name).parameterize),
18-
student_id: "F#{ Faker::Number.number(digits: 6) }",
19-
birth_date: Faker::Date.between(from: 28.years.ago, to: 16.years.ago),
20-
join_date: Faker::Date.between(from: 6.years.ago, to: Date.today),
21-
comments: (Faker::Boolean.boolean(true_ratio: 0.3) ? Faker::Hacker.say_something_smart : nil)
17+
email: Faker::Internet.safe_email(name: (first_name + '.' + last_name).parameterize),
18+
student_id: "F#{ Faker::Number.number(digits: 6) }",
19+
birth_date: Faker::Date.between(from: 28.years.ago, to: 16.years.ago),
20+
join_date: Faker::Date.between(from: 6.years.ago, to: Date.today),
21+
comments: (Faker::Boolean.boolean(true_ratio: 0.3) ? Faker::Hacker.say_something_smart : nil)
2222
)
2323

24-
puts(" -> #{ member.name } (#{ member.student_id })")
24+
puts " [#{ member.valid? ? ' Ok ' : 'Fail' }] #{ member.name } (#{ member.student_id })"
25+
member.errors.objects.each do |error|
26+
puts " > #{error.full_message}"
27+
end
2528
end
2629

2730
puts '-- Creating educations'
2831
Member.all.each do |member|
2932
Faker::Number.within(range: 1..3).times do
3033
# because of the [member, study, start_date] key this goes wrong often
3134
suppress(ActiveRecord::RecordNotUnique) do
32-
status = Faker::Number.within(range: 0..2)
35+
status = Faker::Number.within(range: 0..2)
3336
start_date = Faker::Date.between(from: member.join_date, to: Date.today)
34-
end_date = (status > 0 ? Faker::Date.between(from: start_date, to: Date.today) : nil)
37+
end_date = (status > 0 ? Faker::Date.between(from: start_date, to: Date.today) : nil)
3538

36-
Education.create(
37-
member_id: member.id,
38-
study_id: Study.all.sample.id,
39+
education = Education.create(
40+
member_id: member.id,
41+
study_id: Study.all.sample.id,
3942
start_date: start_date,
40-
end_date: end_date,
41-
status: status
43+
end_date: end_date,
44+
status: status
4245
)
46+
47+
puts " [#{ education.valid? ? ' Ok ' : 'Fail' }] #{ education.study_id } for #{ education.member_id }"
48+
education.errors.objects.each do |error|
49+
puts " > #{error.full_message}"
50+
end
4351
end
4452
end
4553
end

db/seeds/payments.rb

+31-30
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
11
require Rails.root.join('db', 'seeds', 'members.rb')
22
require Rails.root.join('db', 'seeds', 'activities.rb')
33

4-
puts '-- member transaction creation'
4+
puts '-- Creating member transactions'
55

66
Member.all.sample(30).each do |member|
7-
87
15.times do
9-
transactiontype = Faker::Number.within(range: 0..1)
10-
# either ideal or pin (see app/models/payment.rb)
11-
paymenttype = Faker::Number.within(range: 0.0..1.0) < 0.5 ? 0 : 3
12-
status = Faker::Number.within(range:0..2)
13-
if transactiontype == 1 && status == 0
14-
participants = Participant.where(member:member).where.not(activity:[nil,1]).select{|p| p.currency != nil}.sample(Faker::Number.within(range:1..6))
15-
participants.map {|p|p.update(paid:true)}
16-
transaction_id = participants.map{|p| p.activity.id}
17-
amount = 0
18-
participants.map{ |p| amount += p.currency}
19-
status = 2
20-
else
21-
transaction_id = [1]
22-
status = 0
23-
end
24-
description = transactiontype == 0 ? "Mongoose Opwaardering" : "Activiteiten - #{transaction_id}"
25-
26-
@payment =Payment.create(member: member,
27-
transaction_type: transactiontype,
28-
payment_type: paymenttype ,
29-
transaction_id: transaction_id,
30-
description: description,
31-
amount: Faker::Number.within(range: 1.0..10.0),
32-
status: status,
33-
trxid: Digest::MD5.hexdigest(description + Time.now.to_f.to_s),
34-
created_at: Faker::Date.between(from: 1.weeks.ago, to: Date.today),
35-
message: 'seeding',
36-
redirect_uri: ENV['KOALA_DOMAIN']
8+
status = Faker::Number.within(range: 0..2)
9+
payment_type = Faker::Number.within(range: 0.0..1.0) < 0.5 ? 0 : 3
10+
11+
participants = Participant.where(member: member).where.not(activity: [nil, 1]).select { |p| p.currency != nil }.sample(Faker::Number.within(range: 1..6))
12+
13+
participants.map { |p| p.update(paid: true) }
14+
15+
amount = 0
16+
participants.map { |p| amount += p.currency }
17+
18+
transaction_id = participants.map{|p| p.activity.id}
19+
20+
description = "Activiteiten - #{transaction_id}"
21+
22+
payment = Payment.create(member: member,
23+
transaction_type: 1,
24+
payment_type: payment_type,
25+
transaction_id: transaction_id,
26+
description: description,
27+
amount: amount,
28+
status: status,
29+
trxid: Digest::MD5.hexdigest(description + Time.now.to_f.to_s),
30+
created_at: Faker::Date.between(from: 1.weeks.ago, to: Date.today),
31+
message: 'seeding',
32+
redirect_uri: ENV['KOALA_DOMAIN']
3733
)
34+
35+
puts " [#{ payment.valid? ? ' Ok ' : 'Fail' }] #{['Failed', 'In progress', 'Successful'][status]} transaction by #{ payment.member.first_name } #{ payment.member.last_name } of #{ payment.amount } for #{ payment.transaction_id } using #{ payment_type == 0 ? "ideal" : "pin" }"
36+
payment.errors.objects.each do |error|
37+
puts " > #{error.full_message}"
38+
end
3839
end
3940
end

db/seeds/posts.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
6.times do
66
created_at = Faker::Time.between(from: 2.years.ago, to: Date.yesterday)
77

8-
Post.create(
8+
post = Post.create(
99
title: Faker::Movie.title,
10-
status: [0,1].sample,
10+
status: [0, 1].sample,
1111
tags: Faker::Lorem.sentence(word_count: 2),
1212

1313
author: Admin.all.sample,
@@ -18,4 +18,8 @@
1818
content: Faker::Lorem.paragraph(sentence_count: 5)
1919
)
2020

21+
puts " [#{ post.valid? ? ' Ok ' : 'Fail' }] #{ post.title } by #{ post.author.first_name } #{ post.author.last_name }"
22+
post.errors.objects.each do |error|
23+
puts " > #{error.full_message}"
24+
end
2125
end

db/seeds/users.rb

+25-13
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,46 @@
22

33
# Creating login accounts
44
puts '-- Creating users'
5-
Admin.create(
6-
5+
6+
dev = Admin.create(
7+
78
password: 'sticky123'
89
)
9-
puts ' -> [email protected] (admin)'
1010

11-
Admin.create(
12-
13-
password: 'sticky123',
11+
puts " [#{ dev.valid? ? ' Ok ' : 'Fail' }] [email protected] (admin)"
12+
dev.errors.objects.each do |error|
13+
puts " > #{error.full_message}"
14+
end
15+
16+
martijn = Admin.create(
17+
18+
password: 'sticky123',
1419

1520
first_name: 'Martijn',
16-
last_name: 'Casteel',
17-
signature: 'Martijn'
21+
last_name: 'Casteel',
22+
signature: 'Martijn'
1823
)
19-
puts ' -> [email protected] (admin)'
24+
25+
puts " [#{ martijn.valid? ? ' Ok ' : 'Fail' }] [email protected] (admin)"
26+
martijn.errors.objects.each do |error|
27+
puts " > #{error.full_message}"
28+
end
2029

2130
# create actual members, but give the first member [email protected]
2231
member = Member.joins(:educations).where(educations: { status: 0 }).first
2332
member.update(email: '[email protected]')
2433

2534
user = User.new(
26-
email: member.email,
27-
password: 'sticky123',
35+
email: member.email,
36+
password: 'sticky123',
2837
credentials: member,
29-
language: Faker::Number.within(range: 0..1)
38+
language: Faker::Number.within(range: 0..1)
3039
)
3140

3241
user.skip_confirmation!
3342
user.save!
3443

35-
puts " -> #{ user.email } (member)"
44+
puts " [#{ user.valid? ? ' Ok ' : 'Fail' }] #{ user.email } (member)"
45+
user.errors.objects.each do |error|
46+
puts " > #{error.full_message}"
47+
end

0 commit comments

Comments
 (0)