Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

journal.rb 11KB

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