Permalink
Browse files

Only cache a single instance of uploaded attachments.

Prior to this fix, uploading an attachment (to a publication, for
example) would result in the uploaded file being cached twice (in
./public/uploads/tmp/...). This is because the file is cached when the
attachment model is instantiated and we were inadvertently instantiating
it twice: once in DocumentsController#build_document and then again in
DocumentsController::NationalApplicability#create. I added the failing
test and then fixed the double instantiation by overriding the
`#build_document` before filter so that it didn't fire for the `#create`
action.
  • Loading branch information...
1 parent 114990f commit 7745ccf6f10b3d4902006701d32891194b03d8f5 @chrisroos-and-lazyatom chrisroos-and-lazyatom committed Feb 1, 2012
@@ -2,6 +2,8 @@ module Admin::DocumentsController::NationalApplicability
extend ActiveSupport::Concern
included do
+ before_filter :build_document, only: [:new]
+
before_filter :build_nation_inapplicabilities, only: [:new, :edit]
end
@@ -207,6 +207,18 @@ def should_allow_attachments_for(document_type)
assert_equal greenpaper_pdf.size, attachment.file_size
end
+ test "creating a document should result in a single instance of the uploaded file being cached" do
+ greenpaper_pdf = fixture_file_upload('greenpaper.pdf', 'application/pdf')
+ attributes = controller_attributes_for(document_type)
+ attributes[:document_attachments_attributes] = {
+ "0" => { attachment_attributes: attributes_for(:attachment, title: "attachment-title", file: greenpaper_pdf) }
+ }
+
+ Attachment.any_instance.expects(:file=).once
+
+ post :create, document: attributes
+ end
+
test "creating a document with invalid data should still show attachment fields" do
post :create, document: controller_attributes_for(document_type, title: "")

0 comments on commit 7745ccf

Please sign in to comment.