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 ...@@ -17,6 +17,8 @@ module Paperclip
class ValidateAttachmentContentTypeMatcher class ValidateAttachmentContentTypeMatcher
def initialize attachment_name def initialize attachment_name
@attachment_name = attachment_name @attachment_name = attachment_name
@allowed_types = []
@rejected_types = []
end end
def allowing *types def allowing *types
...@@ -37,13 +39,19 @@ module Paperclip ...@@ -37,13 +39,19 @@ module Paperclip
end end
def failure_message def failure_message
"Content types #{@allowed_types.join(", ")} should be accepted" + "".tap do |str|
" and #{@rejected_types.join(", ")} rejected by #{@attachment_name}" 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 end
def negative_failure_message def negative_failure_message
"Content types #{@allowed_types.join(", ")} should be rejected" + "".tap do |str|
" and #{@rejected_types.join(", ")} accepted by #{@attachment_name}" 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 end
def description def description
...@@ -52,22 +60,20 @@ module Paperclip ...@@ -52,22 +60,20 @@ module Paperclip
protected protected
def allow_types?(types) def type_allowed?(type)
types.all? do |type|
file = StringIO.new(".") file = StringIO.new(".")
file.content_type = type file.content_type = type
(subject = @subject.new).attachment_for(@attachment_name).assign(file) (subject = @subject.new).attachment_for(@attachment_name).assign(file)
subject.valid? subject.valid?
subject.errors[:"#{@attachment_name}_content_type"].blank? subject.errors[:"#{@attachment_name}_content_type"].blank?
end end
end
def allowed_types_allowed? def allowed_types_allowed?
allow_types?(@allowed_types) @allowed_types.all? { |type| type_allowed?(type) }
end end
def rejected_types_rejected? def rejected_types_rejected?
not allow_types?(@rejected_types) !@rejected_types.any? { |type| type_allowed?(type) }
end end
end end
end end
......
...@@ -43,5 +43,45 @@ class ValidateAttachmentContentTypeMatcherTest < Test::Unit::TestCase ...@@ -43,5 +43,45 @@ class ValidateAttachmentContentTypeMatcherTest < Test::Unit::TestCase
should_accept_dummy_class should_accept_dummy_class
end 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
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