diff --git a/app/controllers/tables_arrangements_controller.rb b/app/controllers/tables_arrangements_controller.rb index 80935fd..689949c 100644 --- a/app/controllers/tables_arrangements_controller.rb +++ b/app/controllers/tables_arrangements_controller.rb @@ -6,10 +6,16 @@ class TablesArrangementsController < ApplicationController end def show - Seat.joins(:guest).where(tables_arrangement_id: params[:id]) - .pluck(:table_number, Arel.sql("guests.first_name || ' ' || guests.last_name "), 'guests.id') + Seat.joins(guest: :group) + .where(tables_arrangement_id: params[:id]) + .order('guests.group_id') + .pluck( + :table_number, + Arel.sql("guests.first_name || ' ' || guests.last_name "), 'guests.id', + 'groups.color' + ) .group_by(&:first) - .transform_values { |table| table.map { |(_, name, id)| { id:, name: } } } + .transform_values { |table| table.map { |(_, name, id, color)| { id:, name:, color: } } } .map { |number, guests| { number:, guests: } } .then { |result| render json: result } end diff --git a/app/models/group.rb b/app/models/group.rb index 316295e..417e2c6 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -7,7 +7,15 @@ class Group < ApplicationRecord has_many :children, class_name: 'Group', foreign_key: 'parent_id' belongs_to :parent, class_name: 'Group', optional: true + before_create :set_color + scope :roots, -> { where(parent_id: nil) } has_many :guests + + private + + def set_color + self.color = "##{SecureRandom.hex(3)}" + end end diff --git a/db/migrate/20241103133122_add_color_to_group.rb b/db/migrate/20241103133122_add_color_to_group.rb new file mode 100644 index 0000000..bca1c30 --- /dev/null +++ b/db/migrate/20241103133122_add_color_to_group.rb @@ -0,0 +1,7 @@ +# Copyright (C) 2024 Manuel Bustillo + +class AddColorToGroup < ActiveRecord::Migration[7.2] + def change + add_column :groups, :color, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 5a7912b..b2643a7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -12,7 +12,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2024_11_03_093955) do +ActiveRecord::Schema[7.2].define(version: 2024_11_03_133122) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -35,6 +35,7 @@ ActiveRecord::Schema[7.2].define(version: 2024_11_03_093955) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.uuid "parent_id" + t.string "color" t.index ["name"], name: "index_groups_on_name", unique: true t.index ["parent_id"], name: "index_groups_on_parent_id" end diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb index c31cd69..10102f4 100644 --- a/spec/models/group_spec.rb +++ b/spec/models/group_spec.rb @@ -3,5 +3,9 @@ require 'rails_helper' RSpec.describe Group, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + describe 'callbacks' do + it 'should set color before create' do + expect(create(:group).color).to be_present + end + end end