Manuel Bustillo 452b5b2040
All checks were successful
Check usage of free licenses / build-static-assets (pull_request) Successful in 45s
Add copyright notice / copyright_notice (pull_request) Successful in 1m47s
Run unit tests / unit_tests (pull_request) Successful in 5m4s
Introduce endpoint to retrieve a summary of groups and invite attendance
2024-11-13 08:57:20 +01:00

40 lines
992 B
Ruby

# Copyright (C) 2024 Manuel Bustillo
class Group < ApplicationRecord
validates :name, uniqueness: true
validates :name, :order, presence: true
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
def colorize_children(generation = 1)
derived_colors = generation == 1 ? color.paint.palette.analogous(size: children.count) : color.paint.palette.decreasing_saturation
children.zip(derived_colors) do |child, raw_color|
final_color = raw_color.paint
final_color.brighten(60) if final_color.dark?
child.update!(color: final_color)
child.colorize_children(generation + 1)
end
end
private
def set_color
return if color.present?
new_color = "##{SecureRandom.hex(3)}".paint
new_color = new_color.lighten(30) if new_color.dark?
self.color = new_color
end
end