Item.to_s()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
1
# frozen_string_literal: true
2
3
# == Schema Information
4
#
5
# Table name: items
6
#
7
#  id                :integer          not null, primary key
8
#  instrument_id     :integer          not null
9
#  name              :string           not null
10
#  item_type         :string           not null
11
#  title             :string           not null
12
#  response_scale_id :integer
13
#  is_required       :boolean          default(TRUE), not null
14
#  created_at        :datetime         not null
15
#  updated_at        :datetime         not null
16
#
17
# Indexes
18
#
19
#  index_by_item_name                (name) UNIQUE
20
#  index_items_on_instrument_id      (instrument_id)
21
#  index_items_on_response_scale_id  (response_scale_id)
22
#
23
24
# Table name: items
25
26
#  id              :integer          not null, primary key
27
#  instrument_id   :integer          not null
28
#  name            :string           not null
29
#  item_type       :string           not null
30
#  title           :string           not null
31
#  response_scale_id :integer
32
#  is_required     :boolean          default(TRUE), not null
33
#
34
35
# Represents a psychometric item
36
# Similar to a question with metadata
37
class Item < ApplicationRecord
38
  include Discard::Model
39
  belongs_to :instrument, inverse_of: :items, touch: true
40
  belongs_to :response_scale, inverse_of: :items, optional: true
41
  has_many :choices, through: :response_scale  
42
  validates :name, presence: true
43
  validates :instrument, presence: true
44
  validates_uniqueness_of :name
45
  validates_length_of :name, \
46
                      within: 2..20, \
47
                      too_long: 'pick a shorter name', \
48
                      too_short: 'pick a longer name'
49
  validates :item_type, presence: true
50
  validates_length_of :item_type, \
51
                      within: 3..20, \
52
                      too_long: 'pick a shorter item type', \
53
                      too_short: 'pick a longer item type'
54
  
55
  def find_choice_by_value(value)
56
    choices.find_by(response_scale: response_scale, value: value)
57
  end
58
59
  def to_s
60
    name
61
  end
62
end
63