Manuel Bustillo a42f938530
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
Assign a color to every group and expose it via API
2024-11-03 14:41:09 +01:00

22 lines
450 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
private
def set_color
self.color = "##{SecureRandom.hex(3)}"
end
end