Project

General

Profile

Patch #22401 » 0001-notify-the-user-of-missing-attachments.patch

Jens Krämer, 2016-04-05 15:58

View differences:

config/locales/en.yml
171 171
  notice_failed_to_save_issues: "Failed to save %{count} issue(s) on %{total} selected: %{ids}."
172 172
  notice_failed_to_save_time_entries: "Failed to save %{count} time entrie(s) on %{total} selected: %{ids}."
173 173
  notice_failed_to_save_members: "Failed to save member(s): %{errors}."
174
  notice_failed_to_save_attachments:
175
    one: "Failed to save one attachment"
176
    other: "Failed to save %{count} attachments"
174 177
  notice_no_issue_selected: "No issue is selected! Please, check the issues you want to edit."
175 178
  notice_account_pending: "Your account was created and is now pending administrator approval."
176 179
  notice_default_data_loaded: Default configuration successfully loaded.
lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb
34 34
                                 options.merge(:as => :container, :dependent => :destroy, :inverse_of => :container)
35 35
          send :include, Redmine::Acts::Attachable::InstanceMethods
36 36
          before_save :attach_saved_attachments
37
          validate :warn_about_failed_attachments
37 38
        end
38 39
      end
39 40

  
......
82 83
            attachments = attachments.map(&:last)
83 84
          end
84 85
          if attachments.is_a?(Array)
86
            @failed_attachment_count = 0
85 87
            attachments.each do |attachment|
86 88
              next unless attachment.is_a?(Hash)
87 89
              a = nil
......
90 92
                a = Attachment.create(:file => file, :author => author)
91 93
              elsif token = attachment['token']
92 94
                a = Attachment.find_by_token(token)
93
                next unless a
95
                unless a
96
                  @failed_attachment_count += 1
97
                  next
98
                end
94 99
                a.filename = attachment['filename'] unless attachment['filename'].blank?
95 100
                a.content_type = attachment['content_type'] unless attachment['content_type'].blank?
96 101
              end
......
112 117
          end
113 118
        end
114 119

  
120
        def warn_about_failed_attachments
121
          if @failed_attachment_count && @failed_attachment_count > 0
122
            errors.add :base, ::I18n.t('notice_failed_to_save_attachments', count: @failed_attachment_count)
123
          end
124
        end
125

  
115 126
        module ClassMethods
116 127
        end
117 128
      end
test/unit/issue_test.rb
2527 2527
    assert_equal %w(upload foo bar), issue.attachments.map(&:filename)
2528 2528
  end
2529 2529

  
2530
  def test_save_attachments_with_array_should_warn_about_missing_tokens
2531
    set_tmp_attachments_directory
2532
    issue = Issue.generate!
2533
    issue.save_attachments([
2534
      {'token' => 'missing'}
2535
    ])
2536
    assert !issue.save
2537
    assert issue.errors[:base].present?
2538
    assert_equal 0, issue.reload.attachments.count
2539
  end
2540

  
2530 2541
  def test_closed_on_should_be_nil_when_creating_an_open_issue
2531 2542
    issue = Issue.generate!(:status_id => 1).reload
2532 2543
    assert !issue.closed?
    (1-1/1)