diff --git a/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb b/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb index 1dcf3678f..fec6f0457 100644 --- a/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +++ b/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb @@ -33,7 +33,7 @@ def rejecting *types def matches? subject @subject = subject - @subject = @subject.class unless Class === @subject + @subject = @subject.new if @subject.class == Class @allowed_types && @rejected_types && allowed_types_allowed? && rejected_types_rejected? end @@ -63,9 +63,9 @@ def description def type_allowed?(type) file = StringIO.new(".") file.content_type = type - (subject = @subject.new).attachment_for(@attachment_name).assign(file) - subject.valid? - subject.errors[:"#{@attachment_name}_content_type"].blank? + @subject.attachment_for(@attachment_name).assign(file) + @subject.valid? + @subject.errors[:"#{@attachment_name}_content_type"].blank? end def allowed_types_allowed? diff --git a/lib/paperclip/matchers/validate_attachment_presence_matcher.rb b/lib/paperclip/matchers/validate_attachment_presence_matcher.rb index 37c4db061..6f732a02d 100644 --- a/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +++ b/lib/paperclip/matchers/validate_attachment_presence_matcher.rb @@ -18,7 +18,7 @@ def initialize attachment_name def matches? subject @subject = subject - @subject = @subject.class unless Class === @subject + @subject = subject.new if subject.class == Class error_when_not_valid? && no_error_when_valid? end @@ -37,16 +37,16 @@ def description protected def error_when_not_valid? - (subject = @subject.new).send(@attachment_name).assign(nil) - subject.valid? - not subject.errors[:"#{@attachment_name}_file_name"].blank? + @subject.send(@attachment_name).assign(nil) + @subject.valid? + not @subject.errors[:"#{@attachment_name}_file_name"].blank? end def no_error_when_valid? @file = StringIO.new(".") - (subject = @subject.new).send(@attachment_name).assign(@file) - subject.valid? - subject.errors[:"#{@attachment_name}_file_name"].blank? + @subject.send(@attachment_name).assign(@file) + @subject.valid? + @subject.errors[:"#{@attachment_name}_file_name"].blank? end end end diff --git a/lib/paperclip/matchers/validate_attachment_size_matcher.rb b/lib/paperclip/matchers/validate_attachment_size_matcher.rb index 62bd67b81..b821af44b 100644 --- a/lib/paperclip/matchers/validate_attachment_size_matcher.rb +++ b/lib/paperclip/matchers/validate_attachment_size_matcher.rb @@ -38,7 +38,7 @@ def in range def matches? subject @subject = subject - @subject = @subject.class unless Class === @subject + @subject = @subject.new if @subject.class == Class lower_than_low? && higher_than_low? && lower_than_high? && higher_than_high? end @@ -67,9 +67,9 @@ def passes_validation_with_size(new_size) override_method(file, :size){ new_size } override_method(file, :to_tempfile){ file } - (subject = @subject.new).send(@attachment_name).assign(file) - subject.valid? - subject.errors[:"#{@attachment_name}_file_size"].blank? + @subject.send(@attachment_name).assign(file) + @subject.valid? + @subject.errors[:"#{@attachment_name}_file_size"].blank? end def lower_than_low? diff --git a/test/matchers/validate_attachment_content_type_matcher_test.rb b/test/matchers/validate_attachment_content_type_matcher_test.rb index 551f490e2..35d9a1520 100644 --- a/test/matchers/validate_attachment_content_type_matcher_test.rb +++ b/test/matchers/validate_attachment_content_type_matcher_test.rb @@ -83,5 +83,28 @@ class ValidateAttachmentContentTypeMatcherTest < Test::Unit::TestCase should_reject_dummy_class end + + context "using an :if to control the validation" do + setup do + @dummy_class.class_eval do + validates_attachment_content_type :avatar, :content_type => %r{image/*} , :if => :go + attr_accessor :go + end + @matcher = self.class.validate_attachment_content_type(:avatar). + allowing(%w(image/png image/jpeg)). + rejecting(%w(audio/mp3 application/octet-stream)) + @dummy = @dummy_class.new + end + + should "run the validation if the control is true" do + @dummy.go = true + assert_accepts @matcher, @dummy + end + + should "not run the validation if the control is false" do + @dummy.go = false + assert_rejects @matcher, @dummy + end + end end end diff --git a/test/matchers/validate_attachment_presence_matcher_test.rb b/test/matchers/validate_attachment_presence_matcher_test.rb index e798aaf00..341e4f883 100644 --- a/test/matchers/validate_attachment_presence_matcher_test.rb +++ b/test/matchers/validate_attachment_presence_matcher_test.rb @@ -22,5 +22,26 @@ class ValidateAttachmentPresenceMatcherTest < Test::Unit::TestCase should_accept_dummy_class end + + context "using an :if to control the validation" do + setup do + @dummy_class.class_eval do + validates_attachment_presence :avatar, :if => :go + attr_accessor :go + end + @dummy = @dummy_class.new + @dummy.avatar = nil + end + + should "run the validation if the control is true" do + @dummy.go = true + assert_accepts @matcher, @dummy + end + + should "not run the validation if the control is false" do + @dummy.go = false + assert_rejects @matcher, @dummy + end + end end end diff --git a/test/matchers/validate_attachment_size_matcher_test.rb b/test/matchers/validate_attachment_size_matcher_test.rb index 0434a7c01..0285ce348 100644 --- a/test/matchers/validate_attachment_size_matcher_test.rb +++ b/test/matchers/validate_attachment_size_matcher_test.rb @@ -47,5 +47,26 @@ class ValidateAttachmentSizeMatcherTest < Test::Unit::TestCase should_accept_dummy_class end end + + context "using an :if to control the validation" do + setup do + @dummy_class.class_eval do + validates_attachment_size :avatar, :greater_than => 1024, :if => :go + attr_accessor :go + end + @dummy = @dummy_class.new + @matcher = self.class.validate_attachment_size(:avatar).greater_than(1024) + end + + should "run the validation if the control is true" do + @dummy.go = true + assert_accepts @matcher, @dummy + end + + should "not run the validation if the control is false" do + @dummy.go = false + assert_rejects @matcher, @dummy + end + end end end