Class: SoftValidator::LogSubscriber

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/soft_validator/log_subscriber.rb

Overview

Log subscriber for soft validation errors.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attachObject

Helper method to attach the log subscriber.



7
8
9
# File 'lib/soft_validator/log_subscriber.rb', line 7

def attach
  attach_to :soft_validator
end

.detachObject

Helper method to detach the log subscriber.



12
13
14
# File 'lib/soft_validator/log_subscriber.rb', line 12

def detach
  detach_from :soft_validator
end

Instance Method Details

#loggerObject



29
30
31
# File 'lib/soft_validator/log_subscriber.rb', line 29

def logger
  self.class.logger
end

#validation_error(event) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/soft_validator/log_subscriber.rb', line 17

def validation_error(event)
  return unless logger&.warn?

  error = event.payload[:error]
  record = error.base
  ref = record.class.name
  ref = "#{ref}(#{record.id})" if record.respond_to?(:id)
  message = "Soft validation error on #{ref}: #{error.full_message}"

  logger.warn(message)
end