Better handling of attachments when issue validation fails (#10253).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8891 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang
2012-02-16 21:00:11 +00:00
parent a8f98bb749
commit d4e6355eb3
11 changed files with 265 additions and 49 deletions
+38 -1
View File
@@ -32,8 +32,8 @@ module Redmine
has_many :attachments, options.merge(:as => :container,
:order => "#{Attachment.table_name}.created_on",
:dependent => :destroy)
attr_accessor :unsaved_attachments
send :include, Redmine::Acts::Attachable::InstanceMethods
before_save :attach_saved_attachments
end
end
@@ -52,6 +52,43 @@ module Redmine
user.allowed_to?(self.class.attachable_options[:delete_permission], self.project)
end
def saved_attachments
@saved_attachments ||= []
end
def unsaved_attachments
@unsaved_attachments ||= []
end
def save_attachments(attachments, author=User.current)
if attachments && attachments.is_a?(Hash)
attachments.each_value do |attachment|
a = nil
if file = attachment['file']
next unless file && file.size > 0
a = Attachment.create(:file => file,
:description => attachment['description'].to_s.strip,
:author => author)
elsif token = attachment['token']
a = Attachment.find_by_token(token)
end
next unless a
if a.new_record?
unsaved_attachments << a
else
saved_attachments << a
end
end
end
{:files => saved_attachments, :unsaved => unsaved_attachments}
end
def attach_saved_attachments
saved_attachments.each do |attachment|
self.attachments << attachment
end
end
module ClassMethods
end
end