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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dm-paperclip/validations.rb', line 44

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/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",DataMapper::Inflector.humanize(@field_name), @options[:less_than]) unless @options[:less_than].nil?
  error_message ||= sprintf("%s must be greater than %s bytes",DataMapper::Inflector.humanize(@field_name), @options[:greater_than]) unless @options[:greater_than].nil?
  error_message ||= sprintf("%s must be between %s and %s bytes",DataMapper::Inflector.humanize(@field_name), @options[:in].first, @options[:in].last)
  add_error(target, error_message , @field_name)
  return false
end