Commit bd528ba7 by Prem Sichanugrist

Merge pull request #792 from tony-brewerio/patch-1

Fix AttachmentContentTypeValidator to work with multiple values
parents 8390516f c4c22f8a
...@@ -6,16 +6,14 @@ module Paperclip ...@@ -6,16 +6,14 @@ 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
def check_validity! def check_validity!
unless options.has_key?(:content_type) unless options.has_key?(:content_type)
......
...@@ -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