annotate .svn/pristine/37/37f2c260a3ef3c7956ee2bd34621efe201bd4fb9.svn-base @ 1628:9c5f8e24dadc live tip

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