class TournamentParticipant < ApplicationRecord include Brandable belongs_to :tournament belongs_to :group, class_name: "TournamentGroup", optional: true belongs_to :source_team, class_name: "Team", optional: true has_many :home_matches, class_name: "Match", foreign_key: :home_participant_id, dependent: :nullify has_many :away_matches, class_name: "Match", foreign_key: :away_participant_id, dependent: :nullify validates :name, presence: true validates :position, numericality: { only_integer: true, greater_than_or_equal_to: 0 } after_update :sync_related_match_names, if: :saved_change_to_name? def branding_parent source_team || tournament end def effective_logo_url if logo_file.attached? Rails.application.routes.url_helpers.rails_blob_path(logo_file, only_path: true) elsif logo_url.present? logo_url else source_team&.effective_logo_url end end private def sync_related_match_names Match.where(home_participant_id: id).or(Match.where(away_participant_id: id)).find_each(&:save!) end end