Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Allow assigning another paperclip: new_user.avatar = old_user.avatar.
Browse files Browse the repository at this point in the history
  • Loading branch information
henrik committed Aug 1, 2008
1 parent cae3a9f commit 491e9f5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/paperclip/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down
15 changes: 15 additions & 0 deletions test/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down

0 comments on commit 491e9f5

Please sign in to comment.