diff --git a/lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb b/lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb index e35a2bcef..1011b82fd 100644 --- a/lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb +++ b/lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb @@ -29,6 +29,7 @@ module Redmine return if self.included_modules.include?(Redmine::Acts::Customizable::InstanceMethods) cattr_accessor :customizable_options self.customizable_options = options + before_destroy :preserve_custom_field_attachment_data has_many :custom_values, lambda {includes(:custom_field)}, :as => :customized, :inverse_of => :customized, @@ -38,6 +39,7 @@ module Redmine send :include, Redmine::Acts::Customizable::InstanceMethods validate :validate_custom_field_values after_save :save_custom_field_values + after_destroy :destroy_custom_field_attachments end end @@ -170,6 +172,16 @@ module Redmine super end + def preserve_custom_field_attachment_data + @custom_field_attachment_data = self.custom_values.select{ |cv| cv.custom_field.field_format == 'attachment'}.map(&:id) + end + + def destroy_custom_field_attachments + @custom_field_attachment_data.each do |container_id| + Attachment.where(:container_id => container_id, :container_type => 'CustomValue').destroy_all + end + end + module ClassMethods end end diff --git a/test/unit/issue_test.rb b/test/unit/issue_test.rb index bfb35b264..f304b7336 100644 --- a/test/unit/issue_test.rb +++ b/test/unit/issue_test.rb @@ -2163,6 +2163,28 @@ class IssueTest < ActiveSupport::TestCase end end + def test_destroy_should_delete_attachments_on_custom_values + cf = IssueCustomField.create!(:name => 'Attachable field', :field_format => 'attachment', :is_for_all => true, :tracker_ids => [1]) + user = User.find(2) + issue = Issue.new(:project_id => 1, :tracker_id => 1, :subject => 'test', :author_id => user.id) + attachment = Attachment.create!(:container => issue,:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => user.id) + issue.send( + :safe_attributes=, + { + 'custom_fields' => + [ + {'id' => cf.id.to_s, 'value' => attachment.id.to_s}, + ] + }, user + ) + + assert_difference 'CustomValue.where(:customized_type => "Issue").count', -(issue.custom_values.count) do + assert_difference 'Attachment.count', -1 do + issue.destroy + end + end + end + def test_destroying_a_deleted_issue_should_not_raise_an_error issue = Issue.find(1) Issue.find(1).destroy