##// END OF EJS Templates
remove trailing white-spaces from vendor/plugins/acts_as_attachable/lib/acts_as_attachable.rb...
Toshi MARUYAMA -
r9168:f9d123c08fd1
parent child
Show More
@@ -1,102 +1,102
1 1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2012 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[:delete_permission] = options.delete(:delete_permission) || "edit_#{self.name.pluralize.underscore}".to_sym
31 31
32 32 has_many :attachments, options.merge(:as => :container,
33 33 :order => "#{Attachment.table_name}.created_on",
34 34 :dependent => :destroy)
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_deletable?(user=User.current)
51 51 (respond_to?(:visible?) ? visible?(user) : true) &&
52 52 user.allowed_to?(self.class.attachable_options[:delete_permission], self.project)
53 53 end
54 54
55 55 def saved_attachments
56 56 @saved_attachments ||= []
57 57 end
58 58
59 59 def unsaved_attachments
60 60 @unsaved_attachments ||= []
61 61 end
62 62
63 63 def save_attachments(attachments, author=User.current)
64 64 if attachments.is_a?(Hash)
65 65 attachments = attachments.values
66 66 end
67 67 if attachments.is_a?(Array)
68 68 attachments.each do |attachment|
69 69 a = nil
70 70 if file = attachment['file']
71 71 next unless file.size > 0
72 72 a = Attachment.create(:file => file, :author => author)
73 73 elsif token = attachment['token']
74 74 a = Attachment.find_by_token(token)
75 75 next unless a
76 76 a.filename = attachment['filename'] unless attachment['filename'].blank?
77 77 a.content_type = attachment['content_type']
78 78 end
79 79 next unless a
80 80 a.description = attachment['description'].to_s.strip
81 81 if a.new_record?
82 82 unsaved_attachments << a
83 83 else
84 84 saved_attachments << a
85 85 end
86 86 end
87 87 end
88 88 {:files => saved_attachments, :unsaved => unsaved_attachments}
89 89 end
90 90
91 91 def attach_saved_attachments
92 92 saved_attachments.each do |attachment|
93 93 self.attachments << attachment
94 94 end
95 95 end
96 96
97 97 module ClassMethods
98 98 end
99 99 end
100 100 end
101 101 end
102 102 end
General Comments 0
You need to be logged in to leave comments. Login now