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' ...@@ -37,9 +37,6 @@ require 'paperclip/attachment'
module Paperclip module Paperclip
VERSION = "2.1.0" 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 class << self
# Provides configurability to Paperclip. There are a number of options available, such as: # Provides configurability to Paperclip. There are a number of options available, such as:
...@@ -180,15 +177,18 @@ module Paperclip ...@@ -180,15 +177,18 @@ module Paperclip
# Places ActiveRecord-style validations on the content type of the file assigned. The # Places ActiveRecord-style validations on the content type of the file assigned. The
# possible options are: # 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. # * +message+: The message to display when the uploaded file has an invalid content type.
def validates_attachment_content_type name, options = {} def validates_attachment_content_type name, options = {}
attachment_definitions[name][:validations] << lambda do |attachment, instance| 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.nil?
unless attachment.original_filename.blank? || options[:content_type].include?(instance[:"#{name}_content_type"]) unless options[:content_type].blank?
options[:message] || ActiveRecord::Errors.default_error_messages[:inclusion] 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
end end
......
...@@ -64,16 +64,13 @@ class PaperclipTest < Test::Unit::TestCase ...@@ -64,16 +64,13 @@ class PaperclipTest < Test::Unit::TestCase
assert Dummy.new.respond_to?(:avatar=) assert Dummy.new.respond_to?(:avatar=)
end end
[[:presence, nil, "5k.png", nil], [[:presence, nil, "5k.png", nil],
[:size, {:in => 1..10240}, "5k.png", "12k.png"], [:size, {:in => 1..10240}, "5k.png", "12k.png"],
[:content_type1,{:content_type => "image/png"}, "5k.png", "text.txt"], [:size2, {:in => 1..10240}, nil, "12k.png"],
[:content_type2, {:content_type => :image}, "5k.png", "text.txt"], [:content_type1, {:content_type => "image/png"}, "5k.png", "text.txt"],
[:content_type3, {:content_type => "text/plain"}, "text.txt", "5k.png"], [:content_type2, {:content_type => "text/plain"}, "text.txt", "5k.png"],
[:presence2, {:message => "error"}, "5k.png", nil, "error"], [:content_type3, {:content_type => %r{image/.*}}, "5k.png", "text.txt"],
[: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."], [:content_type4, {:content_type => "image/png"}, nil, "text.txt"]].each do |args|
[: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|
context "with #{args[0]} validations" do context "with #{args[0]} validations" do
setup do setup do
Dummy.class_eval do Dummy.class_eval do
...@@ -106,20 +103,20 @@ class PaperclipTest < Test::Unit::TestCase ...@@ -106,20 +103,20 @@ class PaperclipTest < Test::Unit::TestCase
end end
end end
context "and an invalid file with :message" do # context "and an invalid file with :message" do
setup do # setup do
@file = args[3] && File.new(File.join(FIXTURES_DIR, args[3])) # @file = args[3] && File.new(File.join(FIXTURES_DIR, args[3]))
end # end
#
should "have errors" do # should "have errors" do
if args[1] && args[1][:message] && args[4] # if args[1] && args[1][:message] && args[4]
@dummy.avatar = @file # @dummy.avatar = @file
assert ! @dummy.avatar.valid? # assert ! @dummy.avatar.valid?
assert_equal 1, @dummy.avatar.errors.length # assert_equal 1, @dummy.avatar.errors.length
assert_equal args[4], @dummy.avatar.errors[0] # assert_equal args[4], @dummy.avatar.errors[0]
end # end
end # end
end # end
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