Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add ability to specify ".allowing" and ".rejecting" individually with…

… content_type validation matcher.

Closes #472, Closes #532
commit 020625921adae884534608d76c11f65692e4bbec 1 parent 45334fb
@cgs cgs authored sikachu committed
View
34 lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
@@ -17,6 +17,8 @@ def validate_attachment_content_type name
class ValidateAttachmentContentTypeMatcher
def initialize attachment_name
@attachment_name = attachment_name
+ @allowed_types = []
+ @rejected_types = []
end
def allowing *types
@@ -37,13 +39,19 @@ def matches? subject
end
def failure_message
- "Content types #{@allowed_types.join(", ")} should be accepted" +
- " and #{@rejected_types.join(", ")} rejected by #{@attachment_name}"
+ "".tap do |str|
+ str << "Content types #{@allowed_types.join(", ")} should be accepted" if @allowed_types.present?
+ str << "\n" if @allowed_types.present && @rejected_types.present?

@allowed_types.present is obviously typo, right? surprised it's not spotted by anyone yet

@sikachu Admin
sikachu added a note

Right ... I think we should fix that.

Can supply the patch, but only tomorrow :)

@sikachu Admin
sikachu added a note

No worries, it's fixed in 9f38928. Thanks for reporting in :)

Fastest gun in the West, or should I say East? So many thanks! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ str << "Content types #{@rejected_types.join(", ")} should be rejected by #{@attachment_name}" if @rejected_types.present?
+ end
end
def negative_failure_message
- "Content types #{@allowed_types.join(", ")} should be rejected" +
- " and #{@rejected_types.join(", ")} accepted by #{@attachment_name}"
+ "".tap do |str|
+ str << "Content types #{@allowed_types.join(", ")} should be rejected" if @allowed_types.present?
+ str << "\n" if @allowed_types.present && @rejected_types.present?
+ str << "Content types #{@rejected_types.join(", ")} should be accepted by #{@attachment_name}" if @rejected_types.present?
+ end
end
def description
@@ -52,22 +60,20 @@ def description
protected
- def allow_types?(types)
- types.all? do |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?
- end
+ 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?
end
def allowed_types_allowed?
- allow_types?(@allowed_types)
+ @allowed_types.all? { |type| type_allowed?(type) }
end
def rejected_types_rejected?
- not allow_types?(@rejected_types)
+ !@rejected_types.any? { |type| type_allowed?(type) }
end
end
end
View
40 test/matchers/validate_attachment_content_type_matcher_test.rb
@@ -43,5 +43,45 @@ class ValidateAttachmentContentTypeMatcherTest < Test::Unit::TestCase
should_accept_dummy_class
end
+
+ context "given a class that matches and a matcher that only specifies 'allowing'" do
+ setup do
+ @dummy_class.validates_attachment_content_type :avatar, :content_type => %r{image/.*}
+ @matcher = self.class.validate_attachment_content_type(:avatar).
+ allowing(%w(image/png image/jpeg))
+ end
+
+ should_accept_dummy_class
+ end
+
+ context "given a class that does not match and a matcher that only specifies 'allowing'" do
+ setup do
+ @dummy_class.validates_attachment_content_type :avatar, :content_type => %r{audio/.*}
+ @matcher = self.class.validate_attachment_content_type(:avatar).
+ allowing(%w(image/png image/jpeg))
+ end
+
+ should_reject_dummy_class
+ end
+
+ context "given a class that matches and a matcher that only specifies 'rejecting'" do
+ setup do
+ @dummy_class.validates_attachment_content_type :avatar, :content_type => %r{image/.*}
+ @matcher = self.class.validate_attachment_content_type(:avatar).
+ rejecting(%w(audio/mp3 application/octet-stream))
+ end
+
+ should_accept_dummy_class
+ end
+
+ context "given a class that does not match and a matcher that only specifies 'rejecting'" do
+ setup do
+ @dummy_class.validates_attachment_content_type :avatar, :content_type => %r{audio/.*}
+ @matcher = self.class.validate_attachment_content_type(:avatar).
+ rejecting(%w(audio/mp3 application/octet-stream))
+ end
+
+ should_reject_dummy_class
+ end
end
end
@dolzenko

@allowed_types.present is obviously typo, right? surprised it's not spotted by anyone yet

@sikachu

Right ... I think we should fix that.

@sikachu

No worries, it's fixed in 9f38928. Thanks for reporting in :)

@dolzenko

Fastest gun in the West, or should I say East? So many thanks! :)

Please sign in to comment.
Something went wrong with that request. Please try again.