Commit 9fb92553 by Nathan Hyde Committed by Jon Yurek

Don't assign :fingerprint if the value can't be stored (both before and after save).

#fingerprint now consistently returns nil before and after saving an attachment if the value can't be written to the db.
parent 4e076813
...@@ -111,7 +111,7 @@ module Paperclip ...@@ -111,7 +111,7 @@ module Paperclip
instance_write(:file_name, uploaded_filename.strip) instance_write(:file_name, uploaded_filename.strip)
instance_write(:content_type, uploaded_file.content_type.to_s.strip) instance_write(:content_type, uploaded_file.content_type.to_s.strip)
instance_write(:file_size, uploaded_file.size.to_i) 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) instance_write(:updated_at, Time.now)
@dirty = true @dirty = true
...@@ -254,7 +254,13 @@ module Paperclip ...@@ -254,7 +254,13 @@ module Paperclip
# Returns the hash of the file as originally assigned, and lives in the # Returns the hash of the file as originally assigned, and lives in the
# <attachment>_fingerprint attribute of the model. # <attachment>_fingerprint attribute of the model.
def fingerprint 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 end
# Returns the content_type of the file as originally assigned, and lives # Returns the content_type of the file as originally assigned, and lives
......
...@@ -998,13 +998,17 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -998,13 +998,17 @@ class AttachmentTest < Test::Unit::TestCase
end end
end end
should "not calculate fingerprint without fingerprint column" do should "not calculate fingerprint after save" do
assert_equal false, @dummy.respond_to?(:avatar_fingerprint)
@dummy.avatar = @file @dummy.avatar = @file
@dummy.save @dummy.save
assert_nil @dummy.avatar.fingerprint assert_nil @dummy.avatar.fingerprint
end end
should "not calculate fingerprint before saving" do
@dummy.avatar = @file
assert_nil @dummy.avatar.fingerprint
end
context "and avatar_content_type column" do context "and avatar_content_type column" do
setup do setup do
ActiveRecord::Base.connection.add_column :dummies, :avatar_content_type, :string ActiveRecord::Base.connection.add_column :dummies, :avatar_content_type, :string
......
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