Class: SexyValidations::Validators::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/sexy_validations/validators/image.rb

Class Method Summary collapse

Class Method Details

.validate(model, attribute, value, options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sexy_validations/validators/image.rb', line 5

def self.validate(model, attribute, value, options)
  return unless value

  unless options.is_a?(Hash)
    options = {
      :geometry => options,
    }
  end

  file = SexyValidations.load_validator(:file).file_from_value(value)
  return unless file

  if options[:geometry].is_a?(String)
    geo1 = Sequel::Plugins::Paperclip::Processors::Image::Geometry.from_s(options[:geometry])
    geo2 = Sequel::Plugins::Paperclip::Processors::Image::Geometry.from_file(file)
    if geo2
      if geo2.width < geo1.width
        model.errors.add(attribute, "zu klein (weniger als %d Pixel breit)"%[geo1.width])
      end
      if geo2.height < geo1.height
        model.errors.add(attribute, "zu klein (weniger als %d Pixel hoch)"%[geo1.height])
      end
    else
      model.errors.add(attribute, "Bildformat unbekannt oder Datei beschÃĪdigt")
    end
  end
end