Test Setup Failed
Push — master ( c4da73...8c2db5 )
by Steven
01:54 queued 21s
created

Participant.create_journal()   A

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
# Model to represent survey participants
4
class Participant < ApplicationRecord
5
  belongs_to :user, optional: true
6
  validates :email, presence: true, 'valid_email_2/email': true
7
  has_one :journal, inverse_of: :participant, dependent: :destroy
8
  has_many :survey_participants, inverse_of: :participant, dependent: :destroy
9
  has_many :surveys, through: :survey_participants
10
  has_many :study_participants, inverse_of: :participant, dependent: :destroy
11
  has_many :studies, through: :study_participants
12
  validates :journal, presence: true
13
14
  after_initialize :create_journal
15
16
  def to_s
17
    "#{identifier} #{email}".strip
18
  end
19
20
  private 
21
  
22
  def create_journal
23
    self.journal ||= Journal.new(participant: self, name: Time.now.to_s) if new_record?
24
  end
25
end
26