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