Notify the user of missing attachments (#22401).

under certain (rare) circumstances it may happen that, when an issue or other
container is saved, added attachments have already been removed (i.e. by the
attachments:prune rake task). This patch adds a validation error to the
container in this case.

Patch by Jens Kraemer.

git-svn-id: http://svn.redmine.org/redmine/trunk@15378 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang
2016-05-07 10:31:20 +00:00
parent 3c45e433d5
commit 40c9c3e922
2 changed files with 23 additions and 1 deletions

View File

@@ -34,6 +34,7 @@ module Redmine
options.merge(:as => :container, :dependent => :destroy, :inverse_of => :container) options.merge(:as => :container, :dependent => :destroy, :inverse_of => :container)
send :include, Redmine::Acts::Attachable::InstanceMethods send :include, Redmine::Acts::Attachable::InstanceMethods
before_save :attach_saved_attachments before_save :attach_saved_attachments
validate :warn_about_failed_attachments
end end
end end
@@ -82,6 +83,7 @@ module Redmine
attachments = attachments.map(&:last) attachments = attachments.map(&:last)
end end
if attachments.is_a?(Array) if attachments.is_a?(Array)
@failed_attachment_count = 0
attachments.each do |attachment| attachments.each do |attachment|
next unless attachment.is_a?(Hash) next unless attachment.is_a?(Hash)
a = nil a = nil
@@ -90,7 +92,10 @@ module Redmine
a = Attachment.create(:file => file, :author => author) a = Attachment.create(:file => file, :author => author)
elsif token = attachment['token'] elsif token = attachment['token']
a = Attachment.find_by_token(token) a = Attachment.find_by_token(token)
next unless a unless a
@failed_attachment_count += 1
next
end
a.filename = attachment['filename'] unless attachment['filename'].blank? a.filename = attachment['filename'] unless attachment['filename'].blank?
a.content_type = attachment['content_type'] unless attachment['content_type'].blank? a.content_type = attachment['content_type'] unless attachment['content_type'].blank?
end end
@@ -112,6 +117,12 @@ module Redmine
end end
end end
def warn_about_failed_attachments
if @failed_attachment_count && @failed_attachment_count > 0
errors.add :base, ::I18n.t('warning_attachments_not_saved', count: @failed_attachment_count)
end
end
module ClassMethods module ClassMethods
end end
end end

View File

@@ -2527,6 +2527,17 @@ class IssueTest < ActiveSupport::TestCase
assert_equal %w(upload foo bar), issue.attachments.map(&:filename) assert_equal %w(upload foo bar), issue.attachments.map(&:filename)
end end
def test_save_attachments_with_array_should_warn_about_missing_tokens
set_tmp_attachments_directory
issue = Issue.generate!
issue.save_attachments([
{'token' => 'missing'}
])
assert !issue.save
assert issue.errors[:base].present?
assert_equal 0, issue.reload.attachments.count
end
def test_closed_on_should_be_nil_when_creating_an_open_issue def test_closed_on_should_be_nil_when_creating_an_open_issue
issue = Issue.generate!(:status_id => 1).reload issue = Issue.generate!(:status_id => 1).reload
assert !issue.closed? assert !issue.closed?