hensei-api/app/models/gw_individual_score.rb
Justin Edmund f2a058b6b2 add GW events and scoring system
- create gw_events, crew_gw_participations, gw_crew_scores, gw_individual_scores
- add models, blueprints, controllers for GW tracking
- add model specs and gw_events controller specs

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-03 23:34:54 -08:00

34 lines
1 KiB
Ruby

# frozen_string_literal: true
class GwIndividualScore < ApplicationRecord
belongs_to :crew_gw_participation
belongs_to :crew_membership, optional: true
belongs_to :recorded_by, class_name: 'User'
# Use same round enum as GwCrewScore
enum :round, GwCrewScore::ROUNDS
validates :round, presence: true
validates :score, presence: true, numericality: { greater_than_or_equal_to: 0 }
validates :crew_membership_id, uniqueness: {
scope: %i[crew_gw_participation_id round],
message: 'already has a score for this round'
}, if: -> { crew_membership_id.present? }
validate :membership_belongs_to_crew
delegate :crew, :gw_event, to: :crew_gw_participation
scope :for_round, ->(round) { where(round: round) }
scope :for_membership, ->(membership) { where(crew_membership: membership) }
private
def membership_belongs_to_crew
return unless crew_membership.present?
unless crew_membership.crew_id == crew_gw_participation.crew_id
errors.add(:crew_membership, 'must belong to the participating crew')
end
end
end