Le partite ereditano lo sport_key della squadra alla creazione invece del default pallavolo; l'organico mostra ruoli per board (basket, volley, timed) e il wizard Android usa lo sport del team per overlay e icona. Co-authored-by: Cursor <cursoragent@cursor.com>
82 lines
2.2 KiB
Ruby
82 lines
2.2 KiB
Ruby
class TeamRosterMember < ApplicationRecord
|
|
CATEGORIES = %w[staff coach manager player].freeze
|
|
# Ordine di visualizzazione in pagina organico
|
|
DISPLAY_ORDER = %w[coach manager player staff].freeze
|
|
CATEGORY_LABELS = {
|
|
"staff" => "Staff",
|
|
"coach" => "Allenatori",
|
|
"manager" => "Dirigenti",
|
|
"player" => "Giocatori"
|
|
}.freeze
|
|
|
|
VOLLEYBALL_PLAYER_ROLES = Sports::PlayerRoles::BY_BOARD["volley"].freeze
|
|
|
|
belongs_to :team
|
|
|
|
has_one_attached :photo_file
|
|
|
|
validates :full_name, presence: true
|
|
validates :category, inclusion: { in: CATEGORIES }
|
|
validates :jersey_number, numericality: { only_integer: true, greater_than: 0, less_than: 100 }, allow_nil: true
|
|
validate :photo_file_type, if: -> { photo_file.attached? }
|
|
validate :player_role_label_valid, if: -> { category == "player" && role_label.present? }
|
|
|
|
before_validation :assign_position, on: :create
|
|
|
|
scope :ordered, -> { order(:position, :created_at) }
|
|
scope :by_category, ->(cat) { where(category: cat).ordered }
|
|
|
|
def category_label
|
|
CATEGORY_LABELS[category] || category
|
|
end
|
|
|
|
def initials
|
|
parts = full_name.to_s.split(/\s+/).reject(&:blank?)
|
|
return "?" if parts.empty?
|
|
|
|
parts.first(2).map { |p| p[0] }.join.upcase
|
|
end
|
|
|
|
def photo_url
|
|
return unless photo_file.attached?
|
|
|
|
Rails.application.routes.url_helpers.rails_blob_path(photo_file, only_path: true)
|
|
end
|
|
|
|
def display_role
|
|
role_label.presence || default_role_label
|
|
end
|
|
|
|
private
|
|
|
|
def assign_position
|
|
return if position.positive? || team.blank?
|
|
|
|
max_pos = team.roster_members.where(category: category).maximum(:position) || -1
|
|
self.position = max_pos + 1
|
|
end
|
|
|
|
def default_role_label
|
|
case category
|
|
when "coach" then "Allenatore"
|
|
when "manager" then "Dirigente"
|
|
when "staff" then "Staff"
|
|
when "player" then jersey_number.present? ? "Giocatore ##{jersey_number}" : "Giocatore"
|
|
else category
|
|
end
|
|
end
|
|
|
|
def player_role_label_valid
|
|
allowed = Sports::PlayerRoles.for_team(team)
|
|
return if role_label.in?(allowed)
|
|
|
|
errors.add(:role_label, "non è un ruolo valido per questo sport")
|
|
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
|
|
end
|