Commit 69525425 by Jon Yurek

Merged content_type validation changes from austin.bain.

Changed keyword-collection mimetype matching to regexp matching.
parent be890c98
......@@ -37,9 +37,6 @@ require 'paperclip/attachment'
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:
......@@ -180,15 +177,18 @@ 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"])
options[:message] || ActiveRecord::Errors.default_error_messages[:inclusion]
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
......
......@@ -64,16 +64,13 @@ class PaperclipTest < Test::Unit::TestCase
assert Dummy.new.respond_to?(:avatar=)
end
[[: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|
[[:presence, nil, "5k.png", nil],
[:size, {:in => 1..10240}, "5k.png", "12k.png"],
[: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