Chris@441: # Redmine - project management software Chris@1115: # Copyright (C) 2006-2012 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@909: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@909: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: class Journal < ActiveRecord::Base Chris@0: belongs_to :journalized, :polymorphic => true Chris@0: # added as a quick fix to allow eager loading of the polymorphic association Chris@0: # since always associated to an issue, for now Chris@0: belongs_to :issue, :foreign_key => :journalized_id Chris@909: Chris@0: belongs_to :user Chris@0: has_many :details, :class_name => "JournalDetail", :dependent => :delete_all Chris@0: attr_accessor :indice Chris@909: Chris@0: 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: :description => :notes, Chris@0: :author => :user, Chris@0: :type => Proc.new {|o| (s = o.new_status) ? (s.is_closed? ? 'issue-closed' : 'issue-edit') : 'issue-note' }, Chris@0: :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.issue.id, :anchor => "change-#{o.id}"}} Chris@0: Chris@0: acts_as_activity_provider :type => 'issues', Chris@0: :author_key => :user_id, Chris@0: :find_options => {:include => [{:issue => :project}, :details, :user], Chris@0: :conditions => "#{Journal.table_name}.journalized_type = 'Issue' AND" + Chris@0: " (#{JournalDetail.table_name}.prop_key = 'status_id' OR #{Journal.table_name}.notes <> '')"} Chris@909: Chris@1115: before_create :split_private_notes Chris@1115: Chris@1115: scope :visible, lambda {|*args| Chris@1115: user = args.shift || User.current Chris@1115: Chris@1115: includes(:issue => :project). Chris@1115: where(Issue.visible_condition(user, *args)). Chris@1115: where("(#{Journal.table_name}.private_notes = ? OR (#{Project.allowed_to_condition(user, :view_private_notes, *args)}))", false) Chris@1115: } Chris@909: Chris@0: def save(*args) Chris@0: # Do not save an empty journal Chris@0: (details.empty? && notes.blank?) ? false : super Chris@0: end Chris@909: Chris@0: # Returns the new status if the journal contains a status change, otherwise nil Chris@0: def new_status Chris@0: c = details.detect {|detail| detail.prop_key == 'status_id'} Chris@0: (c && c.value) ? IssueStatus.find_by_id(c.value.to_i) : nil Chris@0: end Chris@909: Chris@0: def new_value_for(prop) Chris@0: c = details.detect {|detail| detail.prop_key == prop} Chris@0: c ? c.value : nil Chris@0: end Chris@909: Chris@0: def editable_by?(usr) Chris@0: usr && usr.logged? && (usr.allowed_to?(:edit_issue_notes, project) || (self.user == usr && usr.allowed_to?(:edit_own_issue_notes, project))) Chris@0: end Chris@909: Chris@0: def project Chris@0: journalized.respond_to?(:project) ? journalized.project : nil Chris@0: end Chris@909: Chris@0: def attachments Chris@0: journalized.respond_to?(:attachments) ? journalized.attachments : nil Chris@0: end chris@22: chris@22: # Returns a string of css classes chris@22: def css_classes chris@22: s = 'journal' chris@22: s << ' has-notes' unless notes.blank? chris@22: s << ' has-details' unless details.blank? Chris@1115: s << ' private-notes' if private_notes? chris@22: s chris@22: end Chris@909: Chris@441: def notify? Chris@441: @notify != false Chris@441: end Chris@909: Chris@441: def notify=(arg) Chris@441: @notify = arg Chris@441: end Chris@1115: Chris@1115: def recipients Chris@1115: notified = journalized.notified_users Chris@1115: if private_notes? Chris@1115: notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)} Chris@1115: end Chris@1115: notified.map(&:mail) Chris@1115: end Chris@1115: Chris@1115: def watcher_recipients Chris@1115: notified = journalized.notified_watchers Chris@1115: if private_notes? Chris@1115: notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)} Chris@1115: end Chris@1115: notified.map(&:mail) Chris@1115: end Chris@1115: Chris@1115: private Chris@1115: Chris@1115: def split_private_notes Chris@1115: if private_notes? Chris@1115: if notes.present? Chris@1115: if details.any? Chris@1115: # Split the journal (notes/changes) so we don't have half-private journals Chris@1115: journal = Journal.new(:journalized => journalized, :user => user, :notes => nil, :private_notes => false) Chris@1115: journal.details = details Chris@1115: journal.save Chris@1115: self.details = [] Chris@1115: self.created_on = journal.created_on Chris@1115: end Chris@1115: else Chris@1115: # Blank notes should not be private Chris@1115: self.private_notes = false Chris@1115: end Chris@1115: end Chris@1115: true Chris@1115: end Chris@0: end