Commit b54904e0 by Prem Sichanugrist

Add #check_validity! to validators

parent d3a74275
...@@ -16,6 +16,12 @@ module Paperclip ...@@ -16,6 +16,12 @@ module Paperclip
end end
end 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 end
module HelperMethods module HelperMethods
......
...@@ -31,6 +31,12 @@ module Paperclip ...@@ -31,6 +31,12 @@ module Paperclip
end end
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 private
def extract_options(options) def extract_options(options)
......
...@@ -49,6 +49,7 @@ class PaperclipTest < Test::Unit::TestCase ...@@ -49,6 +49,7 @@ class PaperclipTest < Test::Unit::TestCase
d3 = Dummy.create(:avatar => @file) d3 = Dummy.create(:avatar => @file)
@expected = [d1, d3] @expected = [d1, d3]
end end
should "yield every instance of a model that has an attachment" do should "yield every instance of a model that has an attachment" do
actual = [] actual = []
Paperclip.each_instance_with_attachment("Dummy", "avatar") do |instance| Paperclip.each_instance_with_attachment("Dummy", "avatar") do |instance|
...@@ -166,6 +167,7 @@ class PaperclipTest < Test::Unit::TestCase ...@@ -166,6 +167,7 @@ class PaperclipTest < Test::Unit::TestCase
end end
teardown do teardown do
SubDummy.delete_all
Object.send(:remove_const, "SubDummy") rescue nil Object.send(:remove_const, "SubDummy") rescue nil
end end
end end
......
...@@ -103,4 +103,26 @@ class AttachmentContentTypeValidatorTest < Test::Unit::TestCase ...@@ -103,4 +103,26 @@ class AttachmentContentTypeValidatorTest < Test::Unit::TestCase
end end
end 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 end
...@@ -176,4 +176,32 @@ class AttachmentSizeValidatorTest < Test::Unit::TestCase ...@@ -176,4 +176,32 @@ class AttachmentSizeValidatorTest < Test::Unit::TestCase
:message => "must be in between 5120 Bytes and 10240 Bytes" :message => "must be in between 5120 Bytes and 10240 Bytes"
end end
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 end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment