From 5eb83f62ff2649e0c2b16c1217cd4eb698198a2e Mon Sep 17 00:00:00 2001 From: mo khan Date: Sat, 28 Mar 2020 14:18:08 -0600 Subject: generate seed data --- rvh/Gemfile | 1 + rvh/Gemfile.lock | 3 ++ rvh/app/models/treatment.rb | 2 +- rvh/db/seeds.rb | 69 ++++++++++++++++++++++++++++++++++++--------- 4 files changed, 60 insertions(+), 15 deletions(-) diff --git a/rvh/Gemfile b/rvh/Gemfile index 0e592c9..f867dca 100644 --- a/rvh/Gemfile +++ b/rvh/Gemfile @@ -1,5 +1,6 @@ source 'https://rubygems.org' +gem 'faker' gem 'pg' gem 'rails', '~> 6.0' gem 'rails-erd', '~> 1.6' diff --git a/rvh/Gemfile.lock b/rvh/Gemfile.lock index b7dd29d..2048c9a 100644 --- a/rvh/Gemfile.lock +++ b/rvh/Gemfile.lock @@ -61,6 +61,8 @@ GEM concurrent-ruby (1.1.6) crass (1.0.6) erubi (1.9.0) + faker (2.11.0) + i18n (>= 1.6, < 2) globalid (0.4.2) activesupport (>= 4.2.0) i18n (1.8.2) @@ -140,6 +142,7 @@ PLATFORMS ruby DEPENDENCIES + faker pg rails (~> 6.0) rails-erd (~> 1.6) diff --git a/rvh/app/models/treatment.rb b/rvh/app/models/treatment.rb index c7f6ea3..4b64de1 100644 --- a/rvh/app/models/treatment.rb +++ b/rvh/app/models/treatment.rb @@ -1,4 +1,4 @@ class Treatment < ApplicationRecord - belongs_to :physician + has_one :physician, through: :patient belongs_to :patient end diff --git a/rvh/db/seeds.rb b/rvh/db/seeds.rb index d7507dd..56eba96 100644 --- a/rvh/db/seeds.rb +++ b/rvh/db/seeds.rb @@ -1,3 +1,42 @@ +def create_account + Account.create!({ + name: Faker::Name.name, + address: Faker::Address.street_address, + birth_date: Faker::Date.birthday(min_age: 18, max_age: 65) + }) +end + +def random_physician + if Physician.any? && Time.now.to_i.even? + return Physician.order(Arel.sql('RANDOM()')).first + end + + Physician.create!({ + account: create_account, + hired_at: 1.month.ago, + qualifications: ['heart', 'brain'] + }) +end + +def create_patient + Patient.create!( + account: create_account, + physician: random_physician, + referring_physician: random_physician, + contacted_at: Faker::Date.between(from: 6.months.ago, to: 1.hour.ago) + ) +end + +def random_patient + return Patient.order(Arel.sql('RANDOM()')).first if Patient.any? && Time.now.to_i.odd? + + create_patient +end + +def random_count + rand(100) +end + ApplicationRecord.transaction do [ { name: 'Intensive Care Unit', location: 'MT-M' }, @@ -28,24 +67,26 @@ ApplicationRecord.transaction do { name: 'Orthopedics', location: 'MT-6' }, { name: 'Transition', location: 'FMC-7' }, ].each do |attributes| - nurse = Nurse.create!({ - account: Account.create!({ name: "Nurse #{Time.now.nsec}", address: '123 1 street NW', birth_date: 30.years.ago }), - hired_at: 1.year.ago, - qualifications: ['medicine'] - }) + nurse = Nurse.create!({ account: create_account, hired_at: 1.year.ago, qualifications: ['medicine'] }) care_centre = CareCentre.create!(attributes.merge(nurse_in_charge: nurse)) - Bed.create!(rand(100).times.map { |n| { care_centre: care_centre, room_number: n.to_s } }) + Bed.create!(random_count.times.map { |n| { care_centre: care_centre, room_number: n.to_s } }) + end + + random_count.times do + create_patient end - Physician.create!([ - { - account: Account.create!({ name: 'nini', address: '234 1 street NW', birth_date: 10.years.ago }), - hired_at: 1.month.ago, - qualifications: ['heart', 'brain'] - } - ]) + random_count.times do + patient = random_patient - Patient.create!([ ]) + Treatment.create!( + patient: patient, + name: Faker::FunnyName.name, + number: SecureRandom.uuid, + occurred_at: Faker::Date.between(from: patient.contacted_at, to: 1.hour.ago), + results: Faker::Lorem.paragraphs, + ) + end end CareCentre.find_each do |centre| -- cgit v1.2.3