Assign a color to every group and expose it via API
All checks were successful
Check usage of free licenses / build-static-assets (pull_request) Successful in 55s
Add copyright notice / copyright_notice (pull_request) Successful in 1m28s
Run unit tests / unit_tests (pull_request) Successful in 1m57s

This commit is contained in:
Manuel Bustillo 2024-11-03 14:41:09 +01:00
parent ed0e307e33
commit a42f938530
5 changed files with 28 additions and 7 deletions

View File

@ -6,10 +6,15 @@ 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])
.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

View File

@ -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

View File

@ -0,0 +1,5 @@
class AddColorToGroup < ActiveRecord::Migration[7.2]
def change
add_column :groups, :color, :string
end
end

5
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,7 +10,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 +33,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

View File

@ -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