Class: Paperclip::Validators::AttachmentFileNameValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/paperclip/validators/attachment_file_name_validator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ AttachmentFileNameValidator

Returns a new instance of AttachmentFileNameValidator.



6
7
8
9
10
11
12
# File 'lib/paperclip/validators/attachment_file_name_validator.rb', line 6

def initialize(options)
  options[:allow_nil] = true unless options.key?(:allow_nil)
  unless options.key?(:add_validation_errors_to)
    options[:add_validation_errors_to] = Paperclip.options[:add_validation_errors_to]
  end
  super
end

Class Method Details

.helper_method_nameObject



14
15
16
# File 'lib/paperclip/validators/attachment_file_name_validator.rb', line 14

def self.helper_method_name
  :validates_attachment_file_name
end

Instance Method Details

#allowedObject



51
52
53
# File 'lib/paperclip/validators/attachment_file_name_validator.rb', line 51

def allowed
  [options[:matches]].flatten.compact
end

#check_validity!Object



59
60
61
62
63
# File 'lib/paperclip/validators/attachment_file_name_validator.rb', line 59

def check_validity!
  unless options.key?(:matches) || options.key?(:not)
    raise ArgumentError, "You must pass in either :matches or :not to the validator"
  end
end

#forbiddenObject



55
56
57
# File 'lib/paperclip/validators/attachment_file_name_validator.rb', line 55

def forbidden
  [options[:not]].flatten.compact
end

#mark_invalid(record, attribute, patterns) ⇒ Object



47
48
49
# File 'lib/paperclip/validators/attachment_file_name_validator.rb', line 47

def mark_invalid(record, attribute, patterns)
  record.errors.add attribute, :invalid, **options.merge(names: patterns.join(", "))
end

#validate_blacklist(record, attribute, value) ⇒ Object



43
44
45
# File 'lib/paperclip/validators/attachment_file_name_validator.rb', line 43

def validate_blacklist(record, attribute, value)
  mark_invalid record, attribute, forbidden if forbidden.present? && forbidden.any? { |type| type === value }
end

#validate_each(record, attribute, value) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/paperclip/validators/attachment_file_name_validator.rb', line 18

def validate_each(record, attribute, value)
  base_attribute = attribute.to_sym
  attribute = "#{attribute}_file_name".to_sym
  value = record.send :read_attribute_for_validation, attribute

  return if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])

  validate_whitelist(record, attribute, value)
  validate_blacklist(record, attribute, value)

  if record.errors.include?(attribute) &&
      [:both, :base].include?(options[:add_validation_errors_to])

    record.errors[attribute].each do |error|
      record.errors.add(base_attribute, error)
    end

    record.errors.delete(attribute) if options[:add_validation_errors_to] == :base
  end
end

#validate_whitelist(record, attribute, value) ⇒ Object



39
40
41
# File 'lib/paperclip/validators/attachment_file_name_validator.rb', line 39

def validate_whitelist(record, attribute, value)
  mark_invalid record, attribute, allowed if allowed.present? && allowed.none? { |type| type === value }
end