Skip to content

Instantly share code, notes, and snippets.

@agis
Created September 12, 2012 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agis/3706695 to your computer and use it in GitHub Desktop.
Save agis/3706695 to your computer and use it in GitHub Desktop.
Paperclip image dimension validator for Rails
# #attribute is a private method so we can't call it directly, that's why we use #send.
class DimensionsValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if record.send("#{attribute}?".to_sym)
dimensions = Paperclip::Geometry.from_file(value.queued_for_write[:original].path)
width = options[:width]
height = options[:height]
record.errors[attribute] << "To πλάτος πρέπει να είναι #{width}px" unless dimensions.width == width
record.errors[attribute] << "Το ύψος πρέπει να είναι #{height}px" unless dimensions.height == height
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment