@@ -1,199 +1,199 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2014 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 | 25 | has_many :details, :class_name => "JournalDetail", :dependent => :delete_all |
|
26 | 26 | attr_accessor :indice |
|
27 | 27 | |
|
28 | 28 | 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 | 29 | :description => :notes, |
|
30 | 30 | :author => :user, |
|
31 | 31 | :group => :issue, |
|
32 | 32 | :type => Proc.new {|o| (s = o.new_status) ? (s.is_closed? ? 'issue-closed' : 'issue-edit') : 'issue-note' }, |
|
33 | 33 | :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.issue.id, :anchor => "change-#{o.id}"}} |
|
34 | 34 | |
|
35 | 35 | acts_as_activity_provider :type => 'issues', |
|
36 | 36 | :author_key => :user_id, |
|
37 | 37 | :find_options => {:include => [{:issue => :project}, :details, :user], |
|
38 | 38 | :conditions => "#{Journal.table_name}.journalized_type = 'Issue' AND" + |
|
39 | 39 | " (#{JournalDetail.table_name}.prop_key = 'status_id' OR #{Journal.table_name}.notes <> '')"} |
|
40 | 40 | |
|
41 | 41 | before_create :split_private_notes |
|
42 | 42 | after_create :send_notification |
|
43 | 43 | |
|
44 | 44 | scope :visible, lambda {|*args| |
|
45 | 45 | user = args.shift || User.current |
|
46 | 46 | |
|
47 | 47 | includes(:issue => :project). |
|
48 | 48 | where(Issue.visible_condition(user, *args)). |
|
49 | 49 | where("(#{Journal.table_name}.private_notes = ? OR (#{Project.allowed_to_condition(user, :view_private_notes, *args)}))", false) |
|
50 | 50 | } |
|
51 | 51 | |
|
52 | 52 | def save(*args) |
|
53 | 53 | # Do not save an empty journal |
|
54 | 54 | (details.empty? && notes.blank?) ? false : super |
|
55 | 55 | end |
|
56 | 56 | |
|
57 | 57 | # Returns journal details that are visible to user |
|
58 | 58 | def visible_details(user=User.current) |
|
59 | 59 | details.select do |detail| |
|
60 | 60 | if detail.property == 'cf' |
|
61 | 61 | detail.custom_field && detail.custom_field.visible_by?(project, user) |
|
62 | 62 | elsif detail.property == 'relation' |
|
63 | 63 | Issue.find_by_id(detail.value || detail.old_value).try(:visible?, user) |
|
64 | 64 | else |
|
65 | 65 | true |
|
66 | 66 | end |
|
67 | 67 | end |
|
68 | 68 | end |
|
69 | 69 | |
|
70 | 70 | def each_notification(users, &block) |
|
71 | 71 | if users.any? |
|
72 | 72 | users_by_details_visibility = users.group_by do |user| |
|
73 | 73 | visible_details(user) |
|
74 | 74 | end |
|
75 | 75 | users_by_details_visibility.each do |visible_details, users| |
|
76 | 76 | if notes? || visible_details.any? |
|
77 | 77 | yield(users) |
|
78 | 78 | end |
|
79 | 79 | end |
|
80 | 80 | end |
|
81 | 81 | end |
|
82 | 82 | |
|
83 | 83 | # Returns the JournalDetail for the given attribute, or nil if the attribute |
|
84 | 84 | # was not updated |
|
85 | 85 | def detail_for_attribute(attribute) |
|
86 | 86 | details.detect {|detail| detail.prop_key == attribute} |
|
87 | 87 | end |
|
88 | 88 | |
|
89 | 89 | # Returns the new status if the journal contains a status change, otherwise nil |
|
90 | 90 | def new_status |
|
91 | 91 | s = new_value_for('status_id') |
|
92 | 92 | s ? IssueStatus.find_by_id(s.to_i) : nil |
|
93 | 93 | end |
|
94 | 94 | |
|
95 | 95 | def new_value_for(prop) |
|
96 | 96 | detail_for_attribute(prop).try(:value) |
|
97 | 97 | end |
|
98 | 98 | |
|
99 | 99 | def editable_by?(usr) |
|
100 | 100 | usr && usr.logged? && (usr.allowed_to?(:edit_issue_notes, project) || (self.user == usr && usr.allowed_to?(:edit_own_issue_notes, project))) |
|
101 | 101 | end |
|
102 | 102 | |
|
103 | 103 | def project |
|
104 | 104 | journalized.respond_to?(:project) ? journalized.project : nil |
|
105 | 105 | end |
|
106 | 106 | |
|
107 | 107 | def attachments |
|
108 | 108 | journalized.respond_to?(:attachments) ? journalized.attachments : nil |
|
109 | 109 | end |
|
110 | 110 | |
|
111 | 111 | # Returns a string of css classes |
|
112 | 112 | def css_classes |
|
113 | 113 | s = 'journal' |
|
114 | 114 | s << ' has-notes' unless notes.blank? |
|
115 | 115 | s << ' has-details' unless details.blank? |
|
116 | 116 | s << ' private-notes' if private_notes? |
|
117 | 117 | s |
|
118 | 118 | end |
|
119 | 119 | |
|
120 | 120 | def notify? |
|
121 | 121 | @notify != false |
|
122 | 122 | end |
|
123 | 123 | |
|
124 | 124 | def notify=(arg) |
|
125 | 125 | @notify = arg |
|
126 | 126 | end |
|
127 | 127 | |
|
128 | 128 | def notified_users |
|
129 | 129 | notified = journalized.notified_users |
|
130 | 130 | if private_notes? |
|
131 | 131 | notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)} |
|
132 | 132 | end |
|
133 | 133 | notified |
|
134 | 134 | end |
|
135 | 135 | |
|
136 | 136 | def recipients |
|
137 | 137 | notified_users.map(&:mail) |
|
138 | 138 | end |
|
139 | 139 | |
|
140 | 140 | def notified_watchers |
|
141 | 141 | notified = journalized.notified_watchers |
|
142 | 142 | if private_notes? |
|
143 | 143 | notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)} |
|
144 | 144 | end |
|
145 | 145 | notified |
|
146 | 146 | end |
|
147 | 147 | |
|
148 | 148 | def watcher_recipients |
|
149 | 149 | notified_watchers.map(&:mail) |
|
150 | 150 | end |
|
151 | 151 | |
|
152 | 152 | # Sets @custom_field instance variable on journals details using a single query |
|
153 | 153 | def self.preload_journals_details_custom_fields(journals) |
|
154 | 154 | field_ids = journals.map(&:details).flatten.select {|d| d.property == 'cf'}.map(&:prop_key).uniq |
|
155 | 155 | if field_ids.any? |
|
156 | 156 | fields_by_id = CustomField.where(:id => field_ids).inject({}) {|h, f| h[f.id] = f; h} |
|
157 | 157 | journals.each do |journal| |
|
158 | 158 | journal.details.each do |detail| |
|
159 | 159 | if detail.property == 'cf' |
|
160 | 160 | detail.instance_variable_set "@custom_field", fields_by_id[detail.prop_key.to_i] |
|
161 | 161 | end |
|
162 | 162 | end |
|
163 | 163 | end |
|
164 | 164 | end |
|
165 | 165 | journals |
|
166 | 166 | end |
|
167 | 167 | |
|
168 | 168 | private |
|
169 | 169 | |
|
170 | 170 | def split_private_notes |
|
171 | 171 | if private_notes? |
|
172 | 172 | if notes.present? |
|
173 | 173 | if details.any? |
|
174 | 174 | # Split the journal (notes/changes) so we don't have half-private journals |
|
175 | 175 | journal = Journal.new(:journalized => journalized, :user => user, :notes => nil, :private_notes => false) |
|
176 | 176 | journal.details = details |
|
177 | 177 | journal.save |
|
178 | 178 | self.details = [] |
|
179 | 179 | self.created_on = journal.created_on |
|
180 | 180 | end |
|
181 | 181 | else |
|
182 | 182 | # Blank notes should not be private |
|
183 | 183 | self.private_notes = false |
|
184 | 184 | end |
|
185 | 185 | end |
|
186 | 186 | true |
|
187 | 187 | end |
|
188 | 188 | |
|
189 | 189 | def send_notification |
|
190 | 190 | if notify? && (Setting.notified_events.include?('issue_updated') || |
|
191 | 191 | (Setting.notified_events.include?('issue_note_added') && notes.present?) || |
|
192 | 192 | (Setting.notified_events.include?('issue_status_updated') && new_status.present?) || |
|
193 |
(Setting.notified_events.include?('issue_assigned_to_updated') && |
|
|
193 | (Setting.notified_events.include?('issue_assigned_to_updated') && detail_for_attribute('assigned_to_id').present?) || | |
|
194 | 194 | (Setting.notified_events.include?('issue_priority_updated') && new_value_for('priority_id').present?) |
|
195 | 195 | ) |
|
196 | 196 | Mailer.deliver_issue_edit(self) |
|
197 | 197 | end |
|
198 | 198 | end |
|
199 | 199 | end |
General Comments 0
You need to be logged in to leave comments.
Login now