Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add #check_validity! to validators

  • Loading branch information...
commit b54904e0e46a421e4f5d6af3d034a3a72fc742c0 1 parent d3a7427
Prem Sichanugrist sikachu authored
6 lib/paperclip/validators/attachment_content_type_validator.rb
View
@@ -16,6 +16,12 @@ def validate_each(record, attribute, value)
end
end
end
+
+ def check_validity!
+ unless options.has_key?(:content_type)
+ raise ArgumentError, "You must pass in :content_type to the validator"
+ end
+ end
end
module HelperMethods
6 lib/paperclip/validators/attachment_size_validator.rb
View
@@ -31,6 +31,12 @@ def validate_each(record, attr_name, value)
end
end
+ def check_validity!
+ unless (AVAILABLE_CHECKS + [:in]).any? { |argument| options.has_key?(argument) }
+ raise ArgumentError, "You must pass either :less_than, :greater_than, or :in to the validator"
+ end
+ end
+
private
def extract_options(options)
2  test/paperclip_test.rb
View
@@ -49,6 +49,7 @@ class PaperclipTest < Test::Unit::TestCase
d3 = Dummy.create(:avatar => @file)
@expected = [d1, d3]
end
+
should "yield every instance of a model that has an attachment" do
actual = []
Paperclip.each_instance_with_attachment("Dummy", "avatar") do |instance|
@@ -166,6 +167,7 @@ class ::SubDummy < Dummy; end
end
teardown do
+ SubDummy.delete_all
Object.send(:remove_const, "SubDummy") rescue nil
end
end
22 test/validators/attachment_content_type_validator_test.rb
View
@@ -103,4 +103,26 @@ def build_validator(options)
end
end
end
+
+ context "using the helper" do
+ setup do
+ Dummy.validates_attachment_content_type :avatar, :content_type => "image/jpg"
+ end
+
+ should "add the validator to the class" do
+ assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_content_type }
+ end
+ end
+
+ context "given options" do
+ should "raise argument error if no required argument was given" do
+ assert_raises(ArgumentError) do
+ build_validator :message => "Some message"
+ end
+ end
+
+ should "not raise arguemnt error if :content_type was given" do
+ build_validator :content_type => "image/jpg"
+ end
+ end
end
28 test/validators/attachment_size_validator_test.rb
View
@@ -176,4 +176,32 @@ def self.should_not_allow_attachment_file_size(size, options = {})
:message => "must be in between 5120 Bytes and 10240 Bytes"
end
end
+
+ context "using the helper" do
+ setup do
+ Dummy.validates_attachment_size :avatar, :in => (5.kilobytes..10.kilobytes)
+ end
+
+ should "add the validator to the class" do
+ assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_size }
+ end
+ end
+
+ context "given options" do
+ should "raise argument error if no required argument was given" do
+ assert_raises(ArgumentError) do
+ build_validator :message => "Some message"
+ end
+ end
+
+ (Paperclip::Validators::AttachmentSizeValidator::AVAILABLE_CHECKS).each do |argument|
+ should "not raise arguemnt error if #{argument} was given" do
+ build_validator argument => 5.kilobytes
+ end
+ end
+
+ should "not raise argument error if :in was given" do
+ build_validator :in => (5.kilobytes..10.kilobytes)
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.