class Paperclip::Validators::AttachmentContentTypeValidator

Public Class Methods

new(options) click to toggle source
Calls superclass method
# 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

Public Instance Methods

check_validity!() click to toggle source
# File lib/paperclip/validators/attachment_content_type_validator.rb, line 23
def check_validity!
  unless options.has_key?(:content_type)
    raise ArgumentError, "You must pass in :content_type to the validator"
  end
end
validate_each(record, attribute, value) click to toggle source
# File lib/paperclip/validators/attachment_content_type_validator.rb, line 9
def validate_each(record, attribute, value)
  attribute = "#{attribute}_content_type".to_sym
  value = record.send(:read_attribute_for_validation, attribute)
  allowed_types = [options[:content_type]].flatten

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

  if allowed_types.none? { |type| type === value }
    record.errors.add(attribute, :invalid, options.merge(
      :types => allowed_types.join(', ')
    ))
  end
end