Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Current validation check in AttachmentContentTypeValidator is simply …
…wrong, it will add an error if any of the allowed_types fails comparison with value, so it will pass only if value is either empty or equals to each and every one of allowed_types.

Add test to check that validation passes if even one of content types match.
  • Loading branch information
tony-brewerio committed Mar 27, 2012
1 parent 8390516 commit c4c22f8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
12 changes: 5 additions & 7 deletions lib/paperclip/validators/attachment_content_type_validator.rb
Expand Up @@ -6,13 +6,11 @@ def validate_each(record, attribute, value)
value = record.send(:read_attribute_for_validation, attribute)
allowed_types = [options[:content_type]].flatten

unless value.blank?
allowed_types.any? do |type|
unless type === value
record.errors.add(attribute, :invalid, options.merge(
:types => allowed_types.join(', ')
))
end
if value.present?
unless allowed_types.any? { |type| type === value }
record.errors.add(attribute, :invalid, options.merge(
:types => allowed_types.join(', ')
))
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions test/validators/attachment_content_type_validator_test.rb
Expand Up @@ -48,6 +48,18 @@ def build_validator(options)
assert @dummy.errors[:avatar_content_type].blank?
end
end

context "as a list" do
setup do
build_validator :content_type => ["image/png", "image/jpg", "image/jpeg"]
@dummy.stubs(:avatar_content_type => "image/jpg")
@validator.validate(@dummy)
end

should "not set an error message" do
assert @dummy.errors[:avatar_content_type].blank?
end
end
end

context "with a disallowed type" do
Expand Down

0 comments on commit c4c22f8

Please sign in to comment.