Class: Paperclip::Validators::AttachmentHeightValidator
- Inherits:
-
ActiveModel::Validations::NumericalityValidator
- Object
- ActiveModel::Validations::NumericalityValidator
- Paperclip::Validators::AttachmentHeightValidator
- Defined in:
- lib/paperclip/validators/attachment_height_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
- #check_validity! ⇒ Object
-
#initialize(options) ⇒ AttachmentHeightValidator
constructor
A new instance of AttachmentHeightValidator.
- #validate_each(record, attr_name, value) ⇒ Object
Constructor Details
#initialize(options) ⇒ AttachmentHeightValidator
Returns a new instance of AttachmentHeightValidator.
8 9 10 11 |
# File 'lib/paperclip/validators/attachment_height_validator.rb', line 8 def initialize() () super end |
Instance Method Details
#check_validity! ⇒ Object
29 30 31 32 33 |
# File 'lib/paperclip/validators/attachment_height_validator.rb', line 29 def check_validity! unless (AVAILABLE_CHECKS + [:in]).any? { |argument| .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_height_validator.rb', line 13 def validate_each(record, attr_name, value) = record.send(attr_name) dimensions = Paperclip::Geometry.from_file(.queued_for_write[:original]) value = dimensions.height.to_i .slice(*AVAILABLE_CHECKS).each do |option, option_value| option_value = extract_option_value(option, option_value) unless value.send(CHECKS[option], option_value) = [:in] ? :in_between : option record.errors.add(attr_name, , (value).merge( :min => min_value, :max => max_value )) end end end |