Skip to content

Commit 69a8a7e

Browse files
committed
created validation for when urgency and custom both true
1 parent 69ab778 commit 69a8a7e

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

app/models/event_procedure.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
class EventProcedure < ApplicationRecord
44
acts_as_paranoid
5-
65
has_enumeration_for :room_type, with: EventProcedures::RoomTypes, create_helpers: true
76
has_enumeration_for :payment, with: EventProcedures::Payments, create_helpers: true
87

@@ -31,4 +30,6 @@ class EventProcedure < ApplicationRecord
3130
validates :room_type, presence: true, if: -> { health_insurance? }
3231
validates :urgency, inclusion: [true, false], if: -> { health_insurance? }
3332
validates :payment, presence: true
33+
34+
validates_with CustomAndUrgencyValidator
3435
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
class CustomAndUrgencyValidator < ActiveModel::Validator
4+
def validate(record)
5+
return unless record.procedure&.custom && record.urgency
6+
7+
record.errors.add(:base, "EventProcedure cannot have both custom and urgency as true")
8+
end
9+
end

spec/models/event_procedure_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,16 @@
5050
end
5151

5252
describe "validations" do
53+
let(:event) { build(:event_procedure, urgency: true, procedure_attributes: { custom: true }) }
54+
5355
it { is_expected.to validate_presence_of(:date) }
5456
it { is_expected.to validate_presence_of(:patient_service_number) }
5557
it { is_expected.to validate_presence_of(:room_type) }
5658
it { is_expected.to validate_presence_of(:payment) }
59+
60+
it "is invalid when custom and urgency are both true" do
61+
expect(event).not_to be_valid
62+
end
5763
end
5864

5965
describe ".enumerations" do

0 commit comments

Comments
 (0)