Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Validation matchers respect conditionals on the validations
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Yurek committed Feb 3, 2012
1 parent 1cb40e3 commit 5d4ba62
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 15 deletions.
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down
14 changes: 7 additions & 7 deletions lib/paperclip/matchers/validate_attachment_presence_matcher.rb
Expand Up @@ -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

Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/paperclip/matchers/validate_attachment_size_matcher.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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?
Expand Down
23 changes: 23 additions & 0 deletions test/matchers/validate_attachment_content_type_matcher_test.rb
Expand Up @@ -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
21 changes: 21 additions & 0 deletions test/matchers/validate_attachment_presence_matcher_test.rb
Expand Up @@ -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
21 changes: 21 additions & 0 deletions test/matchers/validate_attachment_size_matcher_test.rb
Expand Up @@ -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

0 comments on commit 5d4ba62

Please sign in to comment.