Skip to content

Instantly share code, notes, and snippets.

@agis
agis / dimensions_validator.rb
Created September 12, 2012 13:42
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