summaryrefslogtreecommitdiffstats
path: root/app/models/attachment.rb
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2014-11-29 13:41:53 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2014-11-29 13:41:53 +0000
commit288c014aa7aa608751dbafeb2c8b358f2fec5c22 (patch)
tree68a5705092edc501641630fa960cee368d27ca88 /app/models/attachment.rb
parent3c7f638a834d6d9717e3d8babe3bab6af5100994 (diff)
downloadredmine-288c014aa7aa608751dbafeb2c8b358f2fec5c22.tar.gz
redmine-288c014aa7aa608751dbafeb2c8b358f2fec5c22.zip
Edit attachments after upload (#1326).
git-svn-id: http://svn.redmine.org/redmine/trunk@13665 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models/attachment.rb')
-rw-r--r--app/models/attachment.rb37
1 files changed, 37 insertions, 0 deletions
diff --git a/app/models/attachment.rb b/app/models/attachment.rb
index da82088f2..42690912a 100644
--- a/app/models/attachment.rb
+++ b/app/models/attachment.rb
@@ -166,6 +166,14 @@ class Attachment < ActiveRecord::Base
end
end
+ def editable?(user=User.current)
+ if container_id
+ container && container.attachments_editable?(user)
+ else
+ author == user
+ end
+ end
+
def deletable?(user=User.current)
if container_id
container && container.attachments_deletable?(user)
@@ -254,6 +262,35 @@ class Attachment < ActiveRecord::Base
result
end
+ # Updates the filename and description of a set of attachments
+ # with the given hash of attributes. Returns true if all
+ # attachments were updated.
+ #
+ # Example:
+ # Attachment.update_attachments(attachments, {
+ # 4 => {:filename => 'foo'},
+ # 7 => {:filename => 'bar', :description => 'file description'}
+ # })
+ #
+ def self.update_attachments(attachments, params)
+ params = params.transform_keys {|key| key.to_i}
+
+ saved = true
+ transaction do
+ attachments.each do |attachment|
+ if p = params[attachment.id]
+ attachment.filename = p[:filename] if p.key?(:filename)
+ attachment.description = p[:description] if p.key?(:description)
+ saved &&= attachment.save
+ end
+ end
+ unless saved
+ raise ActiveRecord::Rollback
+ end
+ end
+ saved
+ end
+
def self.latest_attach(attachments, filename)
attachments.sort_by(&:created_on).reverse.detect do |att|
att.filename.downcase == filename.downcase