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