Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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...
commit c4c22f8aab1d809a46bc97dcb32bb9f8586922a7 1 parent 8390516
@tony-brewerio tony-brewerio authored
View
12 lib/paperclip/validators/attachment_content_type_validator.rb
@@ -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
View
12 test/validators/attachment_content_type_validator_test.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.