From 83e36df14eca044af79e8b714749cfe1cf534881 Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sun, 8 Dec 2024 13:10:49 +0100 Subject: [PATCH] Allow the creation of guests associated to no group --- app/controllers/guests_controller.rb | 2 +- app/models/guest.rb | 2 +- spec/models/guest_spec.rb | 2 +- spec/requests/guests_spec.rb | 3 ++- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/controllers/guests_controller.rb b/app/controllers/guests_controller.rb index a302541..54975b6 100644 --- a/app/controllers/guests_controller.rb +++ b/app/controllers/guests_controller.rb @@ -5,7 +5,7 @@ require 'csv' class GuestsController < ApplicationController def index render json: Guest.all.includes(:group) - .joins(:group) + .left_joins(:group) .order('groups.name' => :asc, name: :asc) .as_json(only: %i[id name status], include: { group: { only: %i[id name] } }) end diff --git a/app/models/guest.rb b/app/models/guest.rb index a418a27..a0a2992 100644 --- a/app/models/guest.rb +++ b/app/models/guest.rb @@ -25,7 +25,7 @@ # class Guest < ApplicationRecord acts_as_tenant :wedding - belongs_to :group + belongs_to :group, optional: true enum :status, { considered: 0, diff --git a/spec/models/guest_spec.rb b/spec/models/guest_spec.rb index b484464..6786271 100644 --- a/spec/models/guest_spec.rb +++ b/spec/models/guest_spec.rb @@ -16,7 +16,7 @@ RSpec.describe Guest, type: :model do end end - it { should belong_to(:group) } + it { should belong_to(:group).optional } describe 'scopes' do describe '.potential' do diff --git a/spec/requests/guests_spec.rb b/spec/requests/guests_spec.rb index 9b542fd..bc73fbe 100644 --- a/spec/requests/guests_spec.rb +++ b/spec/requests/guests_spec.rb @@ -41,7 +41,7 @@ RSpec.describe 'guests', type: :request do properties: { guest: { type: :object, - required: %i[name group_id status], + required: %i[name status], properties: { name: { type: :string }, group_id: { type: :string, format: :uuid }, @@ -70,6 +70,7 @@ RSpec.describe 'guests', type: :request do properties: { guest: { type: :object, + required: %i[name status], properties: { name: { type: :string }, group_id: { type: :string, format: :uuid }, -- 2.47.1