Permalink
Browse files

Don't assign :fingerprint if the value can't be stored (both before a…

…nd after save).

#fingerprint now consistently returns nil before and after saving an attachment if the value can't be written to the db.
  • Loading branch information...
Nathan Hyde authored and jyurek committed Jan 5, 2012
1 parent 4e07681 commit 9fb92553685ddbd4317e0423ddb9216deeeb1118
Showing with 15 additions and 5 deletions.
  1. +9 −3 lib/paperclip/attachment.rb
  2. +6 −2 test/attachment_test.rb
@@ -106,12 +106,12 @@ def assign uploaded_file
return nil if uploaded_file.nil?
uploaded_filename ||= uploaded_file.original_filename
- stores_fingerprint = @instance.respond_to?("#{name}_fingerprint".to_sym)
+ stores_fingerprint = @instance.respond_to?("#{name}_fingerprint".to_sym)
@queued_for_write[:original] = to_tempfile(uploaded_file)
instance_write(:file_name, uploaded_filename.strip)
instance_write(:content_type, uploaded_file.content_type.to_s.strip)
instance_write(:file_size, uploaded_file.size.to_i)
- instance_write(:fingerprint, stores_fingerprint ? generate_fingerprint(uploaded_file) : false)
+ instance_write(:fingerprint, generate_fingerprint(uploaded_file)) if stores_fingerprint
instance_write(:updated_at, Time.now)
@dirty = true
@@ -254,7 +254,13 @@ def size
# Returns the hash of the file as originally assigned, and lives in the
# <attachment>_fingerprint attribute of the model.
def fingerprint
- instance_read(:fingerprint) || (@queued_for_write[:original] && generate_fingerprint(@queued_for_write[:original]))
+ if instance_read(:fingerprint)
+ instance_read(:fingerprint)
+ elsif @instance.respond_to?("#{name}_fingerprint".to_sym)
+ @queued_for_write[:original] && generate_fingerprint(@queued_for_write[:original])
+ else
+ nil
+ end
end
# Returns the content_type of the file as originally assigned, and lives
View
@@ -998,13 +998,17 @@ def do_after_all; end
end
end
- should "not calculate fingerprint without fingerprint column" do
- assert_equal false, @dummy.respond_to?(:avatar_fingerprint)
+ should "not calculate fingerprint after save" do
@dummy.avatar = @file
@dummy.save
assert_nil @dummy.avatar.fingerprint
end
+ should "not calculate fingerprint before saving" do
+ @dummy.avatar = @file
+ assert_nil @dummy.avatar.fingerprint
+ end
+
context "and avatar_content_type column" do
setup do
ActiveRecord::Base.connection.add_column :dummies, :avatar_content_type, :string

0 comments on commit 9fb9255

Please sign in to comment.