Commit 5d4ba628 by Jon Yurek

Validation matchers respect conditionals on the validations

parent 1cb40e35
......@@ -33,7 +33,7 @@ module Paperclip
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 @@ module Paperclip
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?
......
......@@ -18,7 +18,7 @@ module Paperclip
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 @@ module Paperclip
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
......
......@@ -38,7 +38,7 @@ module Paperclip
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 @@ module Paperclip
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?
......
......@@ -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
......@@ -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
......@@ -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
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