Class: Paperclip::Validators::AttachmentWidthValidator

Inherits:
ActiveModel::Validations::NumericalityValidator
  • Object
show all
Defined in:
lib/paperclip/validators/attachment_width_validator.rb

Constant Summary collapse

AVAILABLE_CHECKS =
[:less_than, :less_than_or_equal_to, :greater_than, :greater_than_or_equal_to]

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ AttachmentWidthValidator

Returns a new instance of AttachmentWidthValidator.



8
9
10
11
# File 'lib/paperclip/validators/attachment_width_validator.rb', line 8

def initialize(options)
  extract_options(options)
  super
end

Instance Method Details

#check_validity!Object



29
30
31
32
33
# File 'lib/paperclip/validators/attachment_width_validator.rb', line 29

def check_validity!
  unless (AVAILABLE_CHECKS + [:in]).any? { |argument| options.has_key?(argument) }
    raise ArgumentError, "You must pass either :less_than, :greater_than, or :in to the validator"
  end
end

#validate_each(record, attr_name, value) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/paperclip/validators/attachment_width_validator.rb', line 13

def validate_each(record, attr_name, value)
  attachment = record.send(attr_name)
  dimensions = Paperclip::Geometry.from_file(attachment.queued_for_write[:original])
  value = dimensions.width.to_i
  options.slice(*AVAILABLE_CHECKS).each do |option, option_value|
    option_value = extract_option_value(option, option_value)
    unless value.send(CHECKS[option], option_value)
      error_message_key = options[:in] ? :in_between : option
      record.errors.add(attr_name, error_message_key, filtered_options(value).merge(
        :min => min_value,
        :max => max_value
      ))
    end
  end
end