Class: Paperclip::Validate::ContentTypeValidator

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dm-paperclip/validations.rb', line 67

def call(target)
  valid_types = [@options[:content_type]].flatten
  field_value = target.validation_property_value(@field_name)

  unless field_value.nil? || field_value.original_filename.blank?
    unless @options[:content_type].blank?
      content_type = target.validation_property_value(:"#{@field_name}_content_type")
      unless valid_types.any?{|t| t === content_type }
        error_message ||= @options[:message] unless @options[:message].nil?
        error_message ||= sprintf("%s's content type of '%s' is not a valid content type", @field_name.to_s.humanize, content_type)
        add_error(target, error_message , @field_name)
        return false
      end
    end
  end

  return true
end