From a17f4c8375d1291e6116e3c40492ebc2ed4cfda2 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Wed, 7 Nov 2012 18:45:56 +0000 Subject: [PATCH] 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 --- .../acts_as_attachable/lib/acts_as_attachable.rb | 14 +++++++++++++- test/unit/issue_test.rb | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) 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 626023b1b..344ff0866 100644 --- a/lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb +++ b/lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb @@ -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| diff --git a/test/unit/issue_test.rb b/test/unit/issue_test.rb index 777edcf95..a5ab9aa72 100644 --- a/test/unit/issue_test.rb +++ b/test/unit/issue_test.rb @@ -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