Class: MiniPaperclip::Validators::ContentTypeValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/mini_paperclip/validators/content_type_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/mini_paperclip/validators/content_type_validator.rb', line 6

def validate_each(record, attribute, value)
  allows = Array(options[:content_type])
  attachment_content_type_name = "#{attribute}_content_type"
  attachment_content_type = record.read_attribute_for_validation(attachment_content_type_name)
  if attachment_content_type && !allows.include?(attachment_content_type)
    record.errors.add(attribute, :invalid)
    record.errors.add(attachment_content_type_name, :invalid)
  end
end