Chris@1296
|
1 # Redmine - project management software
|
Chris@1296
|
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
|
Chris@1296
|
3 #
|
Chris@1296
|
4 # This program is free software; you can redistribute it and/or
|
Chris@1296
|
5 # modify it under the terms of the GNU General Public License
|
Chris@1296
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@1296
|
7 # of the License, or (at your option) any later version.
|
Chris@1296
|
8 #
|
Chris@1296
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@1296
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@1296
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@1296
|
12 # GNU General Public License for more details.
|
Chris@1296
|
13 #
|
Chris@1296
|
14 # You should have received a copy of the GNU General Public License
|
Chris@1296
|
15 # along with this program; if not, write to the Free Software
|
Chris@1296
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@1296
|
17
|
Chris@1296
|
18 module Redmine
|
Chris@1296
|
19 module Acts
|
Chris@1296
|
20 module Attachable
|
Chris@1296
|
21 def self.included(base)
|
Chris@1296
|
22 base.extend ClassMethods
|
Chris@1296
|
23 end
|
Chris@1296
|
24
|
Chris@1296
|
25 module ClassMethods
|
Chris@1296
|
26 def acts_as_attachable(options = {})
|
Chris@1296
|
27 cattr_accessor :attachable_options
|
Chris@1296
|
28 self.attachable_options = {}
|
Chris@1296
|
29 attachable_options[:view_permission] = options.delete(:view_permission) || "view_#{self.name.pluralize.underscore}".to_sym
|
Chris@1296
|
30 attachable_options[:delete_permission] = options.delete(:delete_permission) || "edit_#{self.name.pluralize.underscore}".to_sym
|
Chris@1296
|
31
|
Chris@1296
|
32 has_many :attachments, options.merge(:as => :container,
|
Chris@1296
|
33 :order => "#{Attachment.table_name}.created_on ASC, #{Attachment.table_name}.id ASC",
|
Chris@1296
|
34 :dependent => :destroy)
|
Chris@1296
|
35 send :include, Redmine::Acts::Attachable::InstanceMethods
|
Chris@1296
|
36 before_save :attach_saved_attachments
|
Chris@1296
|
37 end
|
Chris@1296
|
38 end
|
Chris@1296
|
39
|
Chris@1296
|
40 module InstanceMethods
|
Chris@1296
|
41 def self.included(base)
|
Chris@1296
|
42 base.extend ClassMethods
|
Chris@1296
|
43 end
|
Chris@1296
|
44
|
Chris@1296
|
45 def attachments_visible?(user=User.current)
|
Chris@1296
|
46 (respond_to?(:visible?) ? visible?(user) : true) &&
|
Chris@1296
|
47 user.allowed_to?(self.class.attachable_options[:view_permission], self.project)
|
Chris@1296
|
48 end
|
Chris@1296
|
49
|
Chris@1296
|
50 def attachments_deletable?(user=User.current)
|
Chris@1296
|
51 (respond_to?(:visible?) ? visible?(user) : true) &&
|
Chris@1296
|
52 user.allowed_to?(self.class.attachable_options[:delete_permission], self.project)
|
Chris@1296
|
53 end
|
Chris@1296
|
54
|
Chris@1296
|
55 def saved_attachments
|
Chris@1296
|
56 @saved_attachments ||= []
|
Chris@1296
|
57 end
|
Chris@1296
|
58
|
Chris@1296
|
59 def unsaved_attachments
|
Chris@1296
|
60 @unsaved_attachments ||= []
|
Chris@1296
|
61 end
|
Chris@1296
|
62
|
Chris@1296
|
63 def save_attachments(attachments, author=User.current)
|
Chris@1296
|
64 if attachments.is_a?(Hash)
|
Chris@1296
|
65 attachments = attachments.stringify_keys
|
Chris@1296
|
66 attachments = attachments.to_a.sort {|a, b|
|
Chris@1296
|
67 if a.first.to_i > 0 && b.first.to_i > 0
|
Chris@1296
|
68 a.first.to_i <=> b.first.to_i
|
Chris@1296
|
69 elsif a.first.to_i > 0
|
Chris@1296
|
70 1
|
Chris@1296
|
71 elsif b.first.to_i > 0
|
Chris@1296
|
72 -1
|
Chris@1296
|
73 else
|
Chris@1296
|
74 a.first <=> b.first
|
Chris@1296
|
75 end
|
Chris@1296
|
76 }
|
Chris@1296
|
77 attachments = attachments.map(&:last)
|
Chris@1296
|
78 end
|
Chris@1296
|
79 if attachments.is_a?(Array)
|
Chris@1296
|
80 attachments.each do |attachment|
|
Chris@1296
|
81 a = nil
|
Chris@1296
|
82 if file = attachment['file']
|
Chris@1296
|
83 next unless file.size > 0
|
Chris@1296
|
84 a = Attachment.create(:file => file, :author => author)
|
Chris@1296
|
85 elsif token = attachment['token']
|
Chris@1296
|
86 a = Attachment.find_by_token(token)
|
Chris@1296
|
87 next unless a
|
Chris@1296
|
88 a.filename = attachment['filename'] unless attachment['filename'].blank?
|
Chris@1296
|
89 a.content_type = attachment['content_type']
|
Chris@1296
|
90 end
|
Chris@1296
|
91 next unless a
|
Chris@1296
|
92 a.description = attachment['description'].to_s.strip
|
Chris@1296
|
93 if a.new_record?
|
Chris@1296
|
94 unsaved_attachments << a
|
Chris@1296
|
95 else
|
Chris@1296
|
96 saved_attachments << a
|
Chris@1296
|
97 end
|
Chris@1296
|
98 end
|
Chris@1296
|
99 end
|
Chris@1296
|
100 {:files => saved_attachments, :unsaved => unsaved_attachments}
|
Chris@1296
|
101 end
|
Chris@1296
|
102
|
Chris@1296
|
103 def attach_saved_attachments
|
Chris@1296
|
104 saved_attachments.each do |attachment|
|
Chris@1296
|
105 self.attachments << attachment
|
Chris@1296
|
106 end
|
Chris@1296
|
107 end
|
Chris@1296
|
108
|
Chris@1296
|
109 module ClassMethods
|
Chris@1296
|
110 end
|
Chris@1296
|
111 end
|
Chris@1296
|
112 end
|
Chris@1296
|
113 end
|
Chris@1296
|
114 end
|