diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 37b256a..967032d 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -83,6 +83,9 @@ class AttachmentsController < ApplicationController end def destroy + if @attachment.container.respond_to?(:init_journal) + @attachment.container.init_journal(User.current) + end # Make sure association callbacks are called @attachment.container.attachments.delete(@attachment) redirect_to :back diff --git a/app/models/issue.rb b/app/models/issue.rb index eced776..ee394a8 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -953,11 +953,10 @@ class Issue < ActiveRecord::Base # Callback on attachment deletion def attachment_removed(obj) - journal = init_journal(User.current) - journal.details << JournalDetail.new(:property => 'attachment', - :prop_key => obj.id, - :old_value => obj.filename) - journal.save + if @current_journal && !obj.new_record? + @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => obj.id, :old_value => obj.filename) + @current_journal.save + end end # Default assignment based on category diff --git a/test/functional/attachments_controller_test.rb b/test/functional/attachments_controller_test.rb index 4deae88..d06d9e8 100644 --- a/test/functional/attachments_controller_test.rb +++ b/test/functional/attachments_controller_test.rb @@ -264,15 +264,18 @@ class AttachmentsControllerTest < ActionController::TestCase @request.session[:user_id] = 2 assert_difference 'issue.attachments.count', -1 do - delete :destroy, :id => 1 + assert_difference 'Journal.count' do + delete :destroy, :id => 1 + assert_redirected_to '/projects/ecookbook' + end end - # no referrer - assert_redirected_to '/projects/ecookbook' assert_nil Attachment.find_by_id(1) - j = issue.journals.find(:first, :order => 'created_on DESC') + j = Journal.first(:order => 'id DESC') + assert_equal issue, j.journalized assert_equal 'attachment', j.details.first.property assert_equal '1', j.details.first.prop_key assert_equal 'error281.txt', j.details.first.old_value + assert_equal User.find(2), j.user end def test_destroy_wiki_page_attachment