From d4e6355eb3f220787e9374eeef6fb496f213f0d1 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Thu, 16 Feb 2012 21:00:11 +0000 Subject: 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 --- .../acts_as_attachable/lib/acts_as_attachable.rb | 39 +++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'vendor') diff --git a/vendor/plugins/acts_as_attachable/lib/acts_as_attachable.rb b/vendor/plugins/acts_as_attachable/lib/acts_as_attachable.rb index e840770a3..346e0ab8f 100644 --- a/vendor/plugins/acts_as_attachable/lib/acts_as_attachable.rb +++ b/vendor/plugins/acts_as_attachable/lib/acts_as_attachable.rb @@ -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 -- cgit v1.2.3