Test Setup Failed
Push — master ( 5c8348...a7f22b )
by Steven
01:48 queued 10s
created

Choice.load_choice()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 4
rs 10
1
# frozen_string_literal: true
2
3
# == Schema Information
4
#
5
# Table name: choices
6
#
7
#  id                :integer          not null, primary key
8
#  response_scale_id :integer          not null
9
#  value             :string           not null
10
#  score             :integer          default(-1), not null
11
#  description       :string           not null
12
#  created_at        :datetime         not null
13
#  updated_at        :datetime         not null
14
#
15
# Indexes
16
#
17
#  index_by_response_scale_value       (response_scale_id,value) UNIQUE
18
#  index_choices_on_response_scale_id  (response_scale_id)
19
#
20
21
# Table name: choices
22
#
23
#  id              :integer          not null, primary key
24
#  response_scale_id :integer          not null
25
#  value           :string           not null
26
#  score           :integer          default(-1), not null
27
#  description     :string           not null
28
#
29
30
# Model to represent choices in a rating scale
31
class Choice < ApplicationRecord
32
  belongs_to :response_scale, inverse_of: :choices
33
  has_many :responses, inverse_of: :choice
34
35
  validates :response_scale, presence: true
36
  validates :value, presence: true
37
  validates :description, presence: true
38
  validates_uniqueness_of :value, scope: :response_scale 
39
  validates_numericality_of :score, message: 'is not a number'
40
41
  def self.load_choice(response_scale, choice)
42
    Choice.create_with(description: choice['text']) \
43
          .find_or_create_by(response_scale: response_scale, value: choice['value'])
44
  end
45
46
  def to_s
47
    "#{response_scale} #{value} #{description}"  
48
  end
49
end
50