Class: Paperclip::Validators::AttachmentContentTypeValidator

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ AttachmentContentTypeValidator

Returns a new instance of AttachmentContentTypeValidator.



4
5
6
7
8
9
10
# File 'lib/paperclip/validators/attachment_content_type_validator.rb', line 4

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



12
13
14
# File 'lib/paperclip/validators/attachment_content_type_validator.rb', line 12

def self.helper_method_name
  :validates_attachment_content_type
end

Instance Method Details

#allowed_typesObject



53
54
55
# File 'lib/paperclip/validators/attachment_content_type_validator.rb', line 53

def allowed_types
  [options[:content_type]].flatten.compact
end

#check_validity!Object



61
62
63
64
65
# File 'lib/paperclip/validators/attachment_content_type_validator.rb', line 61

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

#forbidden_typesObject



57
58
59
# File 'lib/paperclip/validators/attachment_content_type_validator.rb', line 57

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

#mark_invalid(record, attribute, types) ⇒ Object



49
50
51
# File 'lib/paperclip/validators/attachment_content_type_validator.rb', line 49

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

#validate_blacklist(record, attribute, value) ⇒ Object



43
44
45
46
47
# File 'lib/paperclip/validators/attachment_content_type_validator.rb', line 43

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

#validate_each(record, attribute, value) ⇒ Object



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

def validate_each(record, attribute, value)
  base_attribute = attribute.to_sym
  attribute = "#{attribute}_content_type".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



37
38
39
40
41
# File 'lib/paperclip/validators/attachment_content_type_validator.rb', line 37

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