User.create_participant()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 3
rs 10
1
# frozen_string_literal: true
2
3
# Represents an application user/respondent
4
# Users are linked to Alexa users using Alexa session request.user_id
5
class User < ApplicationRecord
6
  audited except: :password
7
  include Discard::Model
8
  has_many :visits, inverse_of: :user, dependent: :destroy
9
  has_many :assessments, through: :visits
10
  has_one :participant, dependent: :destroy
11
  has_many :survey_participants, through: :participant
12
  has_many :surveys, through: :survey_participants
13
  jsonb_accessor :preferences,
14
                 locale: [:string, default: 'en-US']
15
    
16
  validates :username, presence: true
17
  validates_uniqueness_of :username
18
  validates_length_of :username, \
19
                      within: 5..20, \
20
                      too_long: 'pick a shorter name', \
21
                      too_short: 'pick a longer name'
22
    
23
  delegate :journal, to: :participant, allow_nil: true
24
25
  after_initialize :create_participant
26
27
  def self.authenticate(user_id)
28
    user = User.find_or_create_by(username: user_id)
29
    user
30
  end
31
32
  def create_participant 
33
    self.participant ||= Participant.create!(user: self, email: AppConstants::PLACEHOLDER_EMAIL) if new_record?
34
  end
35
36
  def to_s
37
    username
38
  end
39
end
40
41
# == Schema Information
42
#
43
# Table name: users
44
#
45
#  id           :integer          not null, primary key
46
#  username     :string           not null
47
#  firstname    :string
48
#  lastname     :string
49
#  access_token :text             default(""), not null
50
#  preferences  :jsonb            not null
51
#  created_at   :datetime         not null
52
#  updated_at   :datetime         not null
53
#
54
# Indexes
55
#
56
#  preferences  (preferences)
57
#  username     (username) UNIQUE
58
#
59