Class: Base::Project::App::Validators::ImageValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/base/project/app/validators/image_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/base/project/app/validators/image_validator.rb', line 4

def validate_each(record, attribute, value)
  return if value.queued_for_write.blank? || value.queued_for_write[:original].blank?

  dimensions = Paperclip::Geometry.from_file(value.queued_for_write[:original].path)
  width = options[:width]
  height = options[:height]

  record.errors.add(attribute, I18n.t('activerecord.errors.image.width',  value: width)) if dimensions.width > width
  record.errors.add(attribute, I18n.t('activerecord.errors.image.height', value: height)) if dimensions.height > height
end