Валидиране размер на снимка в Paperclip

Използвате Paperclip и искате да валидирате размера на качваните снимки:

class Post < ActiveRecord::Base   
  ...
  validates_attachment_presence :photo, message: "Choose photo"
  validates_attachment_content_type :photo, content_type: ['image/jpeg', 'image/png'], message: "Only JPEG, PNG Images"
  validates_attachment_size :photo, less_than: 500.kilobytes , message: "less than 500 KB"  
  validate :check_image_dimension, on: :create

  # Validate image dimension
  def check_image_dimension
    dimensions = Paperclip::Geometry.from_file(photo.queued_for_write[:original].path)
    width = dimensions.width
    height = dimensions.height

    if (width.to_i < 500 || height.to_i < 500)
      self.errors.add(:photo, "The selected file could not be uploaded. The image is too small, the minimum dimensions are 500x500 pixels.")
    end
  end
end