<commit_before># == Schema Information
#
# Table name: contests
#
#  id                     :integer          not null, primary key
#  venue_id               :integer
#  age_limit              :integer
#  pick_up                :boolean
#  listener_ticket_limit  :integer
#  staff_ticket_limit     :integer
#  notes                  :text
#  created_at             :datetime
#  updated_at             :datetime
#  listener_plus_one      :boolean          default(FALSE)
#  staff_plus_one         :boolean          default(FALSE)
#  send_time              :datetime
#  sent                   :boolean          default(FALSE)
#  staff_count            :integer
#  listener_count         :integer
#  alternate_recipient_id :integer
#

class Contest < ActiveRecord::Base
  after_initialize :set_default_values
  before_save :set_send_time

  belongs_to :venue
  belongs_to :alternate_recipient, class_name: "Venue"
  has_many :staff_tickets, dependent: :destroy
  has_many :listener_tickets, dependent: :destroy
  has_many :users, through: :staff_tickets

  accepts_nested_attributes_for :staff_tickets, :listener_tickets, allow_destroy: true

  default_scope -> { order('date DESC') }
  scope :upcoming, -> { where("date >= :start_date", start_date: Time.zone.now.beginning_of_day) }
  scope :past, -> { where("send_time < :start_date", start_date: Time.zone.now) }
  scope :sendable, -> (time) { where("send_time = :send_time", send_time: time) }
  scope :unsent, -> { where(sent: false) }
  scope :announceable, -> {
    unsent.where(
      "send_time >= :start_date",
      start_date: Time.zone.now
    )
  }
  scope :without_user, -> (user) {
    where("id NOT IN (?)", user.contests) unless user.contests.blank?
  }

  validates :venue, presence: true
  validates :age_limit, presence: true
  validates :listener_ticket_limit, numericality: { greater_than_or_equal_to: 0 }
  validates :staff_ticket_limit, numericality: { greater_than_or_equal_to: 0 }
  validate :date_string_is_date

  def announceable?
    !self.sent and (self.send_time >= Time.zone.now.beginning_of_day)
  end

  def recipient
    return self.alternate_recipient || self.venue
  end

  def date_string
    @date_string || date.try(:strftime, "%-m/%-d/%y %-l:%M %p")
  end

  def date_string=(value)
    @date_string = value
    self.date = parse_date
  end

  def self.send_contests(hour = Time.zone.now.hour)
    time = Time.zone.now.beginning_of_day + hour.hours

    self.sendable(time).each do |contest|
      ContestMailer.ticket_email(contest).deliver
      contest.sent = true
      contest.save
    end
  end

  private
    def set_send_time
      self.send_time = self.date.beginning_of_day - self.venue.send_day_offset.days + self.venue.send_hour.hours
    end

    def set_default_values
      self.listener_ticket_limit ||= 0
      self.staff_ticket_limit ||= 0
      self.staff_count ||= 0
      self.listener_count ||= 0
    end
<commit_msg>contest.date should now refer to events.start_time<commit_after># == Schema Information
#
# Table name: contests
#
#  id                     :integer          not null, primary key
#  venue_id               :integer
#  age_limit              :integer
#  pick_up                :boolean
#  listener_ticket_limit  :integer
#  staff_ticket_limit     :integer
#  notes                  :text
#  created_at             :datetime
#  updated_at             :datetime
#  listener_plus_one      :boolean          default(FALSE)
#  staff_plus_one         :boolean          default(FALSE)
#  send_time              :datetime
#  sent                   :boolean          default(FALSE)
#  staff_count            :integer
#  listener_count         :integer
#  alternate_recipient_id :integer
#

class Contest < ActiveRecord::Base
  after_initialize :set_default_values
  before_save :set_send_time

  belongs_to :venue
  belongs_to :alternate_recipient, class_name: "Venue"
  has_many :staff_tickets, dependent: :destroy
  has_many :listener_tickets, dependent: :destroy
  has_many :users, through: :staff_tickets
  has_one :event, as: :eventable, dependent: :destroy

  accepts_nested_attributes_for :staff_tickets, :listener_tickets, allow_destroy: true
  accepts_nested_attributes_for :event

  default_scope -> { includes(:event).order('events.start_time DESC') }
  scope :upcoming, -> { where("event.start_time >= :start_time", start_time: Time.zone.now.beginning_of_day) }
  scope :past, -> { where("send_time < :start_time", start_time: Time.zone.now) }
  scope :sendable, -> (time) { where("send_time = :send_time", send_time: time) }
  scope :unsent, -> { where(sent: false) }
  scope :announceable, -> {
    unsent.where(
      "send_time >= :start_time",
      start_time: Time.zone.now
    )
  }
  scope :up_to, -> (time = 2.weeks) {
    where(
      "send_time <= :start_time",
      start_time: Time.zone.now.beginning_of_day + time
    )
  }
  scope :without_user, -> (user) {
    where("id NOT IN (?)", user.contests) unless user.contests.blank?
  }

  validates :venue, presence: true
  validates :age_limit, presence: true
  validates :listener_ticket_limit, numericality: { greater_than_or_equal_to: 0 }
  validates :staff_ticket_limit, numericality: { greater_than_or_equal_to: 0 }
  validate :date_string_is_date

  def announceable?
    !self.sent and (self.send_time >= Time.zone.now.beginning_of_day)
  end

  def recipient
    return self.alternate_recipient || self.venue
  end

  def date_string
    @date_string || self.event.start_time.try(:strftime, "%-m/%-d/%y %-l:%M %p")
  end

  def date_string=(value)
    @date_string = value
    self.event.start_time = parse_date
  end

  def self.send_contests(hour = Time.zone.now.hour)
    time = Time.zone.now.beginning_of_day + hour.hours

    self.sendable(time).each do |contest|
      ContestMailer.ticket_email(contest).deliver
      contest.sent = true
      contest.save
    end
  end

  private
    def set_send_time
      self.send_time = self.event.start_time.beginning_of_day - self.venue.send_day_offset.days + self.venue.send_hour.hours
    end

    def set_default_values
      self.listener_ticket_limit ||= 0
      self.staff_ticket_limit ||= 0
      self.staff_count ||= 0
      self.listener_count ||= 0
    end

{'fraction_non_alphanumeric': 0.10337620578778135, 'fraction_numerical': 0.002090032154340836, 'mean_word_length': 2.954863318499682, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 24, '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': '1265920', 'n_tokens_mistral': 2093, 'n_tokens_neox': 2027, 'n_words': 421}