Ruby on Rails, Paperclip y validaciones de dimensiones
Me he picado una extensión de Paperclip para porveer de validaciones de dimensiones para las imágenes adjuntas aquí la comparto contigo por si te resulta útil.
No he hecho un parche para Paperclip pues eso supone un gran esfuerzo en tests y demás y eso tendrá que esperar un poco.
Lo he organizado todo para que copies y pegues el código en un fichero en /lib y lo requieras desde el environment.rb con eso debería valer pero úsalo bajo tu entera responsabilidad :)
La extensión provee de dos nuevos validates:
- validates_attachment_width
- validates_attachment_height
Que se pueden usar con toda la convinación de opciones que el nativo ‘validates_attachment_size‘.
Por ejemplo:
validates_attachment_width :photo, :greater_than => 576, :less_than => 1000 validates_attachment_height :photo, :greater_than => 150, :less_than => 300 validates_attachment_width :photo, :in => 576..1000 validates_attachment_height :photo, :in => 150..300, :message => "file height must be between :min and :max pixels."
Tenemos por ejemplo la validación del ancho:
def validates_attachment_width name, options = {} min = options[:greater_than] || (options[:in] && options[:in].first) || 0 max = options[:less_than] || (options[:in] && options[:in].last) || (1.0/0) range = (min..max) message = options[:message] || "file width must be between :min and :max pixels." attachment_definitions[name][:validations][:width] = lambda do |attachment, instance| if attachment.file? && !range.include?( Geometry.from_file(attachment.queued_for_write[:original]).width.to_i ) message.gsub(/:min/, min.to_s).gsub(/:max/, max.to_s) end end end
El código completo lo tienes aquí: paperclip_validations_extended.rb.
Un ejemplo de tests y usos lo tienes aquí: paperclip_validations_extended_test.rb.
A mi me está funcionando.. y a tí?
Febrero 1st, 2009 at 8:57 pm
Fernando muy bueno, mira a ver si esto te cuadra que queda un poco más limpio. No lo he probado pero creo que deberia funcionar:
http://gist.github.com/55946
Saludos
Febrero 1st, 2009 at 11:32 pm
Gracias David.. lo dejo aquí apuntado para cuando llegue la hora de hacer el parche
Mayo 27th, 2009 at 6:57 pm
Hola,
Muy interesante. Soy un poco novato en rails. Sabeis si es posible dar un valor a un campo de la tabla de fotos en funcion de su proporcion? O sea que si la imagen es de altura tal i amplitud tanto actualizar un campo de la imagen que diga que es de la clase “A”.
Gracias
Mayo 27th, 2009 at 7:25 pm
@Jaime, claro que se podrá.. pero tendrás que encargarte tú.. no creo que paperplic incorpore algo tan personalizado como lo que necesitas.
Diciembre 2nd, 2009 at 3:27 pm
Muy buena solución. Por lo que estuve viendo es prácticamente la única disponible tanto en inglés como en español! Así que felicitaciones =D
Hace poco escribí un artículo también en español y muy completo sobre cómo utilizar Paperclip en Rails. Por si alguien está interesado: http://bit.ly/paperclip-tutorial+
Noviembre 19th, 2018 at 1:40 pm
I intended to draft you the little word in order to thank you very much over again for your personal lovely thoughts you have discussed here. It is certainly unbelievably open-handed of you to make unreservedly all that some people could have marketed as an e-book to generate some bucks on their own, most importantly seeing that you might well have done it in the event you considered necessary. The basics in addition worked to become a fantastic way to fully grasp that other people have the identical dreams the same as my personal own to learn good deal more in respect of this condition. I’m certain there are a lot more pleasurable opportunities up front for people who looked over your blog.