annotate app/models/journal.rb @ 1295:622f24f53b42 redmine-2.3

Update to Redmine SVN revision 11972 on 2.3-stable branch
author Chris Cannam
date Fri, 14 Jun 2013 09:02:21 +0100
parents 433d4f72a19b
children
rev   line source
Chris@441 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2013 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@1295 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@1115 42
Chris@1115 43 scope :visible, lambda {|*args|
Chris@1115 44 user = args.shift || User.current
Chris@1115 45
Chris@1115 46 includes(:issue => :project).
Chris@1115 47 where(Issue.visible_condition(user, *args)).
Chris@1115 48 where("(#{Journal.table_name}.private_notes = ? OR (#{Project.allowed_to_condition(user, :view_private_notes, *args)}))", false)
Chris@1115 49 }
Chris@909 50
Chris@0 51 def save(*args)
Chris@0 52 # Do not save an empty journal
Chris@0 53 (details.empty? && notes.blank?) ? false : super
Chris@0 54 end
Chris@909 55
Chris@0 56 # Returns the new status if the journal contains a status change, otherwise nil
Chris@0 57 def new_status
Chris@0 58 c = details.detect {|detail| detail.prop_key == 'status_id'}
Chris@0 59 (c && c.value) ? IssueStatus.find_by_id(c.value.to_i) : nil
Chris@0 60 end
Chris@909 61
Chris@0 62 def new_value_for(prop)
Chris@0 63 c = details.detect {|detail| detail.prop_key == prop}
Chris@0 64 c ? c.value : nil
Chris@0 65 end
Chris@909 66
Chris@0 67 def editable_by?(usr)
Chris@0 68 usr && usr.logged? && (usr.allowed_to?(:edit_issue_notes, project) || (self.user == usr && usr.allowed_to?(:edit_own_issue_notes, project)))
Chris@0 69 end
Chris@909 70
Chris@0 71 def project
Chris@0 72 journalized.respond_to?(:project) ? journalized.project : nil
Chris@0 73 end
Chris@909 74
Chris@0 75 def attachments
Chris@0 76 journalized.respond_to?(:attachments) ? journalized.attachments : nil
Chris@0 77 end
chris@22 78
chris@22 79 # Returns a string of css classes
chris@22 80 def css_classes
chris@22 81 s = 'journal'
chris@22 82 s << ' has-notes' unless notes.blank?
chris@22 83 s << ' has-details' unless details.blank?
Chris@1115 84 s << ' private-notes' if private_notes?
chris@22 85 s
chris@22 86 end
Chris@909 87
Chris@441 88 def notify?
Chris@441 89 @notify != false
Chris@441 90 end
Chris@909 91
Chris@441 92 def notify=(arg)
Chris@441 93 @notify = arg
Chris@441 94 end
Chris@1115 95
Chris@1115 96 def recipients
Chris@1115 97 notified = journalized.notified_users
Chris@1115 98 if private_notes?
Chris@1115 99 notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)}
Chris@1115 100 end
Chris@1115 101 notified.map(&:mail)
Chris@1115 102 end
Chris@1115 103
Chris@1115 104 def watcher_recipients
Chris@1115 105 notified = journalized.notified_watchers
Chris@1115 106 if private_notes?
Chris@1115 107 notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)}
Chris@1115 108 end
Chris@1115 109 notified.map(&:mail)
Chris@1115 110 end
Chris@1115 111
Chris@1115 112 private
Chris@1115 113
Chris@1115 114 def split_private_notes
Chris@1115 115 if private_notes?
Chris@1115 116 if notes.present?
Chris@1115 117 if details.any?
Chris@1115 118 # Split the journal (notes/changes) so we don't have half-private journals
Chris@1115 119 journal = Journal.new(:journalized => journalized, :user => user, :notes => nil, :private_notes => false)
Chris@1115 120 journal.details = details
Chris@1115 121 journal.save
Chris@1115 122 self.details = []
Chris@1115 123 self.created_on = journal.created_on
Chris@1115 124 end
Chris@1115 125 else
Chris@1115 126 # Blank notes should not be private
Chris@1115 127 self.private_notes = false
Chris@1115 128 end
Chris@1115 129 end
Chris@1115 130 true
Chris@1115 131 end
Chris@0 132 end