Commit 69525425 by Jon Yurek

Merged content_type validation changes from austin.bain.

Changed keyword-collection mimetype matching to regexp matching.
parent be890c98
......@@ -38,9 +38,6 @@ module Paperclip
VERSION = "2.1.0"
@@content_types = ['image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png', 'image/jpg']
mattr_reader :content_types
class << self
# Provides configurability to Paperclip. There are a number of options available, such as:
# * whiny_thumbnails: Will raise an error if Paperclip cannot process thumbnails of
......@@ -180,19 +177,22 @@ module Paperclip
# Places ActiveRecord-style validations on the content type of the file assigned. The
# possible options are:
# * +content_type+: Allowed content types. Can be a single content type or an array. Allows all by default. Use :image to allow all standard image types.
# * +content_type+: Allowed content types. Can be a single content type or an array. Allows all by default.
# * +message+: The message to display when the uploaded file has an invalid content type.
def validates_attachment_content_type name, options = {}
attachment_definitions[name][:validations] << lambda do |attachment, instance|
options[:content_type] = [options[:content_type]].flatten.collect! { |t| t == :image ? Paperclip.content_types : t }.flatten unless options[:content_type].nil?
valid_types = [options[:content_type]].flatten
unless options[:content_type].empty?
unless attachment.original_filename.blank? || options[:content_type].include?(instance[:"#{name}_content_type"])
unless attachment.original_filename.nil?
unless options[:content_type].blank?
content_type = instance[:"#{name}_content_type"]
unless valid_types.any?{|t| t === content_type }
options[:message] || ActiveRecord::Errors.default_error_messages[:inclusion]
end
end
end
end
end
# Returns the attachment definitions defined by each call to has_attached_file.
def attachment_definitions
......
......@@ -66,14 +66,11 @@ class PaperclipTest < Test::Unit::TestCase
[[:presence, nil, "5k.png", nil],
[:size, {:in => 1..10240}, "5k.png", "12k.png"],
[:content_type1,{:content_type => "image/png"}, "5k.png", "text.txt"],
[:content_type2, {:content_type => :image}, "5k.png", "text.txt"],
[:content_type3, {:content_type => "text/plain"}, "text.txt", "5k.png"],
[:presence2, {:message => "error"}, "5k.png", nil, "error"],
[:size2, {:in => 1..10240, :message => 'size is not between #{min} and #{max} bytes.'}, "5k.png", "12k.png", "size is not between 1 and 10240 bytes."],
[:size3, {:in => 1..10240}, nil, "12k.png"],
[:content_type4,{:content_type => "image/png", :message => "error"}, "5k.png", "text.txt", "error"],
[:content_type5,{:content_type => "image/png"}, nil, "text.txt"]].each do |args|
[:size2, {:in => 1..10240}, nil, "12k.png"],
[:content_type1, {:content_type => "image/png"}, "5k.png", "text.txt"],
[:content_type2, {:content_type => "text/plain"}, "text.txt", "5k.png"],
[:content_type3, {:content_type => %r{image/.*}}, "5k.png", "text.txt"],
[:content_type4, {:content_type => "image/png"}, nil, "text.txt"]].each do |args|
context "with #{args[0]} validations" do
setup do
Dummy.class_eval do
......@@ -106,20 +103,20 @@ class PaperclipTest < Test::Unit::TestCase
end
end
context "and an invalid file with :message" do
setup do
@file = args[3] && File.new(File.join(FIXTURES_DIR, args[3]))
end
should "have errors" do
if args[1] && args[1][:message] && args[4]
@dummy.avatar = @file
assert ! @dummy.avatar.valid?
assert_equal 1, @dummy.avatar.errors.length
assert_equal args[4], @dummy.avatar.errors[0]
end
end
end
# context "and an invalid file with :message" do
# setup do
# @file = args[3] && File.new(File.join(FIXTURES_DIR, args[3]))
# end
#
# should "have errors" do
# if args[1] && args[1][:message] && args[4]
# @dummy.avatar = @file
# assert ! @dummy.avatar.valid?
# assert_equal 1, @dummy.avatar.errors.length
# assert_equal args[4], @dummy.avatar.errors[0]
# end
# end
# end
end
end
end
......
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