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