Test Setup Failed
Push — master ( 5e2d1c...4fe2ba )
by Steven
01:31
created

Assessment.set_order_number()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
# frozen_string_literal: true
2
3
# Model to represent the user taking an instrument/questionnaire within a survey/campaign
4
class Assessment < ApplicationRecord
5
  belongs_to :visit, inverse_of: :assessments
6
  has_one :survey, through: :visit
7
  has_one :user, through: :visit
8
  has_many :assessment_instruments, inverse_of: :assessment, dependent: :destroy 
9
  has_many :instruments, through: :assessment_instruments
10
  has_many :responses, inverse_of: :assessment, dependent: :destroy
11
  has_many :scores, inverse_of: :assessment, dependent: :destroy
12
 
13
  default_value_for :order_number, 1
14
  validates :visit, presence: true
15
  validates :order_number, presence: true, allow_blank: false
16
  validates :order_number, numericality: { only_integer: true, 
17
                                           greater_than_or_equal_to: 1, 
18
                                           less_than_or_equal_to: 10_000 }
19
  validates_uniqueness_of :order_number, on: :create, message: 'must be unique', scope: :visit
20
 
21
  before_destroy :destroy_scores
22
 
23
  def to_s
24
    visit.to_s
25
  end
26
27
  private
28
29
  def destroy_scores
30
    scores.destroy_all
31
  end
32
end
33
34
# == Schema Information
35
#
36
# Table name: assessments
37
#
38
#  id           :integer          not null, primary key
39
#  visit_id     :integer          not null
40
#  order_number :integer          default(1), not null
41
#  content      :jsonb            not null
42
#  created_at   :datetime         not null
43
#  updated_at   :datetime         not null
44
#
45
# Indexes
46
#
47
#  assessment_content             (content)
48
#  index_assessments_on_visit_id  (visit_id)
49
#  index_by_visit_order_number    (visit_id,order_number) UNIQUE
50
#
51