Skip to content

Commit

Permalink
Add ability to specify ".allowing" and ".rejecting" individually with…
Browse files Browse the repository at this point in the history
… content_type validation matcher.

Closes thoughtbot#472, Closes thoughtbot#532
  • Loading branch information
cgs authored and sikachu committed Jul 22, 2011
1 parent 45334fb commit 0206259
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
34 changes: 20 additions & 14 deletions lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
Expand Up @@ -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
Expand All @@ -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?
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
Expand All @@ -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
Expand Down
40 changes: 40 additions & 0 deletions test/matchers/validate_attachment_content_type_matcher_test.rb
Expand Up @@ -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

0 comments on commit 0206259

Please sign in to comment.