Commit 491e9f59 by Henrik N

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

parent cae3a9f5
...@@ -50,7 +50,13 @@ module Paperclip ...@@ -50,7 +50,13 @@ module Paperclip
# What gets called when you call instance.attachment = File. It clears errors, # What gets called when you call instance.attachment = File. It clears errors,
# assigns attributes, processes the file, and runs validations. It also queues up # 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. # 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 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) return nil unless valid_assignment?(uploaded_file)
logger.info("[paperclip] Assigning #{uploaded_file} to #{name}") logger.info("[paperclip] Assigning #{uploaded_file} to #{name}")
......
...@@ -198,6 +198,21 @@ class IntegrationTest < Test::Unit::TestCase ...@@ -198,6 +198,21 @@ class IntegrationTest < Test::Unit::TestCase
@dummy.reload @dummy.reload
assert_equal "5k.png", @dummy.avatar_file_name assert_equal "5k.png", @dummy.avatar_file_name
end 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 end
if ENV['S3_TEST_BUCKET'] if ENV['S3_TEST_BUCKET']
......
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