Skip to content
This repository
Browse code

Add #check_validity! to validators

  • Loading branch information...
commit b54904e0e46a421e4f5d6af3d034a3a72fc742c0 1 parent d3a7427
Prem Sichanugrist authored March 16, 2012
6  lib/paperclip/validators/attachment_content_type_validator.rb
@@ -16,6 +16,12 @@ def validate_each(record, attribute, value)
16 16
           end
17 17
         end
18 18
       end
  19
+
  20
+      def check_validity!
  21
+        unless options.has_key?(:content_type)
  22
+          raise ArgumentError, "You must pass in :content_type to the validator"
  23
+        end
  24
+      end
19 25
     end
20 26
 
21 27
     module HelperMethods
6  lib/paperclip/validators/attachment_size_validator.rb
@@ -31,6 +31,12 @@ def validate_each(record, attr_name, value)
31 31
         end
32 32
       end
33 33
 
  34
+      def check_validity!
  35
+        unless (AVAILABLE_CHECKS + [:in]).any? { |argument| options.has_key?(argument) }
  36
+          raise ArgumentError, "You must pass either :less_than, :greater_than, or :in to the validator"
  37
+        end
  38
+      end
  39
+
34 40
       private
35 41
 
36 42
       def extract_options(options)
2  test/paperclip_test.rb
@@ -49,6 +49,7 @@ class PaperclipTest < Test::Unit::TestCase
49 49
       d3 = Dummy.create(:avatar => @file)
50 50
       @expected = [d1, d3]
51 51
     end
  52
+
52 53
     should "yield every instance of a model that has an attachment" do
53 54
       actual = []
54 55
       Paperclip.each_instance_with_attachment("Dummy", "avatar") do |instance|
@@ -166,6 +167,7 @@ class ::SubDummy < Dummy; end
166 167
       end
167 168
 
168 169
       teardown do
  170
+        SubDummy.delete_all
169 171
         Object.send(:remove_const, "SubDummy") rescue nil
170 172
       end
171 173
     end
22  test/validators/attachment_content_type_validator_test.rb
@@ -103,4 +103,26 @@ def build_validator(options)
103 103
       end
104 104
     end
105 105
   end
  106
+
  107
+  context "using the helper" do
  108
+    setup do
  109
+      Dummy.validates_attachment_content_type :avatar, :content_type => "image/jpg"
  110
+    end
  111
+
  112
+    should "add the validator to the class" do
  113
+      assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_content_type }
  114
+    end
  115
+  end
  116
+
  117
+  context "given options" do
  118
+    should "raise argument error if no required argument was given" do
  119
+      assert_raises(ArgumentError) do
  120
+        build_validator :message => "Some message"
  121
+      end
  122
+    end
  123
+
  124
+    should "not raise arguemnt error if :content_type was given" do
  125
+      build_validator :content_type => "image/jpg"
  126
+    end
  127
+  end
106 128
 end
28  test/validators/attachment_size_validator_test.rb
@@ -176,4 +176,32 @@ def self.should_not_allow_attachment_file_size(size, options = {})
176 176
         :message => "must be in between 5120 Bytes and 10240 Bytes"
177 177
     end
178 178
   end
  179
+
  180
+  context "using the helper" do
  181
+    setup do
  182
+      Dummy.validates_attachment_size :avatar, :in => (5.kilobytes..10.kilobytes)
  183
+    end
  184
+
  185
+    should "add the validator to the class" do
  186
+      assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_size }
  187
+    end
  188
+  end
  189
+
  190
+  context "given options" do
  191
+    should "raise argument error if no required argument was given" do
  192
+      assert_raises(ArgumentError) do
  193
+        build_validator :message => "Some message"
  194
+      end
  195
+    end
  196
+
  197
+    (Paperclip::Validators::AttachmentSizeValidator::AVAILABLE_CHECKS).each do |argument|
  198
+      should "not raise arguemnt error if #{argument} was given" do
  199
+        build_validator argument => 5.kilobytes
  200
+      end
  201
+    end
  202
+
  203
+    should "not raise argument error if :in was given" do
  204
+      build_validator :in => (5.kilobytes..10.kilobytes)
  205
+    end
  206
+  end
179 207
 end

0 notes on commit b54904e

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