From 288c014aa7aa608751dbafeb2c8b358f2fec5c22 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sat, 29 Nov 2014 13:41:53 +0000 Subject: Edit attachments after upload (#1326). git-svn-id: http://svn.redmine.org/redmine/trunk@13665 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/attachment.rb | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'app/models/attachment.rb') 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 -- cgit v1.2.3