From c56c0f411c577fe17b64fe33875cad5d75e34284 2010-06-19 03:54:17 From: Eric Davis Date: 2010-06-19 03:54:17 Subject: [PATCH] Fix a nil error when a Project cannot save attachments. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3772 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/app/models/attachment.rb b/app/models/attachment.rb index c752e0e..f05b35c 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -150,7 +150,13 @@ class Attachment < ActiveRecord::Base :file => file, :description => attachment['description'].to_s.strip, :author => User.current) - a.new_record? ? (obj.unsaved_attachments << a) : (attached << a) + + if a.new_record? + obj.unsaved_attachments ||= [] + obj.unsaved_attachments << a + else + attached << a + end end end {:files => attached, :unsaved => obj.unsaved_attachments} diff --git a/test/unit/attachment_test.rb b/test/unit/attachment_test.rb index 7c6a9b4..f04aca4 100644 --- a/test/unit/attachment_test.rb +++ b/test/unit/attachment_test.rb @@ -63,4 +63,30 @@ class AttachmentTest < ActiveSupport::TestCase assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentué")[13..-1] assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentué.ça")[13..-1] end + + context "Attachmnet#attach_files" do + should "add unsaved files to the object as unsaved attachments" do + # Max size of 0 to force Attachment creation failures + with_settings(:attachment_max_size => 0) do + # Mock out a file + @file = 'a_file.png' + @file.stubs(:size).returns(32) + @file.stubs(:original_filename).returns('a_file.png') + @file.stubs(:content_type).returns('image/png') + @file.stubs(:read).returns(false) + + @project = Project.generate! + response = Attachment.attach_files(@project, { + '1' => {'file' => @file, 'description' => 'test'}, + '2' => {'file' => @file, 'description' => 'test'} + }) + + assert response[:unsaved].present? + assert_equal 2, response[:unsaved].length + assert response[:unsaved].first.new_record? + assert response[:unsaved].second.new_record? + assert_equal response[:unsaved], @project.unsaved_attachments + end + end + end end