From c2c832d98709fde397861915a7a17e76cf2a38f4 Mon Sep 17 00:00:00 2001 From: Jon Yurek Date: Tue, 30 Dec 2008 14:09:20 -0500 Subject: [PATCH] Assigning nil to an attachment still deals with validations correctly --- lib/paperclip.rb | 2 +- test/paperclip_test.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/paperclip.rb b/lib/paperclip.rb index ebb6f270a..088ad7c18 100644 --- a/lib/paperclip.rb +++ b/lib/paperclip.rb @@ -255,7 +255,7 @@ def validates_attachment_content_type name, options = {} valid_types = [options[:content_type]].flatten unless attachment.original_filename.blank? - unless options[:content_type].blank? + unless valid_types.blank? content_type = attachment.instance_read(:content_type) unless valid_types.any?{|t| t === content_type } options[:message] || "is not one of the allowed file types." diff --git a/test/paperclip_test.rb b/test/paperclip_test.rb index 35ac4aee0..4fad3fbc4 100644 --- a/test/paperclip_test.rb +++ b/test/paperclip_test.rb @@ -177,6 +177,21 @@ def self.should_validate validation, options, valid_file, invalid_file Dummy.send(:"validates_attachment_#{validation}", :avatar, options) @dummy = Dummy.new end + context "and assigning nil" do + setup do + @dummy.avatar = nil + @dummy.valid? + end + if validation == :presence + should "have an error on the attachment" do + assert @dummy.errors.on(:avatar) + end + else + should "not have an error on the attachment" do + assert_nil @dummy.errors.on(:avatar) + end + end + end context "and assigned a valid file" do setup do @dummy.avatar = valid_file