Skip to content

Commit f14443f

Browse files
authored
Seed data: enable badge creation and linking to units (#307)
1 parent 4b74714 commit f14443f

4 files changed

Lines changed: 39 additions & 11 deletions

File tree

db/seeds/units.rb

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
Rails.logger.debug "Creating units..."
22

3-
def create_unit(name:, serial_prefix:, manufacturer:, operator:, description:)
3+
# Create badge batch and badges for all units
4+
badge_batch = BadgeBatch.create!(note: "Seed data badges", count: 8)
5+
badges = 8.times.map { Badge.create!(badge_batch: badge_batch) }
6+
badge_ids = badges.map(&:id)
7+
8+
def create_unit(name:, serial_prefix:, manufacturer:, operator:, description:, badge_id:)
49
Unit.create!(
510
SeedData.unit_fields.merge(
11+
id: badge_id,
612
user: $test_user,
713
name: name,
814
serial: "#{serial_prefix}-#{SecureRandom.hex(4).upcase}",
@@ -19,63 +25,71 @@ def create_unit(name:, serial_prefix:, manufacturer:, operator:, description:)
1925
serial_prefix: "ACQ-2021",
2026
manufacturer: "Airquee Manufacturing Ltd",
2127
operator: "Stef's Castles",
22-
description: "15ft x 15ft medieval themed bouncy castle with turrets"
28+
description: "15ft x 15ft medieval themed bouncy castle with turrets",
29+
badge_id: badge_ids[0]
2330
)
2431

2532
$castle_large = create_unit(
2633
name: "Giant Party Castle",
2734
serial_prefix: "BCN-2020",
2835
manufacturer: "Bouncy Castle Boys",
2936
operator: "Estephan Events",
30-
description: "30ft x 30ft large bouncy castle suitable for 20+ children"
37+
description: "30ft x 30ft large bouncy castle suitable for 20+ children",
38+
badge_id: badge_ids[1]
3139
)
3240

3341
$castle_slide_combo = create_unit(
3442
name: "Princess Castle with Slide",
3543
serial_prefix: "J4J-2022",
3644
manufacturer: "Jump4Joy Inflatables",
3745
operator: "Stefan's Fun Factory",
38-
description: "Pink princess themed castle with integrated 8ft slide"
46+
description: "Pink princess themed castle with integrated 8ft slide",
47+
badge_id: badge_ids[2]
3948
)
4049

4150
$soft_play_unit = create_unit(
4251
name: "Toddler Soft Play Centre",
4352
serial_prefix: "CIU-2023",
4453
manufacturer: "Custom Inflatables UK",
4554
operator: "Steff's Soft Play",
46-
description: "Fully enclosed soft play area for under 5s"
55+
description: "Fully enclosed soft play area for under 5s",
56+
badge_id: badge_ids[3]
4757
)
4858

4959
$obstacle_course = create_unit(
5060
name: "Assault Course Challenge",
5161
serial_prefix: "IWL-2021",
5262
manufacturer: "Inflatable World Ltd",
5363
operator: "Stephan's Adventure Co",
54-
description: "40ft assault course with obstacles, tunnels and slide finish"
64+
description: "40ft assault course with obstacles, tunnels and slide finish",
65+
badge_id: badge_ids[4]
5566
)
5667

5768
$giant_slide = create_unit(
5869
name: "Mega Slide Experience",
5970
serial_prefix: "ACQ-2019",
6071
manufacturer: "Airquee Manufacturing Ltd",
6172
operator: "Stefan Family Inflatables",
62-
description: "25ft platform height giant inflatable slide"
73+
description: "25ft platform height giant inflatable slide",
74+
badge_id: badge_ids[5]
6375
)
6476

6577
$gladiator_duel = create_unit(
6678
name: "Gladiator Duel Platform",
6779
serial_prefix: "HHE-2022",
6880
manufacturer: "Happy Hop Europe",
6981
operator: "Stefano's Party Hire",
70-
description: "Inflatable gladiator duel platform with pedestals"
82+
description: "Inflatable gladiator duel platform with pedestals",
83+
badge_id: badge_ids[6]
7184
)
7285

7386
$bungee_run = create_unit(
7487
name: "Double Bungee Run",
7588
serial_prefix: "PCM-2023",
7689
manufacturer: "Party Castle Manufacturers",
7790
operator: "Stef's Fun Factory",
78-
description: "Two lane inflatable bungee run competition game"
91+
description: "Two lane inflatable bungee run competition game",
92+
badge_id: badge_ids[7]
7993
)
8094

8195
Rails.logger.debug { "Created #{Unit.count} units." }

spec/factories/units.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@
4040
manufacture_date { 1.year.ago }
4141
is_seed { false }
4242

43+
trait :with_badge do
44+
transient do
45+
badge_batch { nil }
46+
end
47+
48+
after(:build) do |unit, evaluator|
49+
batch = evaluator.badge_batch || FactoryBot.create(:badge_batch, count: 1)
50+
badge = FactoryBot.create(:badge, badge_batch: batch)
51+
unit.id = badge.id
52+
end
53+
end
54+
4355
# Variation with different values
4456
trait :with_different_values do
4557
name { "Different Test Unit" }

spec/seeds/assessment_seed_data_completeness_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
# Columns that are system-managed and don't need to be in seed data
3333
let(:system_columns) do
34-
%w[inspection_id created_at updated_at]
34+
%w[id inspection_id created_at updated_at]
3535
end
3636

3737
# Comment fields are optional

spec/services/seed_data_service_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# typed: false
2+
13
require "rails_helper"
24

35
RSpec.describe SeedDataService do
@@ -11,7 +13,7 @@
1113
end
1214

1315
it "creates complete inspections with all assessments" do
14-
described_class.add_seeds_for_user(user, unit_count: 1, inspection_count: 1)
16+
described_class.add_seeds_for_user(user, unit_count: 1, inspection_count: 2)
1517

1618
inspection = user.inspections.seed_data.complete.first
1719
expect(inspection.user_height_assessment).to be_present

0 commit comments

Comments
 (0)