Class: Paperclip::Validators::AttachmentDimensionsValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/paperclip/validators/attachment_dimensions_validator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ AttachmentDimensionsValidator

Returns a new instance of AttachmentDimensionsValidator.



4
5
6
# File 'lib/paperclip/validators/attachment_dimensions_validator.rb', line 4

def initialize(options)
  super
end

Class Method Details

.helper_method_nameObject



8
9
10
# File 'lib/paperclip/validators/attachment_dimensions_validator.rb', line 8

def self.helper_method_name
  :validates_attachment_dimensions
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/paperclip/validators/attachment_dimensions_validator.rb', line 12

def validate_each(record, attribute, value)
  dimensions = Paperclip::Geometry.from_file(value.queued_for_write[:original].path)

  [:height, :width].each do |dimension|
    if options[dimension] && dimensions.send(dimension) != options[dimension].to_f
      record.errors.add(attribute.to_sym, :dimension, dimension_type: dimension.to_s, dimension: options[dimension], actual_dimension: dimensions.send(dimension).to_i)
    end
  end
end