Commit 2aad4222 authored by Sean Arnold's avatar Sean Arnold

Change policy validaiton

- Move validation to rules model
parent aae855a1
...@@ -4,8 +4,6 @@ module IncidentManagement ...@@ -4,8 +4,6 @@ module IncidentManagement
class EscalationPolicy < ApplicationRecord class EscalationPolicy < ApplicationRecord
self.table_name = 'incident_management_escalation_policies' self.table_name = 'incident_management_escalation_policies'
MAX_RULE_COUNT = 10
belongs_to :project belongs_to :project
has_many :rules, class_name: 'EscalationRule', inverse_of: :policy, foreign_key: 'policy_id', index_errors: true has_many :rules, class_name: 'EscalationRule', inverse_of: :policy, foreign_key: 'policy_id', index_errors: true
...@@ -13,16 +11,9 @@ module IncidentManagement ...@@ -13,16 +11,9 @@ module IncidentManagement
validates :name, presence: true, uniqueness: { scope: [:project_id] }, length: { maximum: 72 } validates :name, presence: true, uniqueness: { scope: [:project_id] }, length: { maximum: 72 }
validates :description, length: { maximum: 160 } validates :description, length: { maximum: 160 }
validates :rules, presence: true validates :rules, presence: true
validate :rules_count_not_exceeded
accepts_nested_attributes_for :rules accepts_nested_attributes_for :rules
scope :with_rules, -> { includes(:rules) } scope :with_rules, -> { includes(:rules) }
private
def rules_count_not_exceeded
errors.add(:base, "cannot have more than #{MAX_RULE_COUNT} rules") if rules.size > MAX_RULE_COUNT
end
end end
end end
...@@ -4,6 +4,8 @@ module IncidentManagement ...@@ -4,6 +4,8 @@ module IncidentManagement
class EscalationRule < ApplicationRecord class EscalationRule < ApplicationRecord
self.table_name = 'incident_management_escalation_rules' self.table_name = 'incident_management_escalation_rules'
MAX_RULE_PER_POLICY_COUNT = 10
belongs_to :policy, class_name: 'EscalationPolicy', inverse_of: 'rules', foreign_key: 'policy_id' belongs_to :policy, class_name: 'EscalationPolicy', inverse_of: 'rules', foreign_key: 'policy_id'
belongs_to :oncall_schedule, class_name: 'OncallSchedule', inverse_of: 'rotations', foreign_key: 'oncall_schedule_id' belongs_to :oncall_schedule, class_name: 'OncallSchedule', inverse_of: 'rotations', foreign_key: 'oncall_schedule_id'
...@@ -16,5 +18,16 @@ module IncidentManagement ...@@ -16,5 +18,16 @@ module IncidentManagement
numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 24.hours } numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 24.hours }
validates :policy_id, uniqueness: { scope: [:oncall_schedule_id, :status, :elapsed_time_seconds], message: _('must have a unique schedule, status, and elapsed time') } validates :policy_id, uniqueness: { scope: [:oncall_schedule_id, :status, :elapsed_time_seconds], message: _('must have a unique schedule, status, and elapsed time') }
validate :rules_count_not_exceeded, on: :create, if: :policy
private
def rules_count_not_exceeded
# We need to add to the count if we aren't creating the rules at the same time as the policy.
rules_count = policy.new_record? ? policy.rules.size : policy.rules.size + 1
errors.add(:base, "cannot have more than #{MAX_RULE_PER_POLICY_COUNT} rules") if rules_count > MAX_RULE_PER_POLICY_COUNT
end
end end
end end
...@@ -21,12 +21,5 @@ RSpec.describe IncidentManagement::EscalationPolicy do ...@@ -21,12 +21,5 @@ RSpec.describe IncidentManagement::EscalationPolicy do
it { is_expected.to validate_uniqueness_of(:name).scoped_to(:project_id) } it { is_expected.to validate_uniqueness_of(:name).scoped_to(:project_id) }
it { is_expected.to validate_length_of(:name).is_at_most(72) } it { is_expected.to validate_length_of(:name).is_at_most(72) }
it { is_expected.to validate_length_of(:description).is_at_most(160) } it { is_expected.to validate_length_of(:description).is_at_most(160) }
it 'validates the number of rules' do
policy = build(:incident_management_escalation_policy, rule_count: 11)
expect(policy).not_to be_valid
expect(policy.errors).to contain_exactly("cannot have more than #{described_class::MAX_RULE_COUNT} rules")
end
end end
end end
...@@ -20,5 +20,13 @@ RSpec.describe IncidentManagement::EscalationRule do ...@@ -20,5 +20,13 @@ RSpec.describe IncidentManagement::EscalationRule do
it { is_expected.to validate_presence_of(:elapsed_time_seconds) } it { is_expected.to validate_presence_of(:elapsed_time_seconds) }
it { is_expected.to validate_numericality_of(:elapsed_time_seconds).is_greater_than_or_equal_to(0).is_less_than_or_equal_to(24.hours) } it { is_expected.to validate_numericality_of(:elapsed_time_seconds).is_greater_than_or_equal_to(0).is_less_than_or_equal_to(24.hours) }
it { is_expected.to validate_uniqueness_of(:policy_id).scoped_to([:oncall_schedule_id, :status, :elapsed_time_seconds] ).with_message('must have a unique schedule, status, and elapsed time') } it { is_expected.to validate_uniqueness_of(:policy_id).scoped_to([:oncall_schedule_id, :status, :elapsed_time_seconds] ).with_message('must have a unique schedule, status, and elapsed time') }
it 'validates the number of rules' do
policy = create(:incident_management_escalation_policy, rule_count: 10)
rule = build(:incident_management_escalation_rule, policy: policy)
expect(rule).not_to be_valid
expect(rule.errors).to contain_exactly("cannot have more than #{described_class::MAX_RULE_PER_POLICY_COUNT} rules")
end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment