From 4b746b39fc7565e92426f42ac7840aa3ca969a2a Mon Sep 17 00:00:00 2001 From: Joern Barthel Date: Fri, 1 Apr 2011 15:42:10 +0200 Subject: [PATCH 1/2] Added uploaded_file instance variable for new assigns --- lib/paperclip/attachment.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/paperclip/attachment.rb b/lib/paperclip/attachment.rb index 902d15149..1852a6401 100644 --- a/lib/paperclip/attachment.rb +++ b/lib/paperclip/attachment.rb @@ -89,6 +89,8 @@ def assign uploaded_file if uploaded_file.is_a?(Paperclip::Attachment) uploaded_file = uploaded_file.to_file(:original) close_uploaded_file = uploaded_file.respond_to?(:close) + else + instance_write(:uploaded_file, uploaded_file) end return nil unless valid_assignment?(uploaded_file) @@ -175,12 +177,17 @@ def destroy save end + # Returns the uploaded file if present. + def uploaded_file + instance_read(:uploaded_file) + end + # Returns the name of the file as originally assigned, and lives in the # _file_name attribute of the model. def original_filename instance_read(:file_name) end - + # Returns the size of the file as originally assigned, and lives in the # _file_size attribute of the model. def size From 58a17e64240e730871249c513a8f0291a12e124a Mon Sep 17 00:00:00 2001 From: Overbryd Date: Tue, 18 Oct 2011 13:49:24 +0200 Subject: [PATCH 2/2] Added a test for Attachment#uploaded_file --- lib/paperclip/attachment.rb | 4 ++-- test/attachment_test.rb | 4 ++++ test/helper.rb | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/paperclip/attachment.rb b/lib/paperclip/attachment.rb index c64dc2666..b6e1cdcda 100644 --- a/lib/paperclip/attachment.rb +++ b/lib/paperclip/attachment.rb @@ -95,7 +95,7 @@ def assign uploaded_file uploaded_file = uploaded_file.to_file(:original) close_uploaded_file = uploaded_file.respond_to?(:close) else - instance_write(:uploaded_file, uploaded_file) + instance_write(:uploaded_file, uploaded_file) if uploaded_file end return nil unless valid_assignment?(uploaded_file) @@ -208,7 +208,7 @@ def uploaded_file def original_filename instance_read(:file_name) end - + # Returns the size of the file as originally assigned, and lives in the # _file_size attribute of the model. def size diff --git a/test/attachment_test.rb b/test/attachment_test.rb index d85051661..f64f75f4f 100644 --- a/test/attachment_test.rb +++ b/test/attachment_test.rb @@ -825,6 +825,10 @@ def do_after_all; end assert @attachment.dirty? end + should "set uploaded_file for access beyond the paperclip lifecycle" do + assert_equal @file, @attachment.uploaded_file + end + context "and saved" do setup do @attachment.save diff --git a/test/helper.rb b/test/helper.rb index 91f39488e..ad86b0421 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -1,5 +1,6 @@ require 'rubygems' require 'tempfile' +require 'pathname' require 'test/unit' require 'shoulda'