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