diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2012-01-20 17:56:28 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2012-01-20 17:56:28 +0000 |
commit | 8a3623733fa2ef879bb2a3f4355e0ce0df49b278 (patch) | |
tree | aabdb9f4aa05eff33a8f19551aa1fa8558ebc813 /app/models | |
parent | e347fba11aea87a6bdc9ab6aeaca742d13a8c613 (diff) | |
download | redmine-8a3623733fa2ef879bb2a3f4355e0ce0df49b278.tar.gz redmine-8a3623733fa2ef879bb2a3f4355e0ce0df49b278.zip |
Copy attachments on issue and project copy (#3055).
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8676 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/attachment.rb | 26 | ||||
-rw-r--r-- | app/models/issue.rb | 3 |
2 files changed, 25 insertions, 4 deletions
diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 09e4057cf..d57422db7 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -49,8 +49,16 @@ class Attachment < ActiveRecord::Base before_save :files_to_final_location after_destroy :delete_from_disk + # Returns an unsaved copy of the attachment + def copy(attributes=nil) + copy = self.class.new + copy.attributes = self.attributes.dup.except("id", "downloads") + copy.attributes = attributes if attributes + copy + end + def validate_max_file_size - if self.filesize > Setting.attachment_max_size.to_i.kilobytes + if @temp_file && self.filesize > Setting.attachment_max_size.to_i.kilobytes errors.add(:base, :too_long, :count => Setting.attachment_max_size.to_i.kilobytes) end end @@ -96,9 +104,11 @@ class Attachment < ActiveRecord::Base end end - # Deletes file on the disk + # Deletes the file from the file system if it's not referenced by other attachments def delete_from_disk - File.delete(diskfile) if !filename.blank? && File.exist?(diskfile) + if Attachment.first(:conditions => ["disk_filename = ? AND id <> ?", disk_filename, id]).nil? + delete_from_disk! + end end # Returns file's location on disk @@ -173,7 +183,15 @@ class Attachment < ActiveRecord::Base } end -private + private + + # Physically deletes the file from the file system + def delete_from_disk! + if disk_filename.present? && File.exist?(diskfile) + File.delete(diskfile) + end + end + def sanitize_filename(value) # get only the filename, not the whole path just_filename = value.gsub(/^.*(\\|\/)/, '') diff --git a/app/models/issue.rb b/app/models/issue.rb index abd2d210a..16257bf83 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -135,6 +135,9 @@ class Issue < ActiveRecord::Base self.custom_field_values = issue.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h} self.status = issue.status self.author = User.current + self.attachments = issue.attachments.map do |attachement| + attachement.copy(:container => self) + end @copied_from = issue self end |