Skip to content
This repository
Browse code

Validation matchers respect conditionals on the validations

  • Loading branch information...
commit 5d4ba628dedc90cb035c874f0479985b7dc767a1 1 parent 1cb40e3
Jon Yurek authored February 03, 2012
8  lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
@@ -33,7 +33,7 @@ def rejecting *types
33 33
 
34 34
         def matches? subject
35 35
           @subject = subject
36  
-          @subject = @subject.class unless Class === @subject
  36
+          @subject = @subject.new if @subject.class == Class
37 37
           @allowed_types && @rejected_types &&
38 38
           allowed_types_allowed? && rejected_types_rejected?
39 39
         end
@@ -63,9 +63,9 @@ def description
63 63
         def type_allowed?(type)
64 64
           file = StringIO.new(".")
65 65
           file.content_type = type
66  
-          (subject = @subject.new).attachment_for(@attachment_name).assign(file)
67  
-          subject.valid?
68  
-          subject.errors[:"#{@attachment_name}_content_type"].blank?
  66
+          @subject.attachment_for(@attachment_name).assign(file)
  67
+          @subject.valid?
  68
+          @subject.errors[:"#{@attachment_name}_content_type"].blank?
69 69
         end
70 70
 
71 71
         def allowed_types_allowed?
14  lib/paperclip/matchers/validate_attachment_presence_matcher.rb
@@ -18,7 +18,7 @@ def initialize attachment_name
18 18
 
19 19
         def matches? subject
20 20
           @subject = subject
21  
-          @subject = @subject.class unless Class === @subject
  21
+          @subject = subject.new if subject.class == Class
22 22
           error_when_not_valid? && no_error_when_valid?
23 23
         end
24 24
 
@@ -37,16 +37,16 @@ def description
37 37
         protected
38 38
 
39 39
         def error_when_not_valid?
40  
-          (subject = @subject.new).send(@attachment_name).assign(nil)
41  
-          subject.valid?
42  
-          not subject.errors[:"#{@attachment_name}_file_name"].blank?
  40
+          @subject.send(@attachment_name).assign(nil)
  41
+          @subject.valid?
  42
+          not @subject.errors[:"#{@attachment_name}_file_name"].blank?
43 43
         end
44 44
 
45 45
         def no_error_when_valid?
46 46
           @file = StringIO.new(".")
47  
-          (subject = @subject.new).send(@attachment_name).assign(@file)
48  
-          subject.valid?
49  
-          subject.errors[:"#{@attachment_name}_file_name"].blank?
  47
+          @subject.send(@attachment_name).assign(@file)
  48
+          @subject.valid?
  49
+          @subject.errors[:"#{@attachment_name}_file_name"].blank?
50 50
         end
51 51
       end
52 52
     end
8  lib/paperclip/matchers/validate_attachment_size_matcher.rb
@@ -38,7 +38,7 @@ def in range
38 38
 
39 39
         def matches? subject
40 40
           @subject = subject
41  
-          @subject = @subject.class unless Class === @subject
  41
+          @subject = @subject.new if @subject.class == Class
42 42
           lower_than_low? && higher_than_low? && lower_than_high? && higher_than_high?
43 43
         end
44 44
 
@@ -67,9 +67,9 @@ def passes_validation_with_size(new_size)
67 67
           override_method(file, :size){ new_size }
68 68
           override_method(file, :to_tempfile){ file }
69 69
 
70  
-          (subject = @subject.new).send(@attachment_name).assign(file)
71  
-          subject.valid?
72  
-          subject.errors[:"#{@attachment_name}_file_size"].blank?
  70
+          @subject.send(@attachment_name).assign(file)
  71
+          @subject.valid?
  72
+          @subject.errors[:"#{@attachment_name}_file_size"].blank?
73 73
         end
74 74
 
75 75
         def lower_than_low?
23  test/matchers/validate_attachment_content_type_matcher_test.rb
@@ -83,5 +83,28 @@ class ValidateAttachmentContentTypeMatcherTest < Test::Unit::TestCase
83 83
 
84 84
       should_reject_dummy_class
85 85
     end
  86
+
  87
+    context "using an :if to control the validation" do
  88
+      setup do
  89
+        @dummy_class.class_eval do
  90
+          validates_attachment_content_type :avatar, :content_type => %r{image/*} , :if => :go
  91
+          attr_accessor :go
  92
+        end
  93
+        @matcher = self.class.validate_attachment_content_type(:avatar).
  94
+                        allowing(%w(image/png image/jpeg)).
  95
+                        rejecting(%w(audio/mp3 application/octet-stream))
  96
+        @dummy = @dummy_class.new
  97
+      end
  98
+
  99
+      should "run the validation if the control is true" do
  100
+        @dummy.go = true
  101
+        assert_accepts @matcher, @dummy
  102
+      end
  103
+
  104
+      should "not run the validation if the control is false" do
  105
+        @dummy.go = false
  106
+        assert_rejects @matcher, @dummy
  107
+      end
  108
+    end
86 109
   end
87 110
 end
21  test/matchers/validate_attachment_presence_matcher_test.rb
@@ -22,5 +22,26 @@ class ValidateAttachmentPresenceMatcherTest < Test::Unit::TestCase
22 22
 
23 23
       should_accept_dummy_class
24 24
     end
  25
+
  26
+    context "using an :if to control the validation" do
  27
+      setup do
  28
+        @dummy_class.class_eval do
  29
+          validates_attachment_presence :avatar, :if => :go
  30
+          attr_accessor :go
  31
+        end
  32
+        @dummy = @dummy_class.new
  33
+        @dummy.avatar = nil
  34
+      end
  35
+
  36
+      should "run the validation if the control is true" do
  37
+        @dummy.go = true
  38
+        assert_accepts @matcher, @dummy
  39
+      end
  40
+
  41
+      should "not run the validation if the control is false" do
  42
+        @dummy.go = false
  43
+        assert_rejects @matcher, @dummy
  44
+      end
  45
+    end
25 46
   end
26 47
 end
21  test/matchers/validate_attachment_size_matcher_test.rb
@@ -47,5 +47,26 @@ class ValidateAttachmentSizeMatcherTest < Test::Unit::TestCase
47 47
         should_accept_dummy_class
48 48
       end
49 49
     end
  50
+
  51
+    context "using an :if to control the validation" do
  52
+      setup do
  53
+        @dummy_class.class_eval do
  54
+          validates_attachment_size :avatar, :greater_than => 1024, :if => :go
  55
+          attr_accessor :go
  56
+        end
  57
+        @dummy = @dummy_class.new
  58
+        @matcher = self.class.validate_attachment_size(:avatar).greater_than(1024)
  59
+      end
  60
+
  61
+      should "run the validation if the control is true" do
  62
+        @dummy.go = true
  63
+        assert_accepts @matcher, @dummy
  64
+      end
  65
+
  66
+      should "not run the validation if the control is false" do
  67
+        @dummy.go = false
  68
+        assert_rejects @matcher, @dummy
  69
+      end
  70
+    end
50 71
   end
51 72
 end

0 notes on commit 5d4ba62

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