<commit_before># == Schema Information
#
# Table name: competitions
#
#  id               :integer          not null, primary key
#  round_id         :integer
#  host_id          :integer
#  begins           :date
#  ends             :date
#  created_at       :datetime         not null
#  updated_at       :datetime         not null
#  certificate_date :date
#  season           :integer
#  signup_deadline  :datetime
#

class Competition < ActiveRecord::Base
  attr_accessible :season, :round_id, :host_id, :begins, :ends, :signup_deadline,
                  :certificate_date, :category_ids

  # Find competitions with matching season and round
  def self.current
    joins(:round, :host)
    .where(season: JUMU_SEASON, rounds: { level: JUMU_ROUND })
    .order("hosts.name")
  end

  # Find current competitions whose signup is open
  def self.current_and_open
    now = Time.now
    current.where("competitions.signup_deadline > ?", now)
  end

  # Finds competitions of same year, but round precedent to current
  scope :preceding, joins(:round, :host)
      .where("rounds.level = ? AND competitions.season = ?", JUMU_ROUND - 1, JUMU_SEASON)
      .order("hosts.name")

  belongs_to :round
  belongs_to :host
  has_many :performances, dependent: :destroy
  has_many :appearances, through: :performances, readonly: true
  has_many :participants, through: :appearances, uniq: true, readonly: true
  has_and_belongs_to_many :categories

  validates :season,          presence: true,
                              numericality: { only_integer: true, greater_than: 0 }
  validates :round_id,        presence: true
  validates :host_id,         presence: true
  validates :begins,          presence: true
  validates :ends,            presence: true
  validates :signup_deadline, presence: true

  # Validate order of associated dates, if available (presence is validated separately)
  validate :require_beginning_before_end
  validate :require_signup_deadline_before_beginning

  # Virtual name that identifies the competition
  def name
    "#{self.host.name}, #{self.round.slug} #{self.year}"
  end

  # Name of school hosting the competition
  def host_name
    "#{self.host.name}"
  end

  # Last full day of signup
  def last_signup_date
    self.signup_deadline.to_date - 1.day
  end

  # Day range during which the competition takes place<commit_msg>Rearrange some code without functional changes<commit_after># == Schema Information
#
# Table name: competitions
#
#  id               :integer          not null, primary key
#  round_id         :integer
#  host_id          :integer
#  begins           :date
#  ends             :date
#  created_at       :datetime         not null
#  updated_at       :datetime         not null
#  certificate_date :date
#  season           :integer
#  signup_deadline  :datetime
#

class Competition < ActiveRecord::Base
  attr_accessible :season, :round_id, :host_id, :begins, :ends, :signup_deadline,
                  :certificate_date, :category_ids

  belongs_to :round
  belongs_to :host
  has_many :performances, dependent: :destroy
  has_many :appearances, through: :performances, readonly: true
  has_many :participants, through: :appearances, uniq: true, readonly: true
  has_and_belongs_to_many :categories

  validates :season,          presence: true,
                              numericality: { only_integer: true, greater_than: 0 }
  validates :round_id,        presence: true
  validates :host_id,         presence: true
  validates :begins,          presence: true
  validates :ends,            presence: true
  validates :signup_deadline, presence: true

  # Validate order of associated dates, if available (presence is validated separately)
  validate :require_beginning_before_end
  validate :require_signup_deadline_before_beginning

  # Find competitions with matching season and round
  def self.current
    joins(:round, :host)
    .where(season: JUMU_SEASON, rounds: { level: JUMU_ROUND })
    .order("hosts.name")
  end

  # Find current competitions whose signup is open
  def self.current_and_open
    now = Time.now
    current.where("competitions.signup_deadline > ?", now)
  end

  # Finds competitions of same year, but round precedent to current
  scope :preceding, joins(:round, :host)
      .where("rounds.level = ? AND competitions.season = ?", JUMU_ROUND - 1, JUMU_SEASON)
      .order("hosts.name")

  # Virtual name that identifies the competition
  def name
    "#{self.host.name}, #{self.round.slug} #{self.year}"
  end

  # Name of school hosting the competition
  def host_name
    "#{self.host.name}"
  end

  # Last full day of signup
  def last_signup_date
    self.signup_deadline.to_date - 1.day
  end

  # Day range during which the competition takes place
{'fraction_non_alphanumeric': 0.08331580054702294, 'fraction_numerical': 0.0012623606143488324, 'mean_word_length': 2.9815745393634843, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4706483', 'n_tokens_mistral': 1468, 'n_tokens_neox': 1431, 'n_words': 418}