Commit 8b5289b6 by Wojciech Wnętrzak Committed by Jon Yurek

Validate media type spoofing only when attachment changed.

It resulted with downloading attachment (i.e. from S3) on each
instance validation even when attachment didn't change at all.
parent b45eac22
...@@ -79,7 +79,8 @@ module Paperclip ...@@ -79,7 +79,8 @@ module Paperclip
end end
def add_required_validations def add_required_validations
@klass.validates_media_type_spoof_detection @name name = @name
@klass.validates_media_type_spoof_detection name, :if => ->{ send(name).dirty? }
end end
def add_active_record_callbacks def add_active_record_callbacks
......
...@@ -18,8 +18,6 @@ class MediaTypeSpoofDetectionValidatorTest < Test::Unit::TestCase ...@@ -18,8 +18,6 @@ class MediaTypeSpoofDetectionValidatorTest < Test::Unit::TestCase
should "return default error message for spoofed media type" do should "return default error message for spoofed media type" do
build_validator build_validator
# Make avatar dirty
file = File.new(fixture_file("5k.png"), "rb") file = File.new(fixture_file("5k.png"), "rb")
@dummy.avatar.assign(file) @dummy.avatar.assign(file)
...@@ -29,4 +27,21 @@ class MediaTypeSpoofDetectionValidatorTest < Test::Unit::TestCase ...@@ -29,4 +27,21 @@ class MediaTypeSpoofDetectionValidatorTest < Test::Unit::TestCase
assert_equal "has an extension that does not match its contents", @dummy.errors[:avatar].first assert_equal "has an extension that does not match its contents", @dummy.errors[:avatar].first
end end
should "run when attachment is dirty" do
build_validator
file = File.new(fixture_file("5k.png"), "rb")
@dummy.avatar.assign(file)
Paperclip::MediaTypeSpoofDetector.stubs(:using).returns(stub(:spoofed? => false))
@dummy.valid?
assert_received(Paperclip::MediaTypeSpoofDetector, :using){|e| e.once }
end
should "not run when attachment is not dirty" do
Paperclip::MediaTypeSpoofDetector.stubs(:using).never
@dummy.valid?
assert_received(Paperclip::MediaTypeSpoofDetector, :using){|e| e.never }
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