Merge first and last name and expose guest update endpoint
All checks were successful
Check usage of free licenses / build-static-assets (pull_request) Successful in 1m42s
Add copyright notice / copyright_notice (pull_request) Successful in 3m5s
Run unit tests / unit_tests (pull_request) Successful in 5m4s

This commit is contained in:
Manuel Bustillo 2024-11-11 07:43:42 +01:00
parent d2974edcc1
commit bd5c4f5482
12 changed files with 34 additions and 41 deletions

View File

@ -6,23 +6,14 @@ class GuestsController < ApplicationController
def index
@guests = Guest.all.includes(:group)
.joins(:group)
.order('groups.name' => :asc, first_name: :asc, last_name: :asc)
.order('groups.name' => :asc, name: :asc)
render jsonapi: @guests
end
def import
csv = CSV.parse(params[:file].read, headers: true)
ActiveRecord::Base.transaction do
csv.each do |row|
guest = Guest.create!(first_name: row['name'])
guest.affinity_group_list.add(row['affinity_group'])
guest.save!
end
end
redirect_to guests_url
def update
Guests::UpdateUseCase.new(guest_ids: [params[:id]], params: params.require(:guest).permit(:name)).call
render json: {}, status: :ok
end
def bulk_update

View File

@ -11,7 +11,8 @@ class TablesArrangementsController < ApplicationController
.order('guests.group_id')
.pluck(
:table_number,
Arel.sql("guests.first_name || ' ' || guests.last_name "), 'guests.id',
'guests.name',
'guests.id',
'groups.color'
)
.group_by(&:first)

View File

@ -12,8 +12,4 @@ class Guest < ApplicationRecord
}
scope :potential, -> { where.not(status: %i[declined considered]) }
def full_name
"#{first_name} #{last_name}"
end
end

View File

@ -6,7 +6,7 @@ class SerializableGuest < JSONAPI::Serializable::Resource
attributes :id, :group_id, :status
attribute :name do
"#{@object.first_name} #{@object.last_name}"
@object.name
end
attribute :group_name do

View File

@ -33,7 +33,7 @@ module Tables
def pretty_print
@tables.map.with_index do |table, i|
"Table #{i + 1} (#{table.count} ppl): (#{local_discomfort(table)}) #{table.map(&:full_name).join(', ')}"
"Table #{i + 1} (#{table.count} ppl): (#{local_discomfort(table)}) #{table.map(&:name).join(', ')}"
end.join("\n")
end

View File

@ -12,6 +12,8 @@ module Guests
Guest.where(id: guest_ids).update!(params)
# TODO: Not all status transitions may require a table re-arrangement
return unless params.key?(:status)
TablesArrangement.delete_all
ActiveJob.perform_all_later(50.times.map { TableSimulatorJob.new })

View File

@ -3,13 +3,12 @@
Rails.application.routes.draw do
resources :groups, only: :index
resources :guests do
post :import, on: :collection
post :bulk_update, on: :collection
end
resources :expenses, only: :index do
resources :expenses, only: %i[index update] do
get :summary, on: :collection
end
resources :tables_arrangements, only: [:index, :show]
resources :tables_arrangements, only: %i[index show]
get 'up' => 'rails/health#show', as: :rails_health_check
end

View File

@ -0,0 +1,17 @@
class MergeGuestNames < ActiveRecord::Migration[8.0]
def change
add_column :guests, :name, :string
reversible do |dir|
dir.up do
execute <<~SQL
UPDATE guests
SET name = CONCAT(first_name, ' ', last_name)
SQL
end
end
remove_column :guests, :first_name, :string
remove_column :guests, :last_name, :string
end
end

9
db/schema.rb generated
View File

@ -1,5 +1,3 @@
# Copyright (C) 2024 Manuel Bustillo
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
@ -12,9 +10,9 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.2].define(version: 2024_11_03_133122) do
ActiveRecord::Schema[8.0].define(version: 2024_11_11_063741) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
enable_extension "pg_catalog.plpgsql"
# Custom types defined in this database.
# Note that some types may not work with other database engines. Be careful if changing database.
@ -41,13 +39,12 @@ ActiveRecord::Schema[7.2].define(version: 2024_11_03_133122) do
end
create_table "guests", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "phone"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.uuid "group_id", null: false
t.integer "status", default: 0
t.string "name"
t.index ["group_id"], name: "index_guests_on_group_id"
end

View File

@ -56,8 +56,7 @@ groups = Group.all
NUMBER_OF_GUESTS.times do
Guest.create!(
first_name: Faker::Name.first_name,
last_name: Faker::Name.last_name,
name: Faker::Name.name,
phone: Faker::PhoneNumber.cell_phone,
group: groups.sample,
status: Guest.statuses.keys.sample

View File

@ -4,8 +4,7 @@ FactoryBot.define do
factory :guest do
association :group
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
name { Faker::Name.name }
phone { Faker::PhoneNumber.cell_phone }
end
end

View File

@ -28,12 +28,4 @@ RSpec.describe Guest, type: :model do
end
end
end
describe '#full_name' do
it 'returns the full name of the guest' do
guest = create(:guest, first_name: 'John', last_name: 'Doe')
expect(guest.full_name).to eq('John Doe')
end
end
end