Class: Paperclip::Validate::SizeValidator

Inherits:
DataMapper::Validate::GenericValidator
  • Object
show all
Defined in:
lib/dm-paperclip/validations.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#call(target) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dm-paperclip/validations.rb', line 37

def call(target)
  field_value = target.validation_property_value(:"#{@field_name}_file_size")
  return true if field_value.nil?

  @options[:in] = (@options[:greater_than]..(1/0)) unless @options[:greater_than].nil?
  @options[:in] = (0..@options[:less_than])        unless @options[:less_than].nil?
  return true if @options[:in].include? field_value.to_i

  error_message ||= @options[:message] unless @options[:message].nil?
  error_message ||= sprintf("%s must be less than %s bytes", @field_name.to_s.humanize, @options[:less_than]) unless @options[:less_than].nil?
  error_message ||= sprintf("%s must be greater than %s bytes", @field_name.to_s.humanize, @options[:greater_than]) unless @options[:greater_than].nil?
  error_message ||= sprintf("%s must be between %s and %s bytes", @field_name.to_s.humanize, @options[:in].first, @options[:in].last)
  add_error(target, error_message , @field_name)
  return false
end