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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/dm-paperclip/validations.rb', line 74

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

  unless field_value.nil? || Paperclip::Ext.blank?(field_value.original_filename)
    unless Paperclip::Ext.blank?(@options[:content_type])
      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",DataMapper::Inflector.humanize(@field_name), content_type)
        add_error(target, error_message , @field_name)
        return false
      end
    end
  end

  return true
end