Skip to content
This repository
Browse code

Fix content_type validator to support blank/nil

ContentTypeValidator now honors the `:allow_nil` and `:allow_blank` option.
  • Loading branch information...
commit 5eed1dcb785e8b42ea7881b1bd822394df6aa3a0 1 parent f647d68
Prem Sichanugrist authored April 16, 2012
9  lib/paperclip/validators/attachment_content_type_validator.rb
... ...
@@ -1,12 +1,19 @@
1 1
 module Paperclip
2 2
   module Validators
3 3
     class AttachmentContentTypeValidator < ActiveModel::EachValidator
  4
+      def initialize(options)
  5
+        options[:allow_nil] = true unless options.has_key?(:allow_nil)
  6
+        super
  7
+      end
  8
+
4 9
       def validate_each(record, attribute, value)
5 10
         attribute = "#{attribute}_content_type".to_sym
6 11
         value = record.send(:read_attribute_for_validation, attribute)
7 12
         allowed_types = [options[:content_type]].flatten
8 13
 
9  
-        if value.present? && allowed_types.none? { |type| type === value }
  14
+        return if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])
  15
+
  16
+        if allowed_types.none? { |type| type === value }
10 17
           record.errors.add(attribute, :invalid, options.merge(
11 18
             :types => allowed_types.join(', ')
12 19
           ))
54  test/validators/attachment_content_type_validator_test.rb
@@ -24,6 +24,58 @@ def build_validator(options)
24 24
     end
25 25
   end
26 26
 
  27
+  context "with :allow_nil option" do
  28
+    context "as true" do
  29
+      setup do
  30
+        build_validator :content_type => "image/png", :allow_nil => true
  31
+        @dummy.stubs(:avatar_content_type => nil)
  32
+        @validator.validate(@dummy)
  33
+      end
  34
+
  35
+      should "allow avatar_content_type as nil" do
  36
+        assert @dummy.errors[:avatar_content_type].blank?
  37
+      end
  38
+    end
  39
+
  40
+    context "as false" do
  41
+      setup do
  42
+        build_validator :content_type => "image/png", :allow_nil => false
  43
+        @dummy.stubs(:avatar_content_type => nil)
  44
+        @validator.validate(@dummy)
  45
+      end
  46
+
  47
+      should "not allow avatar_content_type as nil" do
  48
+        assert @dummy.errors[:avatar_content_type].present?
  49
+      end
  50
+    end
  51
+  end
  52
+
  53
+  context "with :allow_blank option" do
  54
+    context "as true" do
  55
+      setup do
  56
+        build_validator :content_type => "image/png", :allow_blank => true
  57
+        @dummy.stubs(:avatar_content_type => "")
  58
+        @validator.validate(@dummy)
  59
+      end
  60
+
  61
+      should "allow avatar_content_type as blank" do
  62
+        assert @dummy.errors[:avatar_content_type].blank?
  63
+      end
  64
+    end
  65
+
  66
+    context "as false" do
  67
+      setup do
  68
+        build_validator :content_type => "image/png", :allow_blank => false
  69
+        @dummy.stubs(:avatar_content_type => "")
  70
+        @validator.validate(@dummy)
  71
+      end
  72
+
  73
+      should "not allow avatar_content_type as blank" do
  74
+        assert @dummy.errors[:avatar_content_type].present?
  75
+      end
  76
+    end
  77
+  end
  78
+
27 79
   context "with an allowed type" do
28 80
     context "as a string" do
29 81
       setup do
@@ -48,7 +100,7 @@ def build_validator(options)
48 100
         assert @dummy.errors[:avatar_content_type].blank?
49 101
       end
50 102
     end
51  
-    
  103
+
52 104
     context "as a list" do
53 105
       setup do
54 106
         build_validator :content_type => ["image/png", "image/jpg", "image/jpeg"]

0 notes on commit 5eed1dc

Please sign in to comment.
Something went wrong with that request. Please try again.