Module: ActiveStorageValidations::ErrorHandler

Included in:
AspectRatioValidator, AttachedValidator, ContentTypeValidator, DimensionValidator, LimitValidator, ProcessableImageValidator, SizeValidator
Defined in:
lib/active_storage_validations/error_handler.rb

Instance Method Summary collapse

Instance Method Details

#add_error(record, attribute, error_type, **errors_options) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/active_storage_validations/error_handler.rb', line 11

def add_error(record, attribute, error_type, **errors_options)
  type = errors_options[:custom_message].presence || error_type
  return if record.errors.added?(attribute, type)

  # You can read https://api.rubyonrails.org/classes/ActiveModel/Errors.html#method-i-add
  # to better understand how Rails model errors work
  record.errors.add(attribute, type, **errors_options)
end

#initialize_error_options(options) ⇒ Object



4
5
6
7
8
9
# File 'lib/active_storage_validations/error_handler.rb', line 4

def initialize_error_options(options)
  {
    validator_type: self.class.to_sym,
    custom_message: (options[:message] if options[:message].present?)
  }.compact
end