Skip to content
Browse files

Allow assigning another paperclip: new_user.avatar = old_user.avatar.

  • Loading branch information...
1 parent cae3a9f commit 491e9f59d6aca9f73091e08298dc9654f176f7ba @henrik henrik committed
Showing with 21 additions and 0 deletions.
  1. +6 −0 lib/paperclip/attachment.rb
  2. +15 −0 test/integration_test.rb
View
6 lib/paperclip/attachment.rb
@@ -50,7 +50,13 @@ def initialize name, instance, options = {}
# What gets called when you call instance.attachment = File. It clears errors,
# assigns attributes, processes the file, and runs validations. It also queues up
# the previous file for deletion, to be flushed away on #save of its host.
+ # In addition to form uploads, you can also assign another Paperclip attachment:
+ # new_user.avatar = old_user.avatar
def assign uploaded_file
+ if uploaded_file.is_a?(Paperclip::Attachment)
+ uploaded_file = uploaded_file.to_file(:original)
+ end
+
return nil unless valid_assignment?(uploaded_file)
logger.info("[paperclip] Assigning #{uploaded_file} to #{name}")
View
15 test/integration_test.rb
@@ -198,6 +198,21 @@ class IntegrationTest < Test::Unit::TestCase
@dummy.reload
assert_equal "5k.png", @dummy.avatar_file_name
end
+
+ should "assign images from other Paperclips as well" do
+ @dummy2 = Dummy.new
+ @file2 = File.new(File.join(FIXTURES_DIR, "12k.png"))
+ assert @dummy2.avatar = @file2
+ @dummy2.save
+
+ assert_not_equal `identify -format "%wx%h" #{@dummy.avatar.to_file(:original).path}`,
+ `identify -format "%wx%h" #{@dummy2.avatar.to_file(:original).path}`
+
+ assert @dummy.avatar = @dummy2.avatar
+ @dummy.save
+ assert_equal `identify -format "%wx%h" #{@dummy.avatar.to_file(:original).path}`,
+ `identify -format "%wx%h" #{@dummy2.avatar.to_file(:original).path}`
+ end
end
if ENV['S3_TEST_BUCKET']

0 comments on commit 491e9f5

Please sign in to comment.
Something went wrong with that request. Please try again.