Merge pull request 'Assign a color to every group and expose it via API' (#94) from group-color into main
Reviewed-on: #94
This commit is contained in:
commit
231ea3fda8
@ -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
|
||||
|
@ -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
|
||||
|
7
db/migrate/20241103133122_add_color_to_group.rb
Normal file
7
db/migrate/20241103133122_add_color_to_group.rb
Normal file
@ -0,0 +1,7 @@
|
||||
# Copyright (C) 2024 Manuel Bustillo
|
||||
|
||||
class AddColorToGroup < ActiveRecord::Migration[7.2]
|
||||
def change
|
||||
add_column :groups, :color, :string
|
||||
end
|
||||
end
|
3
db/schema.rb
generated
3
db/schema.rb
generated
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user