Make sure that attachments are created in the same order they were selected (#12310).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@10793 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2012-11-07 18:45:56 +00:00
parent 00927f58af
commit a17f4c8375
2 changed files with 27 additions and 1 deletions

View File

@ -62,7 +62,19 @@ module Redmine
def save_attachments(attachments, author=User.current)
if attachments.is_a?(Hash)
attachments = attachments.values
attachments = attachments.stringify_keys
attachments = attachments.to_a.sort {|a, b|
if a.first.to_i > 0 && b.first.to_i > 0
a.first.to_i <=> b.first.to_i
elsif a.first.to_i > 0
1
elsif b.first.to_i > 0
-1
else
a.first <=> b.first
end
}
attachments = attachments.map(&:last)
end
if attachments.is_a?(Array)
attachments.each do |attachment|

View File

@ -1888,4 +1888,18 @@ class IssueTest < ActiveSupport::TestCase
assert_include 'priority-8', classes
assert_include 'priority-highest', classes
end
def test_save_attachments_with_hash_should_save_attachments_in_keys_order
set_tmp_attachments_directory
issue = Issue.generate!
issue.save_attachments({
'p0' => {'file' => mock_file_with_options(:original_filename => 'upload')},
'3' => {'file' => mock_file_with_options(:original_filename => 'bar')},
'1' => {'file' => mock_file_with_options(:original_filename => 'foo')}
})
issue.attach_saved_attachments
assert_equal 3, issue.reload.attachments.count
assert_equal %w(upload foo bar), issue.attachments.map(&:filename)
end
end