Use normalizes
to prepare Webhook#events
value (#27605)
This commit is contained in:
parent
bac9e0b55d
commit
b7807f3d84
2 changed files with 39 additions and 8 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue