Created
July 20, 2016 14:04
-
-
Save alenteria/ce71b7065e39b240d0e0b198ddf41aad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Sport < ActiveRecord::Base | |
| # == Constants ========================================================== | |
| BASKETBALL_ID = 8 | |
| BASKETBALL_5_PERIOD_ID = 58 | |
| INLINE_HOCKEY = 'Inline Hockey (2P)' | |
| INLINE_HOCKEY_2P = INLINE_HOCKEY | |
| INLINE_HOCKEY_3P = 'Inline Hockey (3P)' | |
| ICE_HOCKEY = 'Ice Hockey' | |
| DARTS = 'darts' | |
| BASKETBALL = 'Basketball (4Q)' | |
| BASKETBALL_5_PERIOD = 'Basketball (5P)' | |
| HOCKEY = 'Inline Hockey' | |
| SETS = "sets" | |
| SUPPORTS_POSTGAME_SCORESHEET = [ | |
| HOCKEY, ICE_HOCKEY, INLINE_HOCKEY | |
| ].freeze | |
| SUPPORTS_POSTGAME_PDF_SCORESHEET = [ | |
| ICE_HOCKEY, INLINE_HOCKEY_2P | |
| ].freeze | |
| # == Attributes ========================================================= | |
| # == Extensions ========================================================= | |
| # == Relationships ====================================================== | |
| with_options dependent: :destroy do |sport| | |
| sport.has_many :positions | |
| sport.has_many :periods, -> { order :display_sequence }, { } # rails/issues/9805 | |
| sport.has_many :game_rules | |
| sport.has_many :divisions, inverse_of: :sport | |
| sport.has_many :metric_assignments, foreign_key: :assignment_id | |
| end | |
| # == Validations ======================================================== | |
| validates :name, | |
| presence: true | |
| # == Callbacks ========================================================== | |
| # == Scopes ============================================================= | |
| scope :has_scoresheets, ->{ where(has_scoresheets: true) } | |
| scope :has_pdf_scoresheets, ->{ where(has_pdf_scoresheets: true) } | |
| # == Class Methods ====================================================== | |
| def self.for_select | |
| order(:name).pluck(:name, :id) | |
| end | |
| def self.scoring_points_term_hash | |
| Sport.pluck(:id, :scoring_points_term).inject({}) do |hash, sport_attrs| | |
| hash[sport_attrs[0]] = sport_attrs[1] | |
| hash | |
| end | |
| end | |
| def self.is_sport_a_hockey?(sport_name) | |
| [ICE_HOCKEY, INLINE_HOCKEY].include?(sport_name) | |
| end | |
| def self.upgraded_sports | |
| ['Inline Hockey', 'Hockey', BASKETBALL] | |
| end | |
| def scoring_for_darts? | |
| scoring_type == DARTS | |
| end | |
| def scoring_for_sets? | |
| scoring_type == SETS | |
| end | |
| def score_sheet_class | |
| if is_sport_for_ice_hockey? | |
| IceHockeyScoreSheet | |
| elsif is_sport_for_inline_hockey? | |
| InlineHockeyScoreSheet | |
| elsif is_sport_for_basketball? | |
| BasketballScoreSheet | |
| elsif is_sport_for_basketball_5_period? | |
| Basketball5PeriodScoreSheet | |
| elsif is_sport_for_3_periods? | |
| HockeyScoreSheet | |
| else | |
| ScoreSheet | |
| end | |
| end | |
| def supports_player_statistics? | |
| supports_game_actions | |
| end | |
| def is_sport_for_3_periods? | |
| name.in?([HOCKEY, 'Hockey']) | |
| end | |
| def is_sport_for_basketball? | |
| name == BASKETBALL | |
| end | |
| def is_sport_for_basketball_5_period? | |
| name == BASKETBALL_5_PERIOD | |
| end | |
| def basketball? | |
| [BASKETBALL, BASKETBALL_5_PERIOD].include?(name) | |
| end | |
| def is_sport_for_inline_hockey? | |
| name == INLINE_HOCKEY | |
| end | |
| def is_sport_for_ice_hockey? | |
| name == ICE_HOCKEY | |
| end | |
| def has_pregame_scoresheet? | |
| has_scoresheets || has_pdf_scoresheets | |
| end | |
| def has_postgame_scoresheet? | |
| name.in? SUPPORTS_POSTGAME_SCORESHEET | |
| end | |
| def has_postgame_pdf_scoresheet? | |
| name.in? SUPPORTS_POSTGAME_PDF_SCORESHEET | |
| end | |
| # == Instance methods =================================================== | |
| def logo_url(style = 'small') | |
| # available styles :small, :large, :transparent | |
| "sports/#{style}/#{self.logo_name}_icn.png" | |
| end | |
| def limited_periods | |
| scoring_for_sets? ? periods.limit(number_of_sets) : periods | |
| end | |
| STATS_LABELS = { | |
| [:points, :goals] => :scoring_points_term, | |
| [:scoring_initial, :p, :g] => :scoring_points_initial, | |
| [:points_for, :goals_against, :gf, :pf] => :points_for_label, | |
| [:points_against, :goals_against, :ga, :pa] => :points_against_label, | |
| [:points_difference, :goals_difference, :gd, :pd] => :points_difference_label | |
| }.map do |references, method| | |
| [references].flatten.map{|ref| [ref, method] } | |
| end.flatten_to_hash.with_indifferent_access.freeze | |
| def team_stat_label(term) | |
| STATS_LABELS[term] && send(STATS_LABELS[term]) | |
| end | |
| alias_method :player_stat_label, :team_stat_label | |
| def points_for_label | |
| return "FR" if scoring_for_darts? | |
| self.scoring_points_term =~ /points/i ? "PF" : "GF" | |
| end | |
| def points_against_label | |
| return "AG" if scoring_for_darts? | |
| self.scoring_points_term =~ /points/i ? "PA" : "GA" | |
| end | |
| def points_difference_label | |
| self.scoring_points_term =~ /points/i ? "PD" : "GD" | |
| end | |
| def scoring_points_initial | |
| scoring_points_term[0].upcase | |
| end | |
| def period_name | |
| self['period_name'].present? ? self['period_name'] : 'period' | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment