demo_email = 'demo@localhost' User.transaction do break if User.find_by(email: demo_email) demo = User.create! email: demo_email, password: 'demo123', status: :active do |user| user.skip_confirmation! print "Creating demo account '#{user.email}' with password '#{user.password}'..." end puts "done." # --- Units --- u = {} u[:kg] = demo.units.create! symbol: 'kg', description: 'kilogram' u[:g] = demo.units.create! symbol: 'g', description: 'gram', base: u[:kg], multiplier: 1e-3 u[:cm] = demo.units.create! symbol: 'cm', description: 'centimetre' u[:bpm] = demo.units.create! symbol: 'bpm', description: 'beats per minute' u[:pct] = demo.units.create! symbol: '%', description: 'percent' u[:h] = demo.units.create! symbol: 'h', description: 'hour' u[:min] = demo.units.create! symbol: 'min', description: 'minute', base: u[:h], multiplier: (1.0/60).round(10) u[:kcal]= demo.units.create! symbol: 'kcal',description: 'kilocalorie' u[:mg] = demo.units.create! symbol: 'mg', description: 'milligram', base: u[:kg], multiplier: 1e-6 u[:mmhg]= demo.units.create! symbol: 'mmHg',description: 'millimetre of mercury' # --- Quantities --- body = demo.quantities.create! name: 'Body' weight = demo.quantities.create! name: 'Weight', parent: body height = demo.quantities.create! name: 'Height', parent: body fat = demo.quantities.create! name: 'Body fat', parent: body cardio = demo.quantities.create! name: 'Cardiovascular' hr_rest = demo.quantities.create! name: 'Resting HR', parent: cardio hr_peak = demo.quantities.create! name: 'Peak HR', parent: cardio bp_sys = demo.quantities.create! name: 'BP systolic', parent: cardio bp_dia = demo.quantities.create! name: 'BP diastolic', parent: cardio activity = demo.quantities.create! name: 'Activity' sleep_dur = demo.quantities.create! name: 'Sleep', parent: activity calories = demo.quantities.create! name: 'Calories out', parent: activity nutrition = demo.quantities.create! name: 'Nutrition' cal_in = demo.quantities.create! name: 'Calories in', parent: nutrition caffeine = demo.quantities.create! name: 'Caffeine', parent: nutrition # --- Readouts (60 days of daily-ish data) --- base_time = 60.days.ago.beginning_of_day rng = Random.new(42) weight_val = 82.4 fat_val = 21.5 hr_rest_val = 62.0 60.times do |i| t = base_time + i.days + rng.rand(3600 * 2) weight_val += rng.rand(-0.3..0.3) fat_val += rng.rand(-0.15..0.15) hr_rest_val += rng.rand(-1.5..1.5) hr_rest_val = hr_rest_val.clamp(52, 72) demo.readouts.create! quantity: weight, unit: u[:kg], value: weight_val.round(1), created_at: t demo.readouts.create! quantity: fat, unit: u[:pct], value: fat_val.round(1), created_at: t demo.readouts.create! quantity: hr_rest, unit: u[:bpm], value: hr_rest_val.round, created_at: t if i % 2 == 0 demo.readouts.create! quantity: bp_sys, unit: u[:mmhg], value: (115 + rng.rand(-8..8)).round, created_at: t demo.readouts.create! quantity: bp_dia, unit: u[:mmhg], value: (75 + rng.rand(-5..5)).round, created_at: t end if i % 3 == 0 demo.readouts.create! quantity: hr_peak, unit: u[:bpm], value: (155 + rng.rand(-10..10)).round, created_at: t end demo.readouts.create! quantity: sleep_dur, unit: u[:h], value: (6.5 + rng.rand(-1.5..1.5)).round(1), created_at: t demo.readouts.create! quantity: calories, unit: u[:kcal],value: (2100 + rng.rand(-300..300)).round, created_at: t demo.readouts.create! quantity: cal_in, unit: u[:kcal],value: (1900 + rng.rand(-400..400)).round, created_at: t if i % 4 == 0 demo.readouts.create! quantity: caffeine, unit: u[:mg], value: (200 + rng.rand(-80..80)).round, created_at: t end end # height is stable — record once demo.readouts.create! quantity: height, unit: u[:cm], value: 178.0, created_at: base_time puts " Created #{demo.units.count} units, #{demo.quantities.count} quantities, #{demo.readouts.count} readouts." end