From 522bcb0032816bdcd57d5a12d236d2334670ca49 Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Tue, 28 Jan 2025 08:59:15 +0100 Subject: [PATCH] Generate invitations within the same group --- app/controllers/guests_controller.rb | 2 +- db/seeds.rb | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/app/controllers/guests_controller.rb b/app/controllers/guests_controller.rb index 1d872fa..78cd2a6 100644 --- a/app/controllers/guests_controller.rb +++ b/app/controllers/guests_controller.rb @@ -9,7 +9,7 @@ class GuestsController < ApplicationController render json: Guest.includes(:group) .left_joins(:group) .order('groups.name' => :asc, name: :asc) - .as_json(only: %i[id name status], include: { group: { only: %i[id name] } }) + .as_json(only: %i[id name status invitation_id], include: { group: { only: %i[id name] } }) end def create diff --git a/db/seeds.rb b/db/seeds.rb index 79afb17..a693f64 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -60,18 +60,29 @@ ActsAsTenant.with_tenant(wedding) do groups = Group.all - invitations = ([{}] * (NUMBER_OF_GUESTS * 0.8)).then { Invitation.insert_all!(it) }.rows.flatten - NUMBER_OF_GUESTS.times.map do |i| { name: Faker::Name.name, phone: Faker::PhoneNumber.cell_phone, group_id: groups.sample.id, status: Guest.statuses.keys.sample, - invitation_id: invitations[i] } end.then { Guest.insert_all!(it) } + Group.includes(:guests).each do |group| + guests = group.guests.potential.to_a + + while guests.any? + invitation = Invitation.create! + + guests.shift(rand(1..3)).each do |guest| + guest.update!(invitation:) + end + end + end + + # TODO: Clean up invitations with no guests + ActiveJob.perform_all_later(3.times.map { TableSimulatorJob.new(wedding.id) }) "red".dup.paint.palette.triad(as: :hex).zip(Group.roots).each { |(color, group)| group.update!(color: color.paint.desaturate(40)) }