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