Chris@441
|
1 # Redmine - project management software
|
Chris@1494
|
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@909
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@909
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@0
|
18 class Journal < ActiveRecord::Base
|
Chris@0
|
19 belongs_to :journalized, :polymorphic => true
|
Chris@0
|
20 # added as a quick fix to allow eager loading of the polymorphic association
|
Chris@0
|
21 # since always associated to an issue, for now
|
Chris@0
|
22 belongs_to :issue, :foreign_key => :journalized_id
|
Chris@909
|
23
|
Chris@0
|
24 belongs_to :user
|
Chris@0
|
25 has_many :details, :class_name => "JournalDetail", :dependent => :delete_all
|
Chris@0
|
26 attr_accessor :indice
|
Chris@909
|
27
|
Chris@0
|
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}" },
|
Chris@0
|
29 :description => :notes,
|
Chris@0
|
30 :author => :user,
|
Chris@1464
|
31 :group => :issue,
|
Chris@0
|
32 :type => Proc.new {|o| (s = o.new_status) ? (s.is_closed? ? 'issue-closed' : 'issue-edit') : 'issue-note' },
|
Chris@0
|
33 :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.issue.id, :anchor => "change-#{o.id}"}}
|
Chris@0
|
34
|
Chris@0
|
35 acts_as_activity_provider :type => 'issues',
|
Chris@0
|
36 :author_key => :user_id,
|
Chris@0
|
37 :find_options => {:include => [{:issue => :project}, :details, :user],
|
Chris@0
|
38 :conditions => "#{Journal.table_name}.journalized_type = 'Issue' AND" +
|
Chris@0
|
39 " (#{JournalDetail.table_name}.prop_key = 'status_id' OR #{Journal.table_name}.notes <> '')"}
|
Chris@909
|
40
|
Chris@1115
|
41 before_create :split_private_notes
|
Chris@1464
|
42 after_create :send_notification
|
Chris@1115
|
43
|
Chris@1115
|
44 scope :visible, lambda {|*args|
|
Chris@1115
|
45 user = args.shift || User.current
|
Chris@1115
|
46
|
Chris@1115
|
47 includes(:issue => :project).
|
Chris@1115
|
48 where(Issue.visible_condition(user, *args)).
|
Chris@1115
|
49 where("(#{Journal.table_name}.private_notes = ? OR (#{Project.allowed_to_condition(user, :view_private_notes, *args)}))", false)
|
Chris@1115
|
50 }
|
Chris@909
|
51
|
Chris@0
|
52 def save(*args)
|
Chris@0
|
53 # Do not save an empty journal
|
Chris@0
|
54 (details.empty? && notes.blank?) ? false : super
|
Chris@0
|
55 end
|
Chris@909
|
56
|
Chris@1464
|
57 # Returns journal details that are visible to user
|
Chris@1464
|
58 def visible_details(user=User.current)
|
Chris@1464
|
59 details.select do |detail|
|
Chris@1464
|
60 if detail.property == 'cf'
|
Chris@1464
|
61 detail.custom_field && detail.custom_field.visible_by?(project, user)
|
Chris@1464
|
62 elsif detail.property == 'relation'
|
Chris@1464
|
63 Issue.find_by_id(detail.value || detail.old_value).try(:visible?, user)
|
Chris@1464
|
64 else
|
Chris@1464
|
65 true
|
Chris@1464
|
66 end
|
Chris@1464
|
67 end
|
Chris@1464
|
68 end
|
Chris@1464
|
69
|
Chris@1464
|
70 def each_notification(users, &block)
|
Chris@1464
|
71 if users.any?
|
Chris@1464
|
72 users_by_details_visibility = users.group_by do |user|
|
Chris@1464
|
73 visible_details(user)
|
Chris@1464
|
74 end
|
Chris@1464
|
75 users_by_details_visibility.each do |visible_details, users|
|
Chris@1464
|
76 if notes? || visible_details.any?
|
Chris@1464
|
77 yield(users)
|
Chris@1464
|
78 end
|
Chris@1464
|
79 end
|
Chris@1464
|
80 end
|
Chris@1464
|
81 end
|
Chris@1464
|
82
|
Chris@0
|
83 # Returns the new status if the journal contains a status change, otherwise nil
|
Chris@0
|
84 def new_status
|
Chris@0
|
85 c = details.detect {|detail| detail.prop_key == 'status_id'}
|
Chris@0
|
86 (c && c.value) ? IssueStatus.find_by_id(c.value.to_i) : nil
|
Chris@0
|
87 end
|
Chris@909
|
88
|
Chris@0
|
89 def new_value_for(prop)
|
Chris@0
|
90 c = details.detect {|detail| detail.prop_key == prop}
|
Chris@0
|
91 c ? c.value : nil
|
Chris@0
|
92 end
|
Chris@909
|
93
|
Chris@0
|
94 def editable_by?(usr)
|
Chris@0
|
95 usr && usr.logged? && (usr.allowed_to?(:edit_issue_notes, project) || (self.user == usr && usr.allowed_to?(:edit_own_issue_notes, project)))
|
Chris@0
|
96 end
|
Chris@909
|
97
|
Chris@0
|
98 def project
|
Chris@0
|
99 journalized.respond_to?(:project) ? journalized.project : nil
|
Chris@0
|
100 end
|
Chris@909
|
101
|
Chris@0
|
102 def attachments
|
Chris@0
|
103 journalized.respond_to?(:attachments) ? journalized.attachments : nil
|
Chris@0
|
104 end
|
chris@22
|
105
|
chris@22
|
106 # Returns a string of css classes
|
chris@22
|
107 def css_classes
|
chris@22
|
108 s = 'journal'
|
chris@22
|
109 s << ' has-notes' unless notes.blank?
|
chris@22
|
110 s << ' has-details' unless details.blank?
|
Chris@1115
|
111 s << ' private-notes' if private_notes?
|
chris@22
|
112 s
|
chris@22
|
113 end
|
Chris@909
|
114
|
Chris@441
|
115 def notify?
|
Chris@441
|
116 @notify != false
|
Chris@441
|
117 end
|
Chris@909
|
118
|
Chris@441
|
119 def notify=(arg)
|
Chris@441
|
120 @notify = arg
|
Chris@441
|
121 end
|
Chris@1115
|
122
|
Chris@1464
|
123 def notified_users
|
Chris@1115
|
124 notified = journalized.notified_users
|
Chris@1115
|
125 if private_notes?
|
Chris@1115
|
126 notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)}
|
Chris@1115
|
127 end
|
Chris@1464
|
128 notified
|
Chris@1115
|
129 end
|
Chris@1115
|
130
|
Chris@1464
|
131 def recipients
|
Chris@1464
|
132 notified_users.map(&:mail)
|
Chris@1464
|
133 end
|
Chris@1464
|
134
|
Chris@1464
|
135 def notified_watchers
|
Chris@1115
|
136 notified = journalized.notified_watchers
|
Chris@1115
|
137 if private_notes?
|
Chris@1115
|
138 notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)}
|
Chris@1115
|
139 end
|
Chris@1464
|
140 notified
|
Chris@1464
|
141 end
|
Chris@1464
|
142
|
Chris@1464
|
143 def watcher_recipients
|
Chris@1464
|
144 notified_watchers.map(&:mail)
|
Chris@1464
|
145 end
|
Chris@1464
|
146
|
Chris@1464
|
147 # Sets @custom_field instance variable on journals details using a single query
|
Chris@1464
|
148 def self.preload_journals_details_custom_fields(journals)
|
Chris@1464
|
149 field_ids = journals.map(&:details).flatten.select {|d| d.property == 'cf'}.map(&:prop_key).uniq
|
Chris@1464
|
150 if field_ids.any?
|
Chris@1517
|
151 fields_by_id = CustomField.where(:id => field_ids).inject({}) {|h, f| h[f.id] = f; h}
|
Chris@1464
|
152 journals.each do |journal|
|
Chris@1464
|
153 journal.details.each do |detail|
|
Chris@1464
|
154 if detail.property == 'cf'
|
Chris@1464
|
155 detail.instance_variable_set "@custom_field", fields_by_id[detail.prop_key.to_i]
|
Chris@1464
|
156 end
|
Chris@1464
|
157 end
|
Chris@1464
|
158 end
|
Chris@1464
|
159 end
|
Chris@1464
|
160 journals
|
Chris@1115
|
161 end
|
Chris@1115
|
162
|
Chris@1115
|
163 private
|
Chris@1115
|
164
|
Chris@1115
|
165 def split_private_notes
|
Chris@1115
|
166 if private_notes?
|
Chris@1115
|
167 if notes.present?
|
Chris@1115
|
168 if details.any?
|
Chris@1115
|
169 # Split the journal (notes/changes) so we don't have half-private journals
|
Chris@1115
|
170 journal = Journal.new(:journalized => journalized, :user => user, :notes => nil, :private_notes => false)
|
Chris@1115
|
171 journal.details = details
|
Chris@1115
|
172 journal.save
|
Chris@1115
|
173 self.details = []
|
Chris@1115
|
174 self.created_on = journal.created_on
|
Chris@1115
|
175 end
|
Chris@1115
|
176 else
|
Chris@1115
|
177 # Blank notes should not be private
|
Chris@1115
|
178 self.private_notes = false
|
Chris@1115
|
179 end
|
Chris@1115
|
180 end
|
Chris@1115
|
181 true
|
Chris@1115
|
182 end
|
Chris@1464
|
183
|
Chris@1464
|
184 def send_notification
|
Chris@1464
|
185 if notify? && (Setting.notified_events.include?('issue_updated') ||
|
Chris@1464
|
186 (Setting.notified_events.include?('issue_note_added') && notes.present?) ||
|
Chris@1464
|
187 (Setting.notified_events.include?('issue_status_updated') && new_status.present?) ||
|
Chris@1464
|
188 (Setting.notified_events.include?('issue_priority_updated') && new_value_for('priority_id').present?)
|
Chris@1464
|
189 )
|
Chris@1464
|
190 Mailer.deliver_issue_edit(self)
|
Chris@1464
|
191 end
|
Chris@1464
|
192 end
|
Chris@0
|
193 end
|