Commit c4c22f8a by tony-brewerio Committed by Anton Ustyuzhanin

Current validation check in AttachmentContentTypeValidator is simply wrong, it…

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.
parent 8390516f
...@@ -6,13 +6,11 @@ module Paperclip ...@@ -6,13 +6,11 @@ module Paperclip
value = record.send(:read_attribute_for_validation, attribute) value = record.send(:read_attribute_for_validation, attribute)
allowed_types = [options[:content_type]].flatten allowed_types = [options[:content_type]].flatten
unless value.blank? if value.present?
allowed_types.any? do |type| unless allowed_types.any? { |type| type === value }
unless type === value record.errors.add(attribute, :invalid, options.merge(
record.errors.add(attribute, :invalid, options.merge( :types => allowed_types.join(', ')
:types => allowed_types.join(', ') ))
))
end
end end
end end
end end
......
...@@ -48,6 +48,18 @@ class AttachmentContentTypeValidatorTest < Test::Unit::TestCase ...@@ -48,6 +48,18 @@ class AttachmentContentTypeValidatorTest < Test::Unit::TestCase
assert @dummy.errors[:avatar_content_type].blank? assert @dummy.errors[:avatar_content_type].blank?
end end
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 end
context "with a disallowed type" do context "with a disallowed type" do
......
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