Skip to content

Commit

Permalink
Only cache a single instance of uploaded attachments.
Browse files Browse the repository at this point in the history
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
chrisroos-and-lazyatom committed Feb 1, 2012
1 parent 114990f commit 7745ccf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions test/support/admin_document_controller_test_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: "")

Expand Down

0 comments on commit 7745ccf

Please sign in to comment.