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