Parent

Files

Class/Module Index [+]

Quicksearch

Paperclip::Validators::AttachmentSizeValidator

Constants

AVAILABLE_CHECKS

Public Class Methods

new(options) click to toggle source
# File lib/paperclip/validators/attachment_size_validator.rb, line 8
def initialize(options)
  extract_options(options)
  super
end

Public Instance Methods

check_validity!() click to toggle source
# File lib/paperclip/validators/attachment_size_validator.rb, line 34
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) click to toggle source
# File lib/paperclip/validators/attachment_size_validator.rb, line 13
def validate_each(record, attr_name, value)
  attr_name = "#{attr_name}_file_size".to_sym
  value = record.send(:read_attribute_for_validation, attr_name)

  unless value.blank?
    options.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)

      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_in_human_size(record),
          :max => max_value_in_human_size(record),
          :count => human_size(option_value)
        ))
      end
    end
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.