# == Schema Information # # Table name: contestants # # id :integer not null, primary key # display_time :string # email :string # name :string # projected_time :string # qrcode :string # time_seconds :integer # created_at :datetime not null # updated_at :datetime not null # contest_id :integer not null # # Indexes # # index_contestants_on_contest_id (contest_id) # # Foreign Keys # # contest_id (contest_id => contests.id) # class Contestant < ApplicationRecord belongs_to :contest has_many :completions, dependent: :destroy has_one :offline, dependent: :destroy has_and_belongs_to_many :categories before_validation :initialize_time_seconds_if_empty before_commit :generate_qrcode, on: :create validates :name, presence: true validates :time_seconds, presence: true generates_token_for :token def form_name if email.present? "#{name} - #{email}" else name end end def public_generate_qrcode self.generate_qrcode end private def initialize_time_seconds_if_empty if !self.time_seconds self.time_seconds = 0 end end def generate_qrcode host = Rails.application.config.action_controller.default_url_options[:host] qrcode = RQRCode::QRCode.new("https://#{host}/public/p/#{self.generate_token_for(:token)}") self.qrcode = qrcode.as_svg( color: "000", shape_rendering: "crispEdges", module_size: 3, standalone: true, use_path: true, viewbox: true ) end end