annotate .svn/pristine/eb/ebfee87b692025d708fcb9b6e98c73314886e7fc.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
rev   line source
Chris@1295 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@1295 3 #
Chris@1295 4 # This program is free software; you can redistribute it and/or
Chris@1295 5 # modify it under the terms of the GNU General Public License
Chris@1295 6 # as published by the Free Software Foundation; either version 2
Chris@1295 7 # of the License, or (at your option) any later version.
Chris@1295 8 #
Chris@1295 9 # This program is distributed in the hope that it will be useful,
Chris@1295 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 12 # GNU General Public License for more details.
Chris@1295 13 #
Chris@1295 14 # You should have received a copy of the GNU General Public License
Chris@1295 15 # along with this program; if not, write to the Free Software
Chris@1295 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 17
Chris@1295 18 class Journal < ActiveRecord::Base
Chris@1295 19 belongs_to :journalized, :polymorphic => true
Chris@1295 20 # added as a quick fix to allow eager loading of the polymorphic association
Chris@1295 21 # since always associated to an issue, for now
Chris@1295 22 belongs_to :issue, :foreign_key => :journalized_id
Chris@1295 23
Chris@1295 24 belongs_to :user
Chris@1295 25 has_many :details, :class_name => "JournalDetail", :dependent => :delete_all
Chris@1295 26 attr_accessor :indice
Chris@1295 27
Chris@1295 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@1295 29 :description => :notes,
Chris@1295 30 :author => :user,
Chris@1295 31 :group => :issue,
Chris@1295 32 :type => Proc.new {|o| (s = o.new_status) ? (s.is_closed? ? 'issue-closed' : 'issue-edit') : 'issue-note' },
Chris@1295 33 :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.issue.id, :anchor => "change-#{o.id}"}}
Chris@1295 34
Chris@1295 35 acts_as_activity_provider :type => 'issues',
Chris@1295 36 :author_key => :user_id,
Chris@1295 37 :find_options => {:include => [{:issue => :project}, :details, :user],
Chris@1295 38 :conditions => "#{Journal.table_name}.journalized_type = 'Issue' AND" +
Chris@1295 39 " (#{JournalDetail.table_name}.prop_key = 'status_id' OR #{Journal.table_name}.notes <> '')"}
Chris@1295 40
Chris@1295 41 before_create :split_private_notes
Chris@1295 42
Chris@1295 43 scope :visible, lambda {|*args|
Chris@1295 44 user = args.shift || User.current
Chris@1295 45
Chris@1295 46 includes(:issue => :project).
Chris@1295 47 where(Issue.visible_condition(user, *args)).
Chris@1295 48 where("(#{Journal.table_name}.private_notes = ? OR (#{Project.allowed_to_condition(user, :view_private_notes, *args)}))", false)
Chris@1295 49 }
Chris@1295 50
Chris@1295 51 def save(*args)
Chris@1295 52 # Do not save an empty journal
Chris@1295 53 (details.empty? && notes.blank?) ? false : super
Chris@1295 54 end
Chris@1295 55
Chris@1295 56 # Returns the new status if the journal contains a status change, otherwise nil
Chris@1295 57 def new_status
Chris@1295 58 c = details.detect {|detail| detail.prop_key == 'status_id'}
Chris@1295 59 (c && c.value) ? IssueStatus.find_by_id(c.value.to_i) : nil
Chris@1295 60 end
Chris@1295 61
Chris@1295 62 def new_value_for(prop)
Chris@1295 63 c = details.detect {|detail| detail.prop_key == prop}
Chris@1295 64 c ? c.value : nil
Chris@1295 65 end
Chris@1295 66
Chris@1295 67 def editable_by?(usr)
Chris@1295 68 usr && usr.logged? && (usr.allowed_to?(:edit_issue_notes, project) || (self.user == usr && usr.allowed_to?(:edit_own_issue_notes, project)))
Chris@1295 69 end
Chris@1295 70
Chris@1295 71 def project
Chris@1295 72 journalized.respond_to?(:project) ? journalized.project : nil
Chris@1295 73 end
Chris@1295 74
Chris@1295 75 def attachments
Chris@1295 76 journalized.respond_to?(:attachments) ? journalized.attachments : nil
Chris@1295 77 end
Chris@1295 78
Chris@1295 79 # Returns a string of css classes
Chris@1295 80 def css_classes
Chris@1295 81 s = 'journal'
Chris@1295 82 s << ' has-notes' unless notes.blank?
Chris@1295 83 s << ' has-details' unless details.blank?
Chris@1295 84 s << ' private-notes' if private_notes?
Chris@1295 85 s
Chris@1295 86 end
Chris@1295 87
Chris@1295 88 def notify?
Chris@1295 89 @notify != false
Chris@1295 90 end
Chris@1295 91
Chris@1295 92 def notify=(arg)
Chris@1295 93 @notify = arg
Chris@1295 94 end
Chris@1295 95
Chris@1295 96 def recipients
Chris@1295 97 notified = journalized.notified_users
Chris@1295 98 if private_notes?
Chris@1295 99 notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)}
Chris@1295 100 end
Chris@1295 101 notified.map(&:mail)
Chris@1295 102 end
Chris@1295 103
Chris@1295 104 def watcher_recipients
Chris@1295 105 notified = journalized.notified_watchers
Chris@1295 106 if private_notes?
Chris@1295 107 notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)}
Chris@1295 108 end
Chris@1295 109 notified.map(&:mail)
Chris@1295 110 end
Chris@1295 111
Chris@1295 112 private
Chris@1295 113
Chris@1295 114 def split_private_notes
Chris@1295 115 if private_notes?
Chris@1295 116 if notes.present?
Chris@1295 117 if details.any?
Chris@1295 118 # Split the journal (notes/changes) so we don't have half-private journals
Chris@1295 119 journal = Journal.new(:journalized => journalized, :user => user, :notes => nil, :private_notes => false)
Chris@1295 120 journal.details = details
Chris@1295 121 journal.save
Chris@1295 122 self.details = []
Chris@1295 123 self.created_on = journal.created_on
Chris@1295 124 end
Chris@1295 125 else
Chris@1295 126 # Blank notes should not be private
Chris@1295 127 self.private_notes = false
Chris@1295 128 end
Chris@1295 129 end
Chris@1295 130 true
Chris@1295 131 end
Chris@1295 132 end