Class: Attachs::ActiveRecord::Validators::AttachmentContentTypeValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/attachs/active_record/validators/attachment_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
15
16
17
18
# File 'lib/attachs/active_record/validators/attachment_content_type_validator.rb', line 6

def validate_each(record, attribute, value)
  if value.exists?
    if options.has_key? :with
      if options[:with] !~ value.content_type
        record.errors.add attribute, :attachment_content_type_with
      end
    elsif options.has_key? :in
      if options[:in].exclude? value.content_type
        record.errors.add attribute, :attachment_content_type_in, types: options[:in].to_sentence
      end
    end
  end
end