From e5bd21823cb9b861207d8802948361b1c2aaadca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20B=C3=B6hm?= Date: Thu, 19 Sep 2024 00:00:47 +0200 Subject: [PATCH] Improved seeds --- db/seeds.rb | 60 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/db/seeds.rb b/db/seeds.rb index 6ea60e6..9111554 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -24,12 +24,14 @@ User.create!(email: "stud2.student@student.hs-rm.de", firstname: "Student2", las # Students with jobs students = [] -5.times do +10.times do firstname = Faker::Name.unique.first_name lastname = Faker::Name.unique.last_name - email="#{firstname}.#{lastname}@student.hs-rm.de".downcase.gsub('ö', 'oe').gsub('ä', 'ae').gsub('ü', 'ue') + # created_at = Faker::Time.backward(days: 60, period: :day) + created_at = Faker::Time.between_dates(from: Date.today - 60, to: Date.today - 30, period: :day) + email="#{firstname}.#{lastname}@student.hs-rm.de".downcase.gsub('ö', 'oe').gsub('ä', 'ae').gsub('ü', 'ue').gsub('ß', 'ss') email.delete(" ") - students << User.new(email: email, firstname: firstname, lastname: lastname, password_digest: BCrypt::Password.create("password"), verified: true) + students << User.new(email: email, firstname: firstname, lastname: lastname, password_digest: BCrypt::Password.create("password"), verified: true, created_at: created_at) students.last.save! end @@ -37,19 +39,61 @@ end 100.times do firstname = Faker::Name.unique.first_name lastname = Faker::Name.unique.last_name - email="#{firstname}.#{lastname}@student.hs-rm.de".downcase.gsub('ö', 'oe').gsub('ä', 'ae').gsub('ü', 'ue') + created_at = Faker::Time.backward(days: 60, period: :day) + # created_at = Faker::Time.between_dates(from: Date.today - 60, to: Date.today, period: :day) + email="#{firstname}.#{lastname}@student.hs-rm.de".downcase.gsub('ö', 'oe').gsub('ä', 'ae').gsub('ü', 'ue').gsub('ß', 'ss') email.delete(" ") - User.new(email: email, firstname: firstname, lastname: lastname, password_digest: BCrypt::Password.create("password"), verified: true).save! + User.new(email: email, firstname: firstname, lastname: lastname, password_digest: BCrypt::Password.create("password"), verified: true, created_at: created_at).save! end -# Jobs -5.times do +# Students without jobs not validated email +10.times do + firstname = Faker::Name.unique.first_name + lastname = Faker::Name.unique.last_name + created_at = Faker::Time.backward(days: 60, period: :day) + # created_at = Faker::Time.between_dates(from: Date.today - 60, to: Date.today, period: :day) + email="#{firstname}.#{lastname}@student.hs-rm.de".downcase.gsub('ö', 'oe').gsub('ä', 'ae').gsub('ü', 'ue').gsub('ß', 'ss') + email.delete(" ") + user=User.new(email: email, firstname: firstname, lastname: lastname, password_digest: BCrypt::Password.create("password"), verified: false, created_at: created_at).save! +end + +# Jobs paid (and some canceled) in the past +2.times do [ 'GanzWichtig.pdf', 'IchBinIn5MinDran.pdf', 'DerPlanDerImmerProblemeMacht.pdf', 'DieFarbenGefallenMirNicht.pdf', 'MachHinIchHabsEilig.pdf', 'WarumDauertDasSoLange.pdf', 'DenPlanBezahleIchNicht.pdf', 'IchWarAlsErstesDran.pdf', 'WarumIstDerPlotterDefekt.pdf', 'DasNächsteMalGeheIchWoAndersHin.pdf' ].shuffle.each do |pdf| - status = %i[open open open open open printing pickup paid canceled].sample + status = %i[paid paid paid paid paid paid paid canceled].sample + created_at = Faker::Time.backward(days: 5, period: :day) + if status == :paid + printed_at = created_at + 30.minutes + paid_at = created_at + 45.minutes + status_changed_at = paid_at + updated_at = status_changed_at + else + updated_at = created_at + rand(4..44).minutes + status_changed_at = updated_at + end + job = Job.new(status:, privacy_policy: true, created_at: created_at) + job.pdf = File.open(Rails.root.join('db/pdfs/', pdf)) + job.costumer = students[rand(0...9)] + job.save! + job.update_column :printed_at, printed_at # write with update_column to avoid before_save action + job.update_column :status_changed_at, status_changed_at # write with update_column to avoid before_save action + job.update_column :paid_at, paid_at if status == :paid # write with update_column to avoid before_save action + job.update_column :updated_at, updated_at # write with update_column to avoid before_save action + end +end +# Jobs +3.times do |i| + status_pool = %i[canceled open open printing printing pickup pickup paid paid paid] + [ 'GanzWichtig.pdf', 'IchBinIn5MinDran.pdf', 'DerPlanDerImmerProblemeMacht.pdf', + 'DieFarbenGefallenMirNicht.pdf', 'MachHinIchHabsEilig.pdf', 'WarumDauertDasSoLange.pdf', + 'DenPlanBezahleIchNicht.pdf', 'IchWarAlsErstesDran.pdf', 'WarumIstDerPlotterDefekt.pdf', + 'DasNächsteMalGeheIchWoAndersHin.pdf' ].shuffle.each do |pdf| + status = status_pool.pop + status = :open if i > 0 job = Job.new(status:, privacy_policy: true) job.pdf = File.open(Rails.root.join('db/pdfs/', pdf)) job.costumer = students[rand(0...4)]