2024-10-27 21:42:45 +00:00
|
|
|
# Copyright (C) 2024 Manuel Bustillo
|
|
|
|
|
2024-11-13 08:12:46 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: groups
|
|
|
|
#
|
|
|
|
# id :uuid not null, primary key
|
|
|
|
# color :string
|
|
|
|
# icon :string
|
|
|
|
# name :string not null
|
|
|
|
# order :integer default(1), not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# parent_id :uuid
|
2024-11-30 20:04:17 +01:00
|
|
|
# wedding_id :uuid not null
|
2024-11-13 08:12:46 +00:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2024-11-30 20:04:17 +01:00
|
|
|
# index_groups_on_name (name) UNIQUE
|
|
|
|
# index_groups_on_parent_id (parent_id)
|
|
|
|
# index_groups_on_wedding_id (wedding_id)
|
2024-11-13 08:12:46 +00:00
|
|
|
#
|
|
|
|
# Foreign Keys
|
|
|
|
#
|
|
|
|
# fk_rails_... (parent_id => groups.id)
|
2024-11-30 20:04:17 +01:00
|
|
|
# fk_rails_... (wedding_id => weddings.id)
|
2024-11-13 08:12:46 +00:00
|
|
|
#
|
2024-08-11 16:29:10 +02:00
|
|
|
class Group < ApplicationRecord
|
2024-11-30 20:04:17 +01:00
|
|
|
acts_as_tenant :wedding
|
|
|
|
|
2024-08-11 18:25:12 +02:00
|
|
|
validates :name, uniqueness: true
|
|
|
|
validates :name, :order, presence: true
|
2024-08-11 17:26:43 +02:00
|
|
|
|
2024-08-11 18:25:12 +02:00
|
|
|
has_many :children, class_name: 'Group', foreign_key: 'parent_id'
|
|
|
|
belongs_to :parent, class_name: 'Group', optional: true
|
|
|
|
|
2024-11-03 14:41:09 +01:00
|
|
|
before_create :set_color
|
|
|
|
|
2024-11-01 11:55:32 +01:00
|
|
|
scope :roots, -> { where(parent_id: nil) }
|
|
|
|
|
2024-12-08 11:30:38 +01:00
|
|
|
has_many :guests, dependent: :nullify
|
2024-11-03 14:41:09 +01:00
|
|
|
|
2024-11-10 09:49:08 +01:00
|
|
|
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
|
|
|
|
|
2024-11-03 14:41:09 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
def set_color
|
2024-11-13 08:57:20 +01:00
|
|
|
return if color.present?
|
|
|
|
|
2024-11-04 23:39:44 +01:00
|
|
|
new_color = "##{SecureRandom.hex(3)}".paint
|
|
|
|
new_color = new_color.lighten(30) if new_color.dark?
|
|
|
|
self.color = new_color
|
2024-11-03 14:41:09 +01:00
|
|
|
end
|
2024-08-11 16:29:10 +02:00
|
|
|
end
|