From 61a05368ab5298171bc70209ef2c4f184643bfad Mon Sep 17 00:00:00 2001 From: Jon Yurek Date: Thu, 20 Oct 2011 16:19:37 -0400 Subject: [PATCH] Fixes validates_attachment_presence just flat out not working --- lib/paperclip.rb | 4 +++- test/paperclip_test.rb | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/paperclip.rb b/lib/paperclip.rb index 1bc872a28..304fcc524 100644 --- a/lib/paperclip.rb +++ b/lib/paperclip.rb @@ -388,7 +388,9 @@ def validates_attachment_thumbnails name, options = {} def validates_attachment_presence name, options = {} message = options[:message] || :empty validates_each :"#{name}_file_name" do |record, attr, value| - record.errors.add(name, message) if attr.blank? + if_clause_passed = options[:if].nil? || (options[:if].call(record) != false) + unless_clause_passed = options[:unless].nil? || (!!options[:unless].call(record) == false) + record.errors.add(name, message) if if_clause_passed && unless_clause_passed && value.blank? end end diff --git a/test/paperclip_test.rb b/test/paperclip_test.rb index b049aa143..fd9420f98 100644 --- a/test/paperclip_test.rb +++ b/test/paperclip_test.rb @@ -238,7 +238,7 @@ def self.should_validate validation, options, valid_file, invalid_file @dummy.avatar = valid_file @dummy.valid? end - should "not have an error when assigned a valid file" do + should "not have an error" do assert_equal 0, @dummy.errors.size, @dummy.errors.full_messages.join(", ") end end @@ -247,7 +247,7 @@ def self.should_validate validation, options, valid_file, invalid_file @dummy.avatar = invalid_file @dummy.valid? end - should "have an error when assigned a valid file" do + should "have an error" do assert @dummy.errors.size > 0 end end