annotate .svn/pristine/1d/1d734add023a98369ca3ca65fe3091fb8e56d827.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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