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