Chris@1517: # Redmine - project management software Chris@1517: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1517: # Chris@1517: # This program is free software; you can redistribute it and/or Chris@1517: # modify it under the terms of the GNU General Public License Chris@1517: # as published by the Free Software Foundation; either version 2 Chris@1517: # of the License, or (at your option) any later version. Chris@1517: # Chris@1517: # This program is distributed in the hope that it will be useful, Chris@1517: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1517: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1517: # GNU General Public License for more details. Chris@1517: # Chris@1517: # You should have received a copy of the GNU General Public License Chris@1517: # along with this program; if not, write to the Free Software Chris@1517: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1517: Chris@1517: class IssueRelation < ActiveRecord::Base Chris@1517: # Class used to represent the relations of an issue Chris@1517: class Relations < Array Chris@1517: include Redmine::I18n Chris@1517: Chris@1517: def initialize(issue, *args) Chris@1517: @issue = issue Chris@1517: super(*args) Chris@1517: end Chris@1517: Chris@1517: def to_s(*args) Chris@1517: map {|relation| "#{l(relation.label_for(@issue))} ##{relation.other_issue(@issue).id}"}.join(', ') Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: belongs_to :issue_from, :class_name => 'Issue', :foreign_key => 'issue_from_id' Chris@1517: belongs_to :issue_to, :class_name => 'Issue', :foreign_key => 'issue_to_id' Chris@1517: Chris@1517: TYPE_RELATES = "relates" Chris@1517: TYPE_DUPLICATES = "duplicates" Chris@1517: TYPE_DUPLICATED = "duplicated" Chris@1517: TYPE_BLOCKS = "blocks" Chris@1517: TYPE_BLOCKED = "blocked" Chris@1517: TYPE_PRECEDES = "precedes" Chris@1517: TYPE_FOLLOWS = "follows" Chris@1517: TYPE_COPIED_TO = "copied_to" Chris@1517: TYPE_COPIED_FROM = "copied_from" Chris@1517: Chris@1517: TYPES = { Chris@1517: TYPE_RELATES => { :name => :label_relates_to, :sym_name => :label_relates_to, Chris@1517: :order => 1, :sym => TYPE_RELATES }, Chris@1517: TYPE_DUPLICATES => { :name => :label_duplicates, :sym_name => :label_duplicated_by, Chris@1517: :order => 2, :sym => TYPE_DUPLICATED }, Chris@1517: TYPE_DUPLICATED => { :name => :label_duplicated_by, :sym_name => :label_duplicates, Chris@1517: :order => 3, :sym => TYPE_DUPLICATES, :reverse => TYPE_DUPLICATES }, Chris@1517: TYPE_BLOCKS => { :name => :label_blocks, :sym_name => :label_blocked_by, Chris@1517: :order => 4, :sym => TYPE_BLOCKED }, Chris@1517: TYPE_BLOCKED => { :name => :label_blocked_by, :sym_name => :label_blocks, Chris@1517: :order => 5, :sym => TYPE_BLOCKS, :reverse => TYPE_BLOCKS }, Chris@1517: TYPE_PRECEDES => { :name => :label_precedes, :sym_name => :label_follows, Chris@1517: :order => 6, :sym => TYPE_FOLLOWS }, Chris@1517: TYPE_FOLLOWS => { :name => :label_follows, :sym_name => :label_precedes, Chris@1517: :order => 7, :sym => TYPE_PRECEDES, :reverse => TYPE_PRECEDES }, Chris@1517: TYPE_COPIED_TO => { :name => :label_copied_to, :sym_name => :label_copied_from, Chris@1517: :order => 8, :sym => TYPE_COPIED_FROM }, Chris@1517: TYPE_COPIED_FROM => { :name => :label_copied_from, :sym_name => :label_copied_to, Chris@1517: :order => 9, :sym => TYPE_COPIED_TO, :reverse => TYPE_COPIED_TO } Chris@1517: }.freeze Chris@1517: Chris@1517: validates_presence_of :issue_from, :issue_to, :relation_type Chris@1517: validates_inclusion_of :relation_type, :in => TYPES.keys Chris@1517: validates_numericality_of :delay, :allow_nil => true Chris@1517: validates_uniqueness_of :issue_to_id, :scope => :issue_from_id Chris@1517: validate :validate_issue_relation Chris@1517: Chris@1517: attr_protected :issue_from_id, :issue_to_id Chris@1517: before_save :handle_issue_order Chris@1517: after_create :create_journal_after_create Chris@1517: after_destroy :create_journal_after_delete Chris@1517: Chris@1517: def visible?(user=User.current) Chris@1517: (issue_from.nil? || issue_from.visible?(user)) && (issue_to.nil? || issue_to.visible?(user)) Chris@1517: end Chris@1517: Chris@1517: def deletable?(user=User.current) Chris@1517: visible?(user) && Chris@1517: ((issue_from.nil? || user.allowed_to?(:manage_issue_relations, issue_from.project)) || Chris@1517: (issue_to.nil? || user.allowed_to?(:manage_issue_relations, issue_to.project))) Chris@1517: end Chris@1517: Chris@1517: def initialize(attributes=nil, *args) Chris@1517: super Chris@1517: if new_record? Chris@1517: if relation_type.blank? Chris@1517: self.relation_type = IssueRelation::TYPE_RELATES Chris@1517: end Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def validate_issue_relation Chris@1517: if issue_from && issue_to Chris@1517: errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id Chris@1517: unless issue_from.project_id == issue_to.project_id || Chris@1517: Setting.cross_project_issue_relations? Chris@1517: errors.add :issue_to_id, :not_same_project Chris@1517: end Chris@1517: # detect circular dependencies depending wether the relation should be reversed Chris@1517: if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse] Chris@1517: errors.add :base, :circular_dependency if issue_from.all_dependent_issues.include? issue_to Chris@1517: else Chris@1517: errors.add :base, :circular_dependency if issue_to.all_dependent_issues.include? issue_from Chris@1517: end Chris@1517: if issue_from.is_descendant_of?(issue_to) || issue_from.is_ancestor_of?(issue_to) Chris@1517: errors.add :base, :cant_link_an_issue_with_a_descendant Chris@1517: end Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def other_issue(issue) Chris@1517: (self.issue_from_id == issue.id) ? issue_to : issue_from Chris@1517: end Chris@1517: Chris@1517: # Returns the relation type for +issue+ Chris@1517: def relation_type_for(issue) Chris@1517: if TYPES[relation_type] Chris@1517: if self.issue_from_id == issue.id Chris@1517: relation_type Chris@1517: else Chris@1517: TYPES[relation_type][:sym] Chris@1517: end Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def label_for(issue) Chris@1517: TYPES[relation_type] ? Chris@1517: TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] : Chris@1517: :unknow Chris@1517: end Chris@1517: Chris@1517: def css_classes_for(issue) Chris@1517: "rel-#{relation_type_for(issue)}" Chris@1517: end Chris@1517: Chris@1517: def handle_issue_order Chris@1517: reverse_if_needed Chris@1517: Chris@1517: if TYPE_PRECEDES == relation_type Chris@1517: self.delay ||= 0 Chris@1517: else Chris@1517: self.delay = nil Chris@1517: end Chris@1517: set_issue_to_dates Chris@1517: end Chris@1517: Chris@1517: def set_issue_to_dates Chris@1517: soonest_start = self.successor_soonest_start Chris@1517: if soonest_start && issue_to Chris@1517: issue_to.reschedule_on!(soonest_start) Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def successor_soonest_start Chris@1517: if (TYPE_PRECEDES == self.relation_type) && delay && issue_from && Chris@1517: (issue_from.start_date || issue_from.due_date) Chris@1517: (issue_from.due_date || issue_from.start_date) + 1 + delay Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def <=>(relation) Chris@1517: r = TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order] Chris@1517: r == 0 ? id <=> relation.id : r Chris@1517: end Chris@1517: Chris@1517: private Chris@1517: Chris@1517: # Reverses the relation if needed so that it gets stored in the proper way Chris@1517: # Should not be reversed before validation so that it can be displayed back Chris@1517: # as entered on new relation form Chris@1517: def reverse_if_needed Chris@1517: if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse] Chris@1517: issue_tmp = issue_to Chris@1517: self.issue_to = issue_from Chris@1517: self.issue_from = issue_tmp Chris@1517: self.relation_type = TYPES[relation_type][:reverse] Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def create_journal_after_create Chris@1517: journal = issue_from.init_journal(User.current) Chris@1517: journal.details << JournalDetail.new(:property => 'relation', Chris@1517: :prop_key => relation_type_for(issue_from), Chris@1517: :value => issue_to.id) Chris@1517: journal.save Chris@1517: journal = issue_to.init_journal(User.current) Chris@1517: journal.details << JournalDetail.new(:property => 'relation', Chris@1517: :prop_key => relation_type_for(issue_to), Chris@1517: :value => issue_from.id) Chris@1517: journal.save Chris@1517: end Chris@1517: Chris@1517: def create_journal_after_delete Chris@1517: journal = issue_from.init_journal(User.current) Chris@1517: journal.details << JournalDetail.new(:property => 'relation', Chris@1517: :prop_key => relation_type_for(issue_from), Chris@1517: :old_value => issue_to.id) Chris@1517: journal.save Chris@1517: journal = issue_to.init_journal(User.current) Chris@1517: journal.details << JournalDetail.new(:property => 'relation', Chris@1517: :prop_key => relation_type_for(issue_to), Chris@1517: :old_value => issue_from.id) Chris@1517: journal.save Chris@1517: end Chris@1517: end