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

Arm.set_default_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 a study arm
4
class Arm < ApplicationRecord
5
  belongs_to :schedule, inverse_of: :arms 
6
  has_many :study_events, dependent: :destroy
7
  default_value_for :number, 1
8
  delegate :study, to: :schedule, allow_nil: true
9
 
10
  validates :name, presence: true
11
  validates_uniqueness_of :name, scope: %i[schedule number] 
12
  validates_uniqueness_of :number, scope: %i[schedule name]
13
  validates :number, presence: true, allow_blank: false
14
  validates :number, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: 10_000 }
15
  validates :schedule, presence: true
16
17
  before_validation :add_name_if_missing
18
19
  def to_s
20
    "#{study} #{schedule} #{name} #{number}"
21
  end
22
23
  private
24
25
  def add_name_if_missing
26
    self.name ||= 'arm ' + number.to_s
27
  end
28
end
29
30
# == Schema Information
31
#
32
# Table name: arms
33
#
34
#  id          :integer          not null, primary key
35
#  name        :string           not null
36
#  number      :integer          not null
37
#  schedule_id :integer          not null
38
#  created_at  :datetime         not null
39
#  updated_at  :datetime         not null
40
#
41
# Indexes
42
#
43
#  index_arms_on_name         (name)
44
#  index_arms_on_number       (number)
45
#  index_arms_on_schedule_id  (schedule_id)
46
#  index_by_schedule_name     (schedule_id,id,name) UNIQUE
47
#  index_by_schedule_number   (schedule_id,id,number) UNIQUE
48
#
49