Commit 02062592 by Chris Sepic Committed by Prem Sichanugrist

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

Add ability to specify ".allowing" and ".rejecting" individually with content_type validation matcher.

Closes #472, Closes #532
parent 45334fb0
......@@ -17,6 +17,8 @@ module Paperclip
class ValidateAttachmentContentTypeMatcher
def initialize attachment_name
@attachment_name = attachment_name
@allowed_types = []
@rejected_types = []
end
def allowing *types
......@@ -37,13 +39,19 @@ module Paperclip
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?
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 @@ module Paperclip
protected
def allow_types?(types)
types.all? do |type|
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
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
......
......@@ -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
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