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

- (AttachmentContentTypeValidator) initialize(options)



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

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

Class Method Details

+ (Object) helper_method_name



9
10
11
# File 'lib/paperclip/validators/attachment_content_type_validator.rb', line 9

def self.helper_method_name
  :validates_attachment_content_type
end

Instance Method Details

- (Object) allowed_types



46
47
48
# File 'lib/paperclip/validators/attachment_content_type_validator.rb', line 46

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

- (Object) check_validity!



54
55
56
57
58
# File 'lib/paperclip/validators/attachment_content_type_validator.rb', line 54

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

- (Object) forbidden_types



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

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

- (Object) mark_invalid(record, attribute, types)



42
43
44
# File 'lib/paperclip/validators/attachment_content_type_validator.rb', line 42

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

- (Object) validate_blacklist(record, attribute, value)



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

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

- (Object) validate_each(record, attribute, value)



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/paperclip/validators/attachment_content_type_validator.rb', line 13

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
    record.errors[attribute].each do |error|
      record.errors.add base_attribute, error
    end
  end
end

- (Object) validate_whitelist(record, attribute, value)



30
31
32
33
34
# File 'lib/paperclip/validators/attachment_content_type_validator.rb', line 30

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