diff --git a/app/controllers/guests_controller.rb b/app/controllers/guests_controller.rb index de6127d..18dc949 100644 --- a/app/controllers/guests_controller.rb +++ b/app/controllers/guests_controller.rb @@ -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 diff --git a/app/controllers/tables_arrangements_controller.rb b/app/controllers/tables_arrangements_controller.rb index 689949c..87bcc4c 100644 --- a/app/controllers/tables_arrangements_controller.rb +++ b/app/controllers/tables_arrangements_controller.rb @@ -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) diff --git a/app/models/guest.rb b/app/models/guest.rb index 774eee3..bfa1265 100644 --- a/app/models/guest.rb +++ b/app/models/guest.rb @@ -12,8 +12,4 @@ class Guest < ApplicationRecord } scope :potential, -> { where.not(status: %i[declined considered]) } - - def full_name - "#{first_name} #{last_name}" - end end diff --git a/app/serializers/serializable_guest.rb b/app/serializers/serializable_guest.rb index d711d03..e411307 100644 --- a/app/serializers/serializable_guest.rb +++ b/app/serializers/serializable_guest.rb @@ -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 diff --git a/app/services/tables/distribution.rb b/app/services/tables/distribution.rb index 52621c3..9236007 100644 --- a/app/services/tables/distribution.rb +++ b/app/services/tables/distribution.rb @@ -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 diff --git a/app/use_cases/guests/update_use_case.rb b/app/use_cases/guests/update_use_case.rb index d31a982..3d82c52 100644 --- a/app/use_cases/guests/update_use_case.rb +++ b/app/use_cases/guests/update_use_case.rb @@ -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 }) diff --git a/config/routes.rb b/config/routes.rb index df17dac..dd8654a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/20241111063741_merge_guest_names.rb b/db/migrate/20241111063741_merge_guest_names.rb new file mode 100644 index 0000000..21cdfa6 --- /dev/null +++ b/db/migrate/20241111063741_merge_guest_names.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index b2643a7..7107478 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/db/seeds.rb b/db/seeds.rb index bd9417a..42c175e 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -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 diff --git a/spec/factories/guest.rb b/spec/factories/guest.rb index 2637fb4..4c9b58a 100644 --- a/spec/factories/guest.rb +++ b/spec/factories/guest.rb @@ -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 diff --git a/spec/models/guest_spec.rb b/spec/models/guest_spec.rb index 6861079..bd560af 100644 --- a/spec/models/guest_spec.rb +++ b/spec/models/guest_spec.rb @@ -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