Files
MatchLiveTv/backend/app/models/team.rb
T
eminuxandCursor 1d97271555 Aggiunge i18n IT/EN/FR/DE/ES, pagine pubbliche squadre e UX archivio.
Il selettore lingua funziona sul web e sulle app native; su Android la preferenza è persistita e applicata al riavvio. Incluse anche eliminazione replay a scope e campi pagina pubblica squadra.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-23 19:30:44 +02:00

118 lines
2.7 KiB
Ruby

class Team < ApplicationRecord
include Brandable
belongs_to :club
has_many :user_teams, dependent: :destroy
has_many :users, through: :user_teams
has_many :matches, dependent: :destroy
has_many :recordings, dependent: :destroy
has_many :team_invitations, dependent: :destroy
has_many :roster_members, class_name: "TeamRosterMember", dependent: :destroy
has_one_attached :photo_file
validates :name, presence: true
validates :sport_key, presence: true
validates :slug, presence: true, uniqueness: true,
format: { with: /\A[a-z0-9]+(?:-[a-z0-9]+)*\z/, message: "solo lettere minuscole, numeri e trattini" }
validate :sport_key_known
validate :photo_file_type, if: -> { photo_file.attached? }
before_validation :assign_slug, on: :create
before_validation :normalize_slug, if: -> { slug_changed? && slug.present? }
before_validation :normalize_sport_key
def subscription
club.subscription
end
def entitlements
Teams::Entitlements.new(self)
end
# Canale YouTube OAuth è unico per tutta la società (Premium Full).
def youtube_credential
club&.youtube_credential
end
def owner
club&.owner
end
def team_photo_url
if photo_file.attached?
Rails.application.routes.url_helpers.rails_blob_path(photo_file, only_path: true)
end
end
def roster_by_category
TeamRosterMember::DISPLAY_ORDER.index_with do |cat|
roster_members.by_category(cat).to_a
end
end
def roster_public?
roster_public
end
def public_page_path
Rails.application.routes.url_helpers.public_team_page_path(slug)
end
def sport_label
Sports::Catalog.find_optional(sport_key)&.dig(:label) || sport_key.to_s.humanize
end
def effective_board_type
Sports::Catalog.board_for(sport_key)
end
def sport
sport_key
end
def sport=(value)
self.sport_key = Sports::Catalog.normalize_key(value)
end
private
def normalize_sport_key
self.sport_key = Sports::Catalog.normalize_key(sport_key) if sport_key.present?
end
def sport_key_known
return if sport_key.blank?
return if Sports::Catalog.find_optional(sport_key)
errors.add(:sport_key, "non valido")
end
def photo_file_type
return if photo_file.content_type.in?(%w[image/png image/jpeg image/webp])
errors.add(:photo_file, "deve essere PNG, JPEG o WebP")
end
def branding_parent
club
end
def assign_slug
self.slug = Teams::GenerateSlug.call(self) if slug.blank?
end
def normalize_slug
self.slug = slug.to_s.parameterize
end
def default_primary_color
club&.primary_color.presence || super
end
def default_secondary_color
club&.secondary_color.presence || super
end
end