|
1
|
|
|
# frozen_string_literal: true |
|
2
|
|
|
|
|
3
|
|
|
# Represents a psychometric instrument |
|
4
|
|
|
# Similar to a survey or questionnaire |
|
5
|
|
|
class Instrument < ApplicationRecord |
|
6
|
|
|
audited |
|
7
|
|
|
include PgSearch |
|
8
|
|
|
include Discard::Model |
|
9
|
|
|
multisearchable against: %i[json_content csv_content] |
|
10
|
|
|
pg_search_scope :search_json, against: :json_content |
|
11
|
|
|
pg_search_scope :search_csv, against: :csv_content |
|
12
|
|
|
|
|
13
|
|
|
default_value_for :version_number, '1.0' |
|
14
|
|
|
default_value_for :instrument_type, 'json' |
|
15
|
|
|
|
|
16
|
|
|
has_many :assessment_instruments, inverse_of: :instrument, dependent: :destroy |
|
17
|
|
|
has_many :study_event_instruments, inverse_of: :instrument, dependent: :destroy |
|
18
|
|
|
has_many :assessments, inverse_of: :instrument, through: :assessment_instruments |
|
19
|
|
|
has_many :study_events, inverse_of: :instrument, through: :study_event_instruments |
|
20
|
|
|
has_many :items, inverse_of: :instrument, dependent: :destroy |
|
21
|
|
|
|
|
22
|
|
|
jsonb_accessor :json_content, |
|
23
|
|
|
title: [:string, default: 'Untitled'], |
|
24
|
|
|
pages: [:jsonb, array: true, default: []] |
|
25
|
|
|
|
|
26
|
|
|
validates :name, presence: true |
|
27
|
|
|
validates_uniqueness_of :name |
|
28
|
|
|
validates_length_of :name, \ |
|
29
|
|
|
within: 2..50, \ |
|
30
|
|
|
too_long: 'pick a shorter name', \ |
|
31
|
|
|
too_short: 'pick a longer name' |
|
32
|
|
|
validates :version_number, presence: true |
|
33
|
|
|
validates_uniqueness_of :version_number, scope: :name |
|
34
|
|
|
|
|
35
|
|
|
validates :instrument_type, presence: true |
|
36
|
|
|
validates :instructions, presence: true, allow_blank: true |
|
37
|
|
|
validates :json_content, presence: true |
|
38
|
|
|
validates :csv_content, presence: true, allow_blank: true |
|
39
|
|
|
|
|
40
|
|
|
def self.list_tests(limit: 4) |
|
41
|
|
|
Instrument.order(:name).limit(limit).join(' ') |
|
42
|
|
|
end |
|
43
|
|
|
|
|
44
|
|
|
def to_s |
|
45
|
|
|
name |
|
46
|
|
|
end |
|
47
|
|
|
|
|
48
|
|
|
def first_question_and_instructions |
|
49
|
|
|
%(#{instructions} #{items.first.title}) |
|
50
|
|
|
end |
|
51
|
|
|
end |
|
52
|
|
|
|
|
53
|
|
|
# == Schema Information |
|
54
|
|
|
# |
|
55
|
|
|
# Table name: instruments |
|
56
|
|
|
# |
|
57
|
|
|
# id :integer not null, primary key |
|
58
|
|
|
# name :string not null |
|
59
|
|
|
# version_number :string default("1.0"), not null |
|
60
|
|
|
# instrument_type :string default("json"), not null |
|
61
|
|
|
# json_content :jsonb not null |
|
62
|
|
|
# csv_content :text default(""), not null |
|
63
|
|
|
# instructions :text default(""), not null |
|
64
|
|
|
# created_at :datetime not null |
|
65
|
|
|
# updated_at :datetime not null |
|
66
|
|
|
# tags :string default([]), is an Array |
|
67
|
|
|
# |
|
68
|
|
|
# Indexes |
|
69
|
|
|
# |
|
70
|
|
|
# index_instruments_on_csv_content (csv_content) |
|
71
|
|
|
# index_instruments_on_name (name) |
|
72
|
|
|
# instrument_json_content (json_content) |
|
73
|
|
|
# instrument_tags (tags) |
|
74
|
|
|
# |
|
75
|
|
|
|