0
0
Fork 0

Use normalizes to prepare Webhook#events value (#27605)

This commit is contained in:
Matt Jankowski 2023-11-13 17:47:44 -05:00 committed by GitHub
parent bac9e0b55d
commit b7807f3d84
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 8 deletions

View file

@ -33,11 +33,11 @@ class Webhook < ApplicationRecord
validates :secret, presence: true, length: { minimum: 12 }
validates :events, presence: true
validate :validate_events
validate :events_validation_error, if: :invalid_events?
validate :validate_permissions
validate :validate_template
before_validation :strip_events
normalizes :events, with: ->(events) { events.filter_map { |event| event.strip.presence } }
before_validation :generate_secret
def rotate_secret!
@ -69,8 +69,12 @@ class Webhook < ApplicationRecord
private
def validate_events
errors.add(:events, :invalid) if events.any? { |e| EVENTS.exclude?(e) }
def events_validation_error
errors.add(:events, :invalid)
end
def invalid_events?
events.blank? || events.difference(EVENTS).any?
end
def validate_permissions
@ -88,10 +92,6 @@ class Webhook < ApplicationRecord
end
end
def strip_events
self.events = events.filter_map { |str| str.strip.presence } if events.present?
end
def generate_secret
self.secret = SecureRandom.hex(20) if secret.blank?
end