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