Permalink
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...
1 parent 8390516 commit c4c22f8aab1d809a46bc97dcb32bb9f8586922a7 @tony-brewerio tony-brewerio committed with tony-brewerio Mar 27, 2012
@@ -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
@@ -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

0 comments on commit c4c22f8

Please sign in to comment.