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