##// END OF EJS Templates
Allow Journal to return empty Array instead nil in Journal#attachments (#24308)....
Jean-Philippe Lang -
r15575:fcbd06e09069
parent child
Show More
@@ -1,320 +1,320
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 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 class Journal < ActiveRecord::Base
19 19 include Redmine::SafeAttributes
20 20
21 21 belongs_to :journalized, :polymorphic => true
22 22 # added as a quick fix to allow eager loading of the polymorphic association
23 23 # since always associated to an issue, for now
24 24 belongs_to :issue, :foreign_key => :journalized_id
25 25
26 26 belongs_to :user
27 27 has_many :details, :class_name => "JournalDetail", :dependent => :delete_all, :inverse_of => :journal
28 28 attr_accessor :indice
29 29 attr_protected :id
30 30
31 31 acts_as_event :title => Proc.new {|o| status = ((s = o.new_status) ? " (#{s})" : nil); "#{o.issue.tracker} ##{o.issue.id}#{status}: #{o.issue.subject}" },
32 32 :description => :notes,
33 33 :author => :user,
34 34 :group => :issue,
35 35 :type => Proc.new {|o| (s = o.new_status) ? (s.is_closed? ? 'issue-closed' : 'issue-edit') : 'issue-note' },
36 36 :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.issue.id, :anchor => "change-#{o.id}"}}
37 37
38 38 acts_as_activity_provider :type => 'issues',
39 39 :author_key => :user_id,
40 40 :scope => preload({:issue => :project}, :user).
41 41 joins("LEFT OUTER JOIN #{JournalDetail.table_name} ON #{JournalDetail.table_name}.journal_id = #{Journal.table_name}.id").
42 42 where("#{Journal.table_name}.journalized_type = 'Issue' AND" +
43 43 " (#{JournalDetail.table_name}.prop_key = 'status_id' OR #{Journal.table_name}.notes <> '')").distinct
44 44
45 45 before_create :split_private_notes
46 46 after_create :send_notification
47 47
48 48 scope :visible, lambda {|*args|
49 49 user = args.shift || User.current
50 50 joins(:issue => :project).
51 51 where(Issue.visible_condition(user, *args)).
52 52 where("(#{Journal.table_name}.private_notes = ? OR (#{Project.allowed_to_condition(user, :view_private_notes, *args)}))", false)
53 53 }
54 54
55 55 safe_attributes 'notes',
56 56 :if => lambda {|journal, user| journal.new_record? || journal.editable_by?(user)}
57 57 safe_attributes 'private_notes',
58 58 :if => lambda {|journal, user| user.allowed_to?(:set_notes_private, journal.project)}
59 59
60 60 def initialize(*args)
61 61 super
62 62 if journalized
63 63 if journalized.new_record?
64 64 self.notify = false
65 65 else
66 66 start
67 67 end
68 68 end
69 69 end
70 70
71 71 def save(*args)
72 72 journalize_changes
73 73 # Do not save an empty journal
74 74 (details.empty? && notes.blank?) ? false : super
75 75 end
76 76
77 77 # Returns journal details that are visible to user
78 78 def visible_details(user=User.current)
79 79 details.select do |detail|
80 80 if detail.property == 'cf'
81 81 detail.custom_field && detail.custom_field.visible_by?(project, user)
82 82 elsif detail.property == 'relation'
83 83 Issue.find_by_id(detail.value || detail.old_value).try(:visible?, user)
84 84 else
85 85 true
86 86 end
87 87 end
88 88 end
89 89
90 90 def each_notification(users, &block)
91 91 if users.any?
92 92 users_by_details_visibility = users.group_by do |user|
93 93 visible_details(user)
94 94 end
95 95 users_by_details_visibility.each do |visible_details, users|
96 96 if notes? || visible_details.any?
97 97 yield(users)
98 98 end
99 99 end
100 100 end
101 101 end
102 102
103 103 # Returns the JournalDetail for the given attribute, or nil if the attribute
104 104 # was not updated
105 105 def detail_for_attribute(attribute)
106 106 details.detect {|detail| detail.prop_key == attribute}
107 107 end
108 108
109 109 # Returns the new status if the journal contains a status change, otherwise nil
110 110 def new_status
111 111 s = new_value_for('status_id')
112 112 s ? IssueStatus.find_by_id(s.to_i) : nil
113 113 end
114 114
115 115 def new_value_for(prop)
116 116 detail_for_attribute(prop).try(:value)
117 117 end
118 118
119 119 def editable_by?(usr)
120 120 usr && usr.logged? && (usr.allowed_to?(:edit_issue_notes, project) || (self.user == usr && usr.allowed_to?(:edit_own_issue_notes, project)))
121 121 end
122 122
123 123 def project
124 124 journalized.respond_to?(:project) ? journalized.project : nil
125 125 end
126 126
127 127 def attachments
128 journalized.respond_to?(:attachments) ? journalized.attachments : nil
128 journalized.respond_to?(:attachments) ? journalized.attachments : []
129 129 end
130 130
131 131 # Returns a string of css classes
132 132 def css_classes
133 133 s = 'journal'
134 134 s << ' has-notes' unless notes.blank?
135 135 s << ' has-details' unless details.blank?
136 136 s << ' private-notes' if private_notes?
137 137 s
138 138 end
139 139
140 140 def notify?
141 141 @notify != false
142 142 end
143 143
144 144 def notify=(arg)
145 145 @notify = arg
146 146 end
147 147
148 148 def notified_users
149 149 notified = journalized.notified_users
150 150 if private_notes?
151 151 notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)}
152 152 end
153 153 notified
154 154 end
155 155
156 156 def recipients
157 157 notified_users.map(&:mail)
158 158 end
159 159
160 160 def notified_watchers
161 161 notified = journalized.notified_watchers
162 162 if private_notes?
163 163 notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)}
164 164 end
165 165 notified
166 166 end
167 167
168 168 def watcher_recipients
169 169 notified_watchers.map(&:mail)
170 170 end
171 171
172 172 # Sets @custom_field instance variable on journals details using a single query
173 173 def self.preload_journals_details_custom_fields(journals)
174 174 field_ids = journals.map(&:details).flatten.select {|d| d.property == 'cf'}.map(&:prop_key).uniq
175 175 if field_ids.any?
176 176 fields_by_id = CustomField.where(:id => field_ids).inject({}) {|h, f| h[f.id] = f; h}
177 177 journals.each do |journal|
178 178 journal.details.each do |detail|
179 179 if detail.property == 'cf'
180 180 detail.instance_variable_set "@custom_field", fields_by_id[detail.prop_key.to_i]
181 181 end
182 182 end
183 183 end
184 184 end
185 185 journals
186 186 end
187 187
188 188 # Stores the values of the attributes and custom fields of the journalized object
189 189 def start
190 190 if journalized
191 191 @attributes_before_change = journalized.journalized_attribute_names.inject({}) do |h, attribute|
192 192 h[attribute] = journalized.send(attribute)
193 193 h
194 194 end
195 195 @custom_values_before_change = journalized.custom_field_values.inject({}) do |h, c|
196 196 h[c.custom_field_id] = c.value
197 197 h
198 198 end
199 199 end
200 200 self
201 201 end
202 202
203 203 # Adds a journal detail for an attachment that was added or removed
204 204 def journalize_attachment(attachment, added_or_removed)
205 205 key = (added_or_removed == :removed ? :old_value : :value)
206 206 details << JournalDetail.new(
207 207 :property => 'attachment',
208 208 :prop_key => attachment.id,
209 209 key => attachment.filename
210 210 )
211 211 end
212 212
213 213 # Adds a journal detail for an issue relation that was added or removed
214 214 def journalize_relation(relation, added_or_removed)
215 215 key = (added_or_removed == :removed ? :old_value : :value)
216 216 details << JournalDetail.new(
217 217 :property => 'relation',
218 218 :prop_key => relation.relation_type_for(journalized),
219 219 key => relation.other_issue(journalized).try(:id)
220 220 )
221 221 end
222 222
223 223 private
224 224
225 225 # Generates journal details for attribute and custom field changes
226 226 def journalize_changes
227 227 # attributes changes
228 228 if @attributes_before_change
229 229 attrs = (journalized.journalized_attribute_names + @attributes_before_change.keys).uniq
230 230 attrs.each do |attribute|
231 231 before = @attributes_before_change[attribute]
232 232 after = journalized.send(attribute)
233 233 next if before == after || (before.blank? && after.blank?)
234 234 add_attribute_detail(attribute, before, after)
235 235 end
236 236 end
237 237 # custom fields changes
238 238 if @custom_values_before_change
239 239 values_by_custom_field_id = {}
240 240 @custom_values_before_change.each do |custom_field_id, value|
241 241 values_by_custom_field_id[custom_field_id] = nil
242 242 end
243 243 journalized.custom_field_values.each do |c|
244 244 values_by_custom_field_id[c.custom_field_id] = c.value
245 245 end
246 246
247 247 values_by_custom_field_id.each do |custom_field_id, after|
248 248 before = @custom_values_before_change[custom_field_id]
249 249 next if before == after || (before.blank? && after.blank?)
250 250
251 251 if before.is_a?(Array) || after.is_a?(Array)
252 252 before = [before] unless before.is_a?(Array)
253 253 after = [after] unless after.is_a?(Array)
254 254
255 255 # values removed
256 256 (before - after).reject(&:blank?).each do |value|
257 257 add_custom_field_detail(custom_field_id, value, nil)
258 258 end
259 259 # values added
260 260 (after - before).reject(&:blank?).each do |value|
261 261 add_custom_field_detail(custom_field_id, nil, value)
262 262 end
263 263 else
264 264 add_custom_field_detail(custom_field_id, before, after)
265 265 end
266 266 end
267 267 end
268 268 start
269 269 end
270 270
271 271 # Adds a journal detail for an attribute change
272 272 def add_attribute_detail(attribute, old_value, value)
273 273 add_detail('attr', attribute, old_value, value)
274 274 end
275 275
276 276 # Adds a journal detail for a custom field value change
277 277 def add_custom_field_detail(custom_field_id, old_value, value)
278 278 add_detail('cf', custom_field_id, old_value, value)
279 279 end
280 280
281 281 # Adds a journal detail
282 282 def add_detail(property, prop_key, old_value, value)
283 283 details << JournalDetail.new(
284 284 :property => property,
285 285 :prop_key => prop_key,
286 286 :old_value => old_value,
287 287 :value => value
288 288 )
289 289 end
290 290
291 291 def split_private_notes
292 292 if private_notes?
293 293 if notes.present?
294 294 if details.any?
295 295 # Split the journal (notes/changes) so we don't have half-private journals
296 296 journal = Journal.new(:journalized => journalized, :user => user, :notes => nil, :private_notes => false)
297 297 journal.details = details
298 298 journal.save
299 299 self.details = []
300 300 self.created_on = journal.created_on
301 301 end
302 302 else
303 303 # Blank notes should not be private
304 304 self.private_notes = false
305 305 end
306 306 end
307 307 true
308 308 end
309 309
310 310 def send_notification
311 311 if notify? && (Setting.notified_events.include?('issue_updated') ||
312 312 (Setting.notified_events.include?('issue_note_added') && notes.present?) ||
313 313 (Setting.notified_events.include?('issue_status_updated') && new_status.present?) ||
314 314 (Setting.notified_events.include?('issue_assigned_to_updated') && detail_for_attribute('assigned_to_id').present?) ||
315 315 (Setting.notified_events.include?('issue_priority_updated') && new_value_for('priority_id').present?)
316 316 )
317 317 Mailer.deliver_issue_edit(self)
318 318 end
319 319 end
320 320 end
General Comments 0
You need to be logged in to leave comments. Login now