Project

General

Profile

Defect #38966 » fix-38966-remove_attachment_after_custom_value_deleted.patch

Takenori TAKAKI, 2023-09-07 08:09

View differences:

lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb
29 29
          return if self.included_modules.include?(Redmine::Acts::Customizable::InstanceMethods)
30 30
          cattr_accessor :customizable_options
31 31
          self.customizable_options = options
32
          before_destroy :preserve_custom_field_attachment_data
32 33
          has_many :custom_values, lambda {includes(:custom_field)},
33 34
                                   :as => :customized,
34 35
                                   :inverse_of => :customized,
......
38 39
          send :include, Redmine::Acts::Customizable::InstanceMethods
39 40
          validate :validate_custom_field_values
40 41
          after_save :save_custom_field_values
42
          after_destroy :destroy_custom_field_attachments
41 43
        end
42 44
      end
43 45

  
......
170 172
          super
171 173
        end
172 174

  
175
        def preserve_custom_field_attachment_data
176
          @custom_field_attachment_data = self.custom_values.select{ |cv| cv.custom_field.field_format == 'attachment'}.map(&:id)
177
        end
178

  
179
        def destroy_custom_field_attachments
180
          @custom_field_attachment_data.each do |container_id|
181
            Attachment.where(:container_id => container_id, :container_type => 'CustomValue').destroy_all
182
          end
183
        end
184

  
173 185
        module ClassMethods
174 186
        end
175 187
      end
test/unit/issue_test.rb
2163 2163
    end
2164 2164
  end
2165 2165

  
2166
  def test_destroy_should_delete_attachments_on_custom_values
2167
    cf = IssueCustomField.create!(:name => 'Attachable field', :field_format => 'attachment', :is_for_all => true, :tracker_ids => [1])
2168
    user = User.find(2)
2169
    issue = Issue.new(:project_id => 1, :tracker_id => 1, :subject => 'test', :author_id => user.id)
2170
    attachment = Attachment.create!(:container => issue,:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => user.id)
2171
    issue.send(
2172
      :safe_attributes=,
2173
      {
2174
        'custom_fields' =>
2175
          [
2176
            {'id' => cf.id.to_s, 'value' => attachment.id.to_s},
2177
          ]
2178
      }, user
2179
    )
2180

  
2181
    assert_difference 'CustomValue.where(:customized_type => "Issue").count', -(issue.custom_values.count) do
2182
      assert_difference 'Attachment.count', -1 do
2183
        issue.destroy
2184
      end
2185
    end
2186
  end
2187

  
2166 2188
  def test_destroying_a_deleted_issue_should_not_raise_an_error
2167 2189
    issue = Issue.find(1)
2168 2190
    Issue.find(1).destroy
(2-2/4)