Assign a color to every group and expose it via API #94
| @ -6,10 +6,15 @@ class TablesArrangementsController < ApplicationController | |||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   def show |   def show | ||||||
|     Seat.joins(:guest).where(tables_arrangement_id: params[:id]) |     Seat.joins(guest: :group) | ||||||
|         .pluck(:table_number, Arel.sql("guests.first_name || ' ' || guests.last_name "), 'guests.id') |         .where(tables_arrangement_id: params[:id]) | ||||||
|  |         .pluck( | ||||||
|  |           :table_number, | ||||||
|  |           Arel.sql("guests.first_name || ' ' || guests.last_name "), 'guests.id', | ||||||
|  |           'groups.color' | ||||||
|  |         ) | ||||||
|         .group_by(&:first) |         .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: } } |         .map { |number, guests| { number:, guests: } } | ||||||
|         .then { |result| render json: result } |         .then { |result| render json: result } | ||||||
|   end |   end | ||||||
|  | |||||||
| @ -7,7 +7,15 @@ class Group < ApplicationRecord | |||||||
|   has_many :children, class_name: 'Group', foreign_key: 'parent_id' |   has_many :children, class_name: 'Group', foreign_key: 'parent_id' | ||||||
|   belongs_to :parent, class_name: 'Group', optional: true |   belongs_to :parent, class_name: 'Group', optional: true | ||||||
| 
 | 
 | ||||||
|  |   before_create :set_color | ||||||
|  | 
 | ||||||
|   scope :roots, -> { where(parent_id: nil) } |   scope :roots, -> { where(parent_id: nil) } | ||||||
| 
 | 
 | ||||||
|   has_many :guests |   has_many :guests | ||||||
|  | 
 | ||||||
|  |   private | ||||||
|  | 
 | ||||||
|  |   def set_color | ||||||
|  |     self.color = "##{SecureRandom.hex(3)}" | ||||||
|  |   end | ||||||
| end | end | ||||||
|  | |||||||
							
								
								
									
										5
									
								
								db/migrate/20241103133122_add_color_to_group.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								db/migrate/20241103133122_add_color_to_group.rb
									
									
									
									
									
										Normal 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
									
									
									
								
							
							
						
						
									
										5
									
								
								db/schema.rb
									
									
									
										generated
									
									
									
								
							| @ -1,5 +1,3 @@ | |||||||
| # Copyright (C) 2024 Manuel Bustillo |  | ||||||
| 
 |  | ||||||
| # This file is auto-generated from the current state of the database. Instead | # 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 | # of editing this file, please use the migrations feature of Active Record to | ||||||
| # incrementally modify your database, and then regenerate this schema definition. | # 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. | # 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 |   # These are extensions that must be enabled in order to support this database | ||||||
|   enable_extension "plpgsql" |   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 "created_at", null: false | ||||||
|     t.datetime "updated_at", null: false |     t.datetime "updated_at", null: false | ||||||
|     t.uuid "parent_id" |     t.uuid "parent_id" | ||||||
|  |     t.string "color" | ||||||
|     t.index ["name"], name: "index_groups_on_name", unique: true |     t.index ["name"], name: "index_groups_on_name", unique: true | ||||||
|     t.index ["parent_id"], name: "index_groups_on_parent_id" |     t.index ["parent_id"], name: "index_groups_on_parent_id" | ||||||
|   end |   end | ||||||
|  | |||||||
| @ -3,5 +3,9 @@ | |||||||
| require 'rails_helper' | require 'rails_helper' | ||||||
| 
 | 
 | ||||||
| RSpec.describe Group, type: :model do | 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 | end | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user