Test Setup Failed
Push — master ( 480572...f90cae )
by Steven
01:51
created

StudyEvent.to_s()   A

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
# Model to represent study events
4
class StudyEvent < ApplicationRecord
5
  belongs_to :arm, inverse_of: :study_events 
6
  delegate :schedule, to: :arm, allow_nil: true
7
  delegate :study, to: :schedule, allow_nil: true
8
  has_many :study_event_instruments
9
  has_many :instruments, through: :study_event_instruments
10
  
11
  default_value_for :order, 1
12
  default_value_for :event_date, Date.today
13
  validates :arm, presence: true
14
  validates :name, presence: true
15
  validates_uniqueness_of :name
16
  validates_length_of :name, \
17
                      within: 2..50, \
18
                      too_long: 'pick a shorter name', \
19
                      too_short: 'pick a longer name'
20
  validates_uniqueness_of :order, scope: %i[arm]
21
  validates :order, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: 10_000 }
22
  validates :order, presence: true
23
  validates_datetime :event_date
24
25
  before_create :unique_order_number
26
27
  def to_s
28
    "#{arm} #{name}"
29
  end
30
31
  private
32
33
  def unique_order_number
34
    last_order = StudyEvent.order(:arm_id, order: :desc)&.first&.order || 1
35
    self.order = last_order + 1
36
  end
37
end
38
39
# == Schema Information
40
#
41
# Table name: study_events
42
#
43
#  id         :integer          not null, primary key
44
#  name       :string           not null
45
#  order      :integer          default(1), not null
46
#  arm_id     :integer          not null
47
#  event_date :datetime         not null
48
#  created_at :datetime         not null
49
#  updated_at :datetime         not null
50
#
51
# Indexes
52
#
53
#  index_by_arm_name             (arm_id,name) UNIQUE
54
#  index_study_events_on_arm_id  (arm_id)
55
#  index_study_events_on_name    (name)
56
#  index_study_events_on_order   (order)
57
#
58