Class: Paperclip::Validators::AttachmentSizeValidator
- Inherits:
-
ActiveModel::Validations::NumericalityValidator
- Object
- ActiveModel::Validations::NumericalityValidator
- Paperclip::Validators::AttachmentSizeValidator
- Defined in:
- lib/paperclip/validators/attachment_size_validator.rb
Constant Summary collapse
- AVAILABLE_CHECKS =
[:less_than, :less_than_or_equal_to, :greater_than, :greater_than_or_equal_to].freeze
Class Method Summary collapse
Instance Method Summary collapse
- #check_validity! ⇒ Object
-
#initialize(options) ⇒ AttachmentSizeValidator
constructor
A new instance of AttachmentSizeValidator.
- #validate_each(record, attr_name, value) ⇒ Object
Constructor Details
#initialize(options) ⇒ AttachmentSizeValidator
Returns a new instance of AttachmentSizeValidator.
10 11 12 13 |
# File 'lib/paperclip/validators/attachment_size_validator.rb', line 10 def initialize() () super end |
Class Method Details
.helper_method_name ⇒ Object
15 16 17 |
# File 'lib/paperclip/validators/attachment_size_validator.rb', line 15 def self.helper_method_name :validates_attachment_size end |
Instance Method Details
#check_validity! ⇒ Object
56 57 58 59 60 |
# File 'lib/paperclip/validators/attachment_size_validator.rb', line 56 def check_validity! unless (AVAILABLE_CHECKS + [:in]).any? { |argument| .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
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/paperclip/validators/attachment_size_validator.rb', line 19 def validate_each(record, attr_name, value) base_attr_name = attr_name attr_name = "#{attr_name}_file_size".to_sym error_attrs = [] case [:add_validation_errors_to] when :base error_attrs << base_attr_name when :attribute error_attrs << attr_name else error_attrs << base_attr_name error_attrs << attr_name end value = record.send(:read_attribute_for_validation, attr_name) unless value.blank? .slice(*AVAILABLE_CHECKS).each do |option, option_value| option_value = option_value.call(record) if option_value.is_a?(Proc) option_value = extract_option_value(option, option_value) operator = ActiveModel::VERSION::MAJOR >= 7 ? COMPARE_CHECKS[option] : CHECKS[option] unless value.send(operator, option_value) = [:in] ? :in_between : option error_attrs.each do |error_attr_name| record.errors.add(error_attr_name, , **(value).merge( min: min_value_in_human_size(record), max: max_value_in_human_size(record), count: human_size(option_value) )) end end end end end |