Commit 19a3bfc6 by Dan Collis-Puro Committed by Jon Yurek

A missing content_type validator shows deprecation warning

parent 066a139e
# encoding: utf-8 # encoding: utf-8
require 'uri' require 'uri'
require 'paperclip/url_generator' require 'paperclip/url_generator'
require 'active_support/deprecation'
module Paperclip module Paperclip
# The Attachment class manages the files for a given attachment. It saves # The Attachment class manages the files for a given attachment. It saves
...@@ -93,6 +94,7 @@ module Paperclip ...@@ -93,6 +94,7 @@ module Paperclip
# new_user.avatar = old_user.avatar # new_user.avatar = old_user.avatar
def assign uploaded_file def assign uploaded_file
ensure_required_accessors! ensure_required_accessors!
ensure_required_validations!
file = Paperclip.io_adapters.for(uploaded_file) file = Paperclip.io_adapters.for(uploaded_file)
return nil if not file.assignment? return nil if not file.assignment?
...@@ -385,6 +387,19 @@ module Paperclip ...@@ -385,6 +387,19 @@ module Paperclip
@options[:path].respond_to?(:call) ? @options[:path].call(self) : @options[:path] @options[:path].respond_to?(:call) ? @options[:path].call(self) : @options[:path]
end end
def active_validator_classes
@instance.class.validators.map(&:class)
end
def ensure_required_validations!
if ! active_validator_classes.include?(Paperclip::Validators::AttachmentContentTypeValidator)
ActiveSupport::Deprecation.warn(
"You must define a content_type validator to ensure you only accept files of the correct type. Failure to do so will raise an error in Paperclip versions >= 4.0"
)
# raise Paperclip::Errors::NoContentTypeValidator.new('you must define a content type validation')
end
end
def ensure_required_accessors! #:nodoc: def ensure_required_accessors! #:nodoc:
%w(file_name).each do |field| %w(file_name).each do |field|
unless @instance.respond_to?("#{name}_#{field}") && @instance.respond_to?("#{name}_#{field}=") unless @instance.respond_to?("#{name}_#{field}") && @instance.respond_to?("#{name}_#{field}=")
......
...@@ -13,6 +13,10 @@ module Paperclip ...@@ -13,6 +13,10 @@ module Paperclip
class CommandNotFoundError < Paperclip::Error class CommandNotFoundError < Paperclip::Error
end end
# Thrown when no content_type validation exists
# class NoContentTypeValidator < Paperclip::Error
# end
# Will be thrown when ImageMagic cannot determine the uploaded file's # Will be thrown when ImageMagic cannot determine the uploaded file's
# metadata, usually this would mean the file is not an image. # metadata, usually this would mean the file is not an image.
class NotIdentifiedByImageMagickError < Paperclip::Error class NotIdentifiedByImageMagickError < Paperclip::Error
......
...@@ -40,6 +40,7 @@ class Test::Unit::TestCase ...@@ -40,6 +40,7 @@ class Test::Unit::TestCase
Rails.stubs(:root).returns(Pathname.new(ROOT).join('tmp')) Rails.stubs(:root).returns(Pathname.new(ROOT).join('tmp'))
Rails.stubs(:env).returns('test') Rails.stubs(:env).returns('test')
Rails.stubs(:const_defined?).with(:Railtie).returns(false) Rails.stubs(:const_defined?).with(:Railtie).returns(false)
ActiveSupport::Deprecation.silenced = true
end end
end end
......
...@@ -15,6 +15,9 @@ class SchemaTest < Test::Unit::TestCase ...@@ -15,6 +15,9 @@ class SchemaTest < Test::Unit::TestCase
context "within table definition" do context "within table definition" do
context "using #has_attached_file" do context "using #has_attached_file" do
setup do
ActiveSupport::Deprecation.silenced = false
end
should "create attachment columns" do should "create attachment columns" do
Dummy.connection.create_table :dummies, :force => true do |t| Dummy.connection.create_table :dummies, :force => true do |t|
ActiveSupport::Deprecation.silence do ActiveSupport::Deprecation.silence do
...@@ -122,6 +125,9 @@ class SchemaTest < Test::Unit::TestCase ...@@ -122,6 +125,9 @@ class SchemaTest < Test::Unit::TestCase
end end
context "using #drop_attached_file" do context "using #drop_attached_file" do
setup do
ActiveSupport::Deprecation.silenced = false
end
should "remove the attachment columns" do should "remove the attachment columns" do
ActiveSupport::Deprecation.silence do ActiveSupport::Deprecation.silence do
Dummy.connection.drop_attached_file :dummies, :avatar Dummy.connection.drop_attached_file :dummies, :avatar
......
require './test/helper' require './test/helper'
class ValidatorsTest < Test::Unit::TestCase class ValidatorsTest < Test::Unit::TestCase
include ActiveSupport::Testing::Deprecation
def setup def setup
rebuild_model rebuild_model
end end
...@@ -58,4 +60,41 @@ class ValidatorsTest < Test::Unit::TestCase ...@@ -58,4 +60,41 @@ class ValidatorsTest < Test::Unit::TestCase
assert_equal [], dummy.errors.keys assert_equal [], dummy.errors.keys
end end
end end
context 'when no content_type validation exists' do
setup do
ActiveSupport::Deprecation.silenced = false
end
should 'emit a deprecation warning' do
assert_deprecated do
Dummy.new(:avatar => File.new(fixture_file("12k.png")))
end
end
# should 'raise an error' do
# assert_raises(Paperclip::Errors::NoContentTypeValidator) do
# Dummy.new(:avatar => File.new(fixture_file("12k.png")))
# end
# end
end
context 'when a content_type validation exists' do
setup do
Dummy.validates_attachment :avatar, :content_type => { :content_type => "image/jpeg" }
ActiveSupport::Deprecation.silenced = false
end
should 'not emit a deprecation warning' do
assert_not_deprecated do
Dummy.new(:avatar => File.new(fixture_file("12k.png")))
end
end
# should 'not raise an error' do
# assert_nothing_raised(Paperclip::Errors::NoContentTypeValidator) do
# Dummy.new(:avatar => File.new(fixture_file("12k.png")))
# 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