Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Validation matchers respect conditionals on the validations

  • Loading branch information...
commit 5d4ba628dedc90cb035c874f0479985b7dc767a1 1 parent 1cb40e3
Jon Yurek jyurek authored
8 lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
View
@@ -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?
14 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
View
@@ -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
8 lib/paperclip/matchers/validate_attachment_size_matcher.rb
View
@@ -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?
23 test/matchers/validate_attachment_content_type_matcher_test.rb
View
@@ -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 test/matchers/validate_attachment_presence_matcher_test.rb
View
@@ -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 test/matchers/validate_attachment_size_matcher_test.rb
View
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.