Created
September 12, 2012 13:42
-
-
Save agis/3706695 to your computer and use it in GitHub Desktop.
Paperclip image dimension validator for Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# #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