From 5f7e880dffd3341c4e6d94cf96530c4f1ce338cf Mon Sep 17 00:00:00 2001 From: Jens Kraemer Date: Tue, 5 Apr 2016 21:35:07 +0800 Subject: [PATCH] notify the user of missing attachments - 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. --- config/locales/en.yml | 3 +++ lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb | 13 ++++++++++++- test/unit/issue_test.rb | 11 +++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/config/locales/en.yml b/config/locales/en.yml index c198851..0d3ab4e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -171,6 +171,9 @@ en: notice_failed_to_save_issues: "Failed to save %{count} issue(s) on %{total} selected: %{ids}." notice_failed_to_save_time_entries: "Failed to save %{count} time entrie(s) on %{total} selected: %{ids}." notice_failed_to_save_members: "Failed to save member(s): %{errors}." + notice_failed_to_save_attachments: + one: "Failed to save one attachment" + other: "Failed to save %{count} attachments" notice_no_issue_selected: "No issue is selected! Please, check the issues you want to edit." notice_account_pending: "Your account was created and is now pending administrator approval." notice_default_data_loaded: Default configuration successfully loaded. diff --git a/lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb b/lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb index 31403d7..f2b17ec 100644 --- a/lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb +++ b/lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb @@ -34,6 +34,7 @@ module Redmine options.merge(:as => :container, :dependent => :destroy, :inverse_of => :container) send :include, Redmine::Acts::Attachable::InstanceMethods before_save :attach_saved_attachments + validate :warn_about_failed_attachments end end @@ -82,6 +83,7 @@ module Redmine attachments = attachments.map(&:last) end if attachments.is_a?(Array) + @failed_attachment_count = 0 attachments.each do |attachment| next unless attachment.is_a?(Hash) a = nil @@ -90,7 +92,10 @@ module Redmine a = Attachment.create(:file => file, :author => author) elsif token = attachment['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.content_type = attachment['content_type'] unless attachment['content_type'].blank? end @@ -112,6 +117,12 @@ module Redmine end end + def warn_about_failed_attachments + if @failed_attachment_count && @failed_attachment_count > 0 + errors.add :base, ::I18n.t('notice_failed_to_save_attachments', count: @failed_attachment_count) + end + end + module ClassMethods end end diff --git a/test/unit/issue_test.rb b/test/unit/issue_test.rb index a07b18f..03dd572 100644 --- a/test/unit/issue_test.rb +++ b/test/unit/issue_test.rb @@ -2527,6 +2527,17 @@ class IssueTest < ActiveSupport::TestCase assert_equal %w(upload foo bar), issue.attachments.map(&:filename) 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 issue = Issue.generate!(:status_id => 1).reload assert !issue.closed? -- 2.1.4