Skip to content

Instantly share code, notes, and snippets.

@zgohr
Created February 11, 2014 18:45
Show Gist options
  • Save zgohr/8941352 to your computer and use it in GitHub Desktop.
Save zgohr/8941352 to your computer and use it in GitHub Desktop.
class DimensionsValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
# I'm not sure about this:
dimensions = Paperclip::Geometry.from_file(value.queued_for_write[:original].path)
# But this is what you need to know:
width = options[:width]
height = options[:height]
record.errors[attribute] << "Width must be #{width}px" unless dimensions.width == width
record.errors[attribute] << "Height must be #{height}px" unless dimensions.height == height
end
end
end
validates :file, :dimensions => { :width => 300, :height => 300 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment