Class: Attachs::Extensions::ActiveRecord::Validations::AttachmentContentTypeValidator

Inherits:
AttachmentValidator
  • Object
show all
Defined in:
lib/attachs/extensions/active_record/validations/attachment_content_type_validator.rb

Instance Method Summary collapse

Methods inherited from AttachmentValidator

#validate_each

Instance Method Details

#validate_attachment(record, attachment) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/attachs/extensions/active_record/validations/attachment_content_type_validator.rb', line 9

def validate_attachment(record, attachment)
  unless attachment.blank?
    if options.has_key?(:with)
      if options[:with] !~ attachment.content_type
        attachment.errors.add :value, :not_allowed
      end
    elsif options.has_key?(:in) || options.has_key?(:within)
      list = (options[:in] || options[:within])
      if list.exclude?(attachment.content_type)
        attachment.errors.add :value, :can_only_be, list: list.to_sentence
      end
    end
  end
end