Module: ActiveStorageValidations::Errorable

Extended by:
ActiveSupport::Concern
Included in:
AspectRatioValidator, AttachedValidator, BaseSizeValidator, ContentTypeValidator, DimensionValidator, LimitValidator, ProcessableImageValidator
Defined in:
lib/active_storage_validations/concerns/errorable.rb

Instance Method Summary collapse

Instance Method Details

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



18
19
20
21
22
23
24
25
# File 'lib/active_storage_validations/concerns/errorable.rb', line 18

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, file = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/active_storage_validations/concerns/errorable.rb', line 5

def initialize_error_options(options, file = nil)
  not_explicitly_written_options = %i(with in)
  curated_options = options.except(*not_explicitly_written_options)

  active_storage_validations_options = {
    validator_type: self.class.to_sym,
    custom_message: (options[:message] if options[:message].present?),
    filename: (get_filename(file) unless self.class.to_sym == :total_size)
  }.compact

  curated_options.merge(active_storage_validations_options)
end