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