Permalink
Browse files

Don't process if the attachment isn't valid.

  • Loading branch information...
1 parent 3f4180e commit 1544a7b2f054b53a52aa53a12f471d1a1bb36ca6 @jyurek jyurek committed Nov 30, 2012
Showing with 38 additions and 3 deletions.
  1. +9 −3 lib/paperclip/attachment.rb
  2. +29 −0 test/attachment_processing_test.rb
@@ -105,7 +105,7 @@ def assign uploaded_file
@dirty = true
- post_process(*only_process) if post_processing
+ post_process(*only_process) if post_processing && valid_assignment?
# Reset the file size if the original file was reprocessed.
instance_write(:file_size, @queued_for_write[:original].size)
@@ -369,8 +369,14 @@ def log message #:nodoc:
Paperclip.log(message)
end
- def valid_assignment? file #:nodoc:
- file.nil? || (file.respond_to?(:original_filename) && file.respond_to?(:content_type))
+ def valid_assignment? #:nodoc:
+ if instance.valid?
+ true
+ else
+ instance.errors.none? do |attr, message|
+ attr.to_s.start_with?(@name.to_s)
+ end
+ end
end
def initialize_storage #:nodoc:
@@ -0,0 +1,29 @@
+# encoding: utf-8
+require './test/helper'
+require 'paperclip/attachment'
+
+class AttachmentProcessingTest < Test::Unit::TestCase
+ def setup
+ rebuild_model
+ end
+
+ should 'process attachments given a valid assignment' do
+ file = File.new(fixture_file("5k.png"))
+ Dummy.validates_attachment_content_type :avatar, :content_type => "image/png"
+ instance = Dummy.new
+ attachment = instance.avatar
+ attachment.expects(:post_process)
+
+ attachment.assign(file)
+ end
+
+ should 'not process attachments if the assignment does not pass validation' do
+ file = File.new(fixture_file("5k.png"))
+ Dummy.validates_attachment_content_type :avatar, :content_type => "image/tiff"
+ instance = Dummy.new
+ attachment = instance.avatar
+ attachment.expects(:post_process).never
+
+ attachment.assign(file)
+ end
+end

0 comments on commit 1544a7b

Please sign in to comment.