Class: Paperclip::Validate::GeometryValidator

Inherits:
DataMapper::Validate::GenericValidator
  • Object
show all
Defined in:
lib/dm-paperclip/validations.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#call(target) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/dm-paperclip/validations.rb', line 109

def call(target)
  field_value = target.validation_property_value(@field_name)
  return true if field_value.queued_for_write[:original].nil?

  geometry = Paperclip::Geometry.from_file(field_value.queued_for_write[:original].path)
  
  return true if @options[:width].include?(geometry.width) && @options[:height].include?(geometry.height)

  error_message ||= sprintf("%s width must be between %s and %s px", DataMapper::Inflector.humanize(@field_name), @options[:width].begin, @options[:width].end) unless @options[:width].include?(geometry.width)
  error_message ||= sprintf("%s height must be between %s and %s px", DataMapper::Inflector.humanize(@field_name), @options[:height].begin, @options[:height].end) unless@options[:height].include?(geometry.height)

  add_error(target, error_message , @field_name)
  return false
end