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