# == Schema Information # # Table name: contests # # id :integer not null, primary key # allow_registration :boolean default(FALSE) # duration :string # duration_seconds :integer # lang :string default("en") # name :string # offline_form :boolean default(FALSE) # public :boolean default(FALSE) # ranking_mode :string # slug :string # team :boolean default(FALSE) # created_at :datetime not null # updated_at :datetime not null # user_id :integer not null # # Indexes # # index_contests_on_slug (slug) UNIQUE # index_contests_on_user_id (user_id) # # Foreign Keys # # user_id (user_id => users.id) # class Contest < ApplicationRecord extend FriendlyId belongs_to :user has_many :categories has_many :completions, dependent: :destroy has_many :contestants, dependent: :destroy has_many :puzzles, dependent: :destroy has_many :messages, dependent: :destroy has_many :offlines, dependent: :destroy friendly_id :name, use: :slugged before_save :add_duration_seconds, if: -> { duration.present? } validates :name, presence: true validates :lang, inclusion: { in: Languages::AVAILABLE_LANGUAGES.map { |lang| lang[:id] } } validates :ranking_mode, inclusion: { in: Ranking::AVAILABLE_RANKING_MODES.map { |lang| lang[:id] } } validates :duration, presence: true, format: { with: /\A(\d\d:\d\d|\d:\d\d)\z/ } generates_token_for :token def add_duration_seconds arr = self.duration.split(":") if arr.size == 2 self.duration_seconds = arr[0].to_i * 3600 + arr[1].to_i * 60 end end end