@@ -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, :inverse_of => :journal |
|
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 <> '')").uniq | |
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 |
@@ -1,189 +1,197 | |||||
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 | require File.expand_path('../../test_helper', __FILE__) |
|
18 | require File.expand_path('../../test_helper', __FILE__) | |
19 |
|
19 | |||
20 | class ActivityTest < ActiveSupport::TestCase |
|
20 | class ActivityTest < ActiveSupport::TestCase | |
21 | fixtures :projects, :versions, :attachments, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details, |
|
21 | fixtures :projects, :versions, :attachments, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details, | |
22 | :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages, :time_entries, |
|
22 | :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages, :time_entries, | |
23 | :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions |
|
23 | :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions | |
24 |
|
24 | |||
25 | def setup |
|
25 | def setup | |
26 | @project = Project.find(1) |
|
26 | @project = Project.find(1) | |
27 | end |
|
27 | end | |
28 |
|
28 | |||
29 | def test_activity_without_subprojects |
|
29 | def test_activity_without_subprojects | |
30 | events = find_events(User.anonymous, :project => @project) |
|
30 | events = find_events(User.anonymous, :project => @project) | |
31 | assert_not_nil events |
|
31 | assert_not_nil events | |
32 |
|
32 | |||
33 | assert events.include?(Issue.find(1)) |
|
33 | assert events.include?(Issue.find(1)) | |
34 | assert !events.include?(Issue.find(4)) |
|
34 | assert !events.include?(Issue.find(4)) | |
35 | # subproject issue |
|
35 | # subproject issue | |
36 | assert !events.include?(Issue.find(5)) |
|
36 | assert !events.include?(Issue.find(5)) | |
37 | end |
|
37 | end | |
38 |
|
38 | |||
39 | def test_activity_with_subprojects |
|
39 | def test_activity_with_subprojects | |
40 | events = find_events(User.anonymous, :project => @project, :with_subprojects => 1) |
|
40 | events = find_events(User.anonymous, :project => @project, :with_subprojects => 1) | |
41 | assert_not_nil events |
|
41 | assert_not_nil events | |
42 |
|
42 | |||
43 | assert events.include?(Issue.find(1)) |
|
43 | assert events.include?(Issue.find(1)) | |
44 | # subproject issue |
|
44 | # subproject issue | |
45 | assert events.include?(Issue.find(5)) |
|
45 | assert events.include?(Issue.find(5)) | |
46 | end |
|
46 | end | |
47 |
|
47 | |||
48 | def test_global_activity_anonymous |
|
48 | def test_global_activity_anonymous | |
49 | events = find_events(User.anonymous) |
|
49 | events = find_events(User.anonymous) | |
50 | assert_not_nil events |
|
50 | assert_not_nil events | |
51 |
|
51 | |||
52 | assert events.include?(Issue.find(1)) |
|
52 | assert events.include?(Issue.find(1)) | |
53 | assert events.include?(Message.find(5)) |
|
53 | assert events.include?(Message.find(5)) | |
54 | # Issue of a private project |
|
54 | # Issue of a private project | |
55 | assert !events.include?(Issue.find(4)) |
|
55 | assert !events.include?(Issue.find(4)) | |
56 | # Private issue and comment |
|
56 | # Private issue and comment | |
57 | assert !events.include?(Issue.find(14)) |
|
57 | assert !events.include?(Issue.find(14)) | |
58 | assert !events.include?(Journal.find(5)) |
|
58 | assert !events.include?(Journal.find(5)) | |
59 | end |
|
59 | end | |
60 |
|
60 | |||
61 | def test_global_activity_logged_user |
|
61 | def test_global_activity_logged_user | |
62 | events = find_events(User.find(2)) # manager |
|
62 | events = find_events(User.find(2)) # manager | |
63 | assert_not_nil events |
|
63 | assert_not_nil events | |
64 |
|
64 | |||
65 | assert events.include?(Issue.find(1)) |
|
65 | assert events.include?(Issue.find(1)) | |
66 | # Issue of a private project the user belongs to |
|
66 | # Issue of a private project the user belongs to | |
67 | assert events.include?(Issue.find(4)) |
|
67 | assert events.include?(Issue.find(4)) | |
68 | end |
|
68 | end | |
69 |
|
69 | |||
70 | def test_user_activity |
|
70 | def test_user_activity | |
71 | user = User.find(2) |
|
71 | user = User.find(2) | |
72 | events = Redmine::Activity::Fetcher.new(User.anonymous, :author => user).events(nil, nil, :limit => 10) |
|
72 | events = Redmine::Activity::Fetcher.new(User.anonymous, :author => user).events(nil, nil, :limit => 10) | |
73 |
|
73 | |||
74 | assert(events.size > 0) |
|
74 | assert(events.size > 0) | |
75 | assert(events.size <= 10) |
|
75 | assert(events.size <= 10) | |
76 | assert_nil(events.detect {|e| e.event_author != user}) |
|
76 | assert_nil(events.detect {|e| e.event_author != user}) | |
77 | end |
|
77 | end | |
78 |
|
78 | |||
|
79 | def test_journal_with_notes_and_changes_should_be_returned_once | |||
|
80 | f = Redmine::Activity::Fetcher.new(User.anonymous, :project => Project.find(1)) | |||
|
81 | f.scope = ['issues'] | |||
|
82 | events = f.events | |||
|
83 | ||||
|
84 | assert_equal events, events.uniq | |||
|
85 | end | |||
|
86 | ||||
79 | def test_files_activity |
|
87 | def test_files_activity | |
80 | f = Redmine::Activity::Fetcher.new(User.anonymous, :project => Project.find(1)) |
|
88 | f = Redmine::Activity::Fetcher.new(User.anonymous, :project => Project.find(1)) | |
81 | f.scope = ['files'] |
|
89 | f.scope = ['files'] | |
82 | events = f.events |
|
90 | events = f.events | |
83 |
|
91 | |||
84 | assert_kind_of Array, events |
|
92 | assert_kind_of Array, events | |
85 | assert events.include?(Attachment.find_by_container_type_and_container_id('Project', 1)) |
|
93 | assert events.include?(Attachment.find_by_container_type_and_container_id('Project', 1)) | |
86 | assert events.include?(Attachment.find_by_container_type_and_container_id('Version', 1)) |
|
94 | assert events.include?(Attachment.find_by_container_type_and_container_id('Version', 1)) | |
87 | assert_equal [Attachment], events.collect(&:class).uniq |
|
95 | assert_equal [Attachment], events.collect(&:class).uniq | |
88 | assert_equal %w(Project Version), events.collect(&:container_type).uniq.sort |
|
96 | assert_equal %w(Project Version), events.collect(&:container_type).uniq.sort | |
89 | end |
|
97 | end | |
90 |
|
98 | |||
91 | def test_event_group_for_issue |
|
99 | def test_event_group_for_issue | |
92 | issue = Issue.find(1) |
|
100 | issue = Issue.find(1) | |
93 | assert_equal issue, issue.event_group |
|
101 | assert_equal issue, issue.event_group | |
94 | end |
|
102 | end | |
95 |
|
103 | |||
96 | def test_event_group_for_journal |
|
104 | def test_event_group_for_journal | |
97 | issue = Issue.find(1) |
|
105 | issue = Issue.find(1) | |
98 | journal = issue.journals.first |
|
106 | journal = issue.journals.first | |
99 | assert_equal issue, journal.event_group |
|
107 | assert_equal issue, journal.event_group | |
100 | end |
|
108 | end | |
101 |
|
109 | |||
102 | def test_event_group_for_issue_time_entry |
|
110 | def test_event_group_for_issue_time_entry | |
103 | time = TimeEntry.where(:issue_id => 1).first |
|
111 | time = TimeEntry.where(:issue_id => 1).first | |
104 | assert_equal time.issue, time.event_group |
|
112 | assert_equal time.issue, time.event_group | |
105 | end |
|
113 | end | |
106 |
|
114 | |||
107 | def test_event_group_for_project_time_entry |
|
115 | def test_event_group_for_project_time_entry | |
108 | time = TimeEntry.where(:issue_id => nil).first |
|
116 | time = TimeEntry.where(:issue_id => nil).first | |
109 | assert_equal time, time.event_group |
|
117 | assert_equal time, time.event_group | |
110 | end |
|
118 | end | |
111 |
|
119 | |||
112 | def test_event_group_for_message |
|
120 | def test_event_group_for_message | |
113 | message = Message.find(1) |
|
121 | message = Message.find(1) | |
114 | reply = message.children.first |
|
122 | reply = message.children.first | |
115 | assert_equal message, message.event_group |
|
123 | assert_equal message, message.event_group | |
116 | assert_equal message, reply.event_group |
|
124 | assert_equal message, reply.event_group | |
117 | end |
|
125 | end | |
118 |
|
126 | |||
119 | def test_event_group_for_wiki_content_version |
|
127 | def test_event_group_for_wiki_content_version | |
120 | content = WikiContent::Version.find(1) |
|
128 | content = WikiContent::Version.find(1) | |
121 | assert_equal content.page, content.event_group |
|
129 | assert_equal content.page, content.event_group | |
122 | end |
|
130 | end | |
123 |
|
131 | |||
124 | class TestActivityProviderWithPermission |
|
132 | class TestActivityProviderWithPermission | |
125 | def self.activity_provider_options |
|
133 | def self.activity_provider_options | |
126 | {'test' => {:permission => :custom_permission}} |
|
134 | {'test' => {:permission => :custom_permission}} | |
127 | end |
|
135 | end | |
128 | end |
|
136 | end | |
129 |
|
137 | |||
130 | class TestActivityProviderWithNilPermission |
|
138 | class TestActivityProviderWithNilPermission | |
131 | def self.activity_provider_options |
|
139 | def self.activity_provider_options | |
132 | {'test' => {:permission => nil}} |
|
140 | {'test' => {:permission => nil}} | |
133 | end |
|
141 | end | |
134 | end |
|
142 | end | |
135 |
|
143 | |||
136 | class TestActivityProviderWithoutPermission |
|
144 | class TestActivityProviderWithoutPermission | |
137 | def self.activity_provider_options |
|
145 | def self.activity_provider_options | |
138 | {'test' => {}} |
|
146 | {'test' => {}} | |
139 | end |
|
147 | end | |
140 | end |
|
148 | end | |
141 |
|
149 | |||
142 | class MockUser |
|
150 | class MockUser | |
143 | def initialize(*permissions) |
|
151 | def initialize(*permissions) | |
144 | @permissions = permissions |
|
152 | @permissions = permissions | |
145 | end |
|
153 | end | |
146 |
|
154 | |||
147 | def allowed_to?(permission, *args) |
|
155 | def allowed_to?(permission, *args) | |
148 | @permissions.include?(permission) |
|
156 | @permissions.include?(permission) | |
149 | end |
|
157 | end | |
150 | end |
|
158 | end | |
151 |
|
159 | |||
152 | def test_event_types_should_consider_activity_provider_permission |
|
160 | def test_event_types_should_consider_activity_provider_permission | |
153 | Redmine::Activity.register 'test', :class_name => 'ActivityTest::TestActivityProviderWithPermission' |
|
161 | Redmine::Activity.register 'test', :class_name => 'ActivityTest::TestActivityProviderWithPermission' | |
154 | user = MockUser.new(:custom_permission) |
|
162 | user = MockUser.new(:custom_permission) | |
155 | f = Redmine::Activity::Fetcher.new(user, :project => Project.find(1)) |
|
163 | f = Redmine::Activity::Fetcher.new(user, :project => Project.find(1)) | |
156 | assert_include 'test', f.event_types |
|
164 | assert_include 'test', f.event_types | |
157 | ensure |
|
165 | ensure | |
158 | Redmine::Activity.delete 'test' |
|
166 | Redmine::Activity.delete 'test' | |
159 | end |
|
167 | end | |
160 |
|
168 | |||
161 | def test_event_types_should_include_activity_provider_with_nil_permission |
|
169 | def test_event_types_should_include_activity_provider_with_nil_permission | |
162 | Redmine::Activity.register 'test', :class_name => 'ActivityTest::TestActivityProviderWithNilPermission' |
|
170 | Redmine::Activity.register 'test', :class_name => 'ActivityTest::TestActivityProviderWithNilPermission' | |
163 | user = MockUser.new() |
|
171 | user = MockUser.new() | |
164 | f = Redmine::Activity::Fetcher.new(user, :project => Project.find(1)) |
|
172 | f = Redmine::Activity::Fetcher.new(user, :project => Project.find(1)) | |
165 | assert_include 'test', f.event_types |
|
173 | assert_include 'test', f.event_types | |
166 | ensure |
|
174 | ensure | |
167 | Redmine::Activity.delete 'test' |
|
175 | Redmine::Activity.delete 'test' | |
168 | end |
|
176 | end | |
169 |
|
177 | |||
170 | def test_event_types_should_use_default_permission_for_activity_provider_without_permission |
|
178 | def test_event_types_should_use_default_permission_for_activity_provider_without_permission | |
171 | Redmine::Activity.register 'test', :class_name => 'ActivityTest::TestActivityProviderWithoutPermission' |
|
179 | Redmine::Activity.register 'test', :class_name => 'ActivityTest::TestActivityProviderWithoutPermission' | |
172 |
|
180 | |||
173 | user = MockUser.new() |
|
181 | user = MockUser.new() | |
174 | f = Redmine::Activity::Fetcher.new(user, :project => Project.find(1)) |
|
182 | f = Redmine::Activity::Fetcher.new(user, :project => Project.find(1)) | |
175 | assert_not_include 'test', f.event_types |
|
183 | assert_not_include 'test', f.event_types | |
176 |
|
184 | |||
177 | user = MockUser.new(:view_test) |
|
185 | user = MockUser.new(:view_test) | |
178 | f = Redmine::Activity::Fetcher.new(user, :project => Project.find(1)) |
|
186 | f = Redmine::Activity::Fetcher.new(user, :project => Project.find(1)) | |
179 | assert_include 'test', f.event_types |
|
187 | assert_include 'test', f.event_types | |
180 | ensure |
|
188 | ensure | |
181 | Redmine::Activity.delete 'test' |
|
189 | Redmine::Activity.delete 'test' | |
182 | end |
|
190 | end | |
183 |
|
191 | |||
184 | private |
|
192 | private | |
185 |
|
193 | |||
186 | def find_events(user, options={}) |
|
194 | def find_events(user, options={}) | |
187 | Redmine::Activity::Fetcher.new(user, options).events(Date.today - 30, Date.today + 1) |
|
195 | Redmine::Activity::Fetcher.new(user, options).events(Date.today - 30, Date.today + 1) | |
188 | end |
|
196 | end | |
189 | end |
|
197 | end |
General Comments 0
You need to be logged in to leave comments.
Login now