Sha256: d6fa30ec22b6942cf2ce7c093066e249aaa9b504de6ae7951ceeb5db74a76739

Contents?: true

Size: 760 Bytes

Versions: 13

Compression:

Stored size: 760 Bytes

Contents

# frozen_string_literal: true
require "securerandom"

# Associates a Participant with an action on a date and provides an obfuscated
# token representation.
class ParticipantToken < ActiveRecord::Base
  TYPES = %w( phq9 wai ).freeze
  TOKEN_LENGTH = 10

  belongs_to :participant

  validates :participant, :token, :token_type, presence: true
  validates :token, uniqueness: { scope: :participant_id }
  validates :token_type, presence: { in: TYPES }

  before_validation :make_token

  def to_s
    token
  end

  def others_on_this_day
    self.class.where(participant_id: participant_id,
                     release_date: release_date)
        .where.not(id: id)
  end

  private

  def make_token
    self.token = SecureRandom.hex(TOKEN_LENGTH)
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
think_feel_do_engine-3.22.9 app/models/participant_token.rb
think_feel_do_engine-3.22.8 app/models/participant_token.rb
think_feel_do_engine-3.22.7 app/models/participant_token.rb
think_feel_do_engine-3.22.6 app/models/participant_token.rb
think_feel_do_engine-3.22.5 app/models/participant_token.rb
think_feel_do_engine-3.22.4 app/models/participant_token.rb
think_feel_do_engine-3.22.2 app/models/participant_token.rb
think_feel_do_engine-3.22.1 app/models/participant_token.rb
think_feel_do_engine-3.22.0 app/models/participant_token.rb
think_feel_do_engine-3.21.2 app/models/participant_token.rb
think_feel_do_engine-3.21.1 app/models/participant_token.rb
think_feel_do_engine-3.21.0 app/models/participant_token.rb
think_feel_do_engine-3.20.1 app/models/participant_token.rb