Chris@0: # redMine - project management software Chris@0: # Copyright (C) 2006-2007 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@0: # 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@0: # 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 IssueRelation < ActiveRecord::Base Chris@0: belongs_to :issue_from, :class_name => 'Issue', :foreign_key => 'issue_from_id' Chris@0: belongs_to :issue_to, :class_name => 'Issue', :foreign_key => 'issue_to_id' Chris@0: Chris@0: TYPE_RELATES = "relates" Chris@0: TYPE_DUPLICATES = "duplicates" Chris@0: TYPE_DUPLICATED = "duplicated" Chris@0: TYPE_BLOCKS = "blocks" Chris@0: TYPE_BLOCKED = "blocked" Chris@0: TYPE_PRECEDES = "precedes" Chris@0: TYPE_FOLLOWS = "follows" Chris@0: Chris@0: TYPES = { TYPE_RELATES => { :name => :label_relates_to, :sym_name => :label_relates_to, :order => 1, :sym => TYPE_RELATES }, Chris@0: TYPE_DUPLICATES => { :name => :label_duplicates, :sym_name => :label_duplicated_by, :order => 2, :sym => TYPE_DUPLICATED }, Chris@0: TYPE_DUPLICATED => { :name => :label_duplicated_by, :sym_name => :label_duplicates, :order => 3, :sym => TYPE_DUPLICATES, :reverse => TYPE_DUPLICATES }, Chris@0: TYPE_BLOCKS => { :name => :label_blocks, :sym_name => :label_blocked_by, :order => 4, :sym => TYPE_BLOCKED }, Chris@0: TYPE_BLOCKED => { :name => :label_blocked_by, :sym_name => :label_blocks, :order => 5, :sym => TYPE_BLOCKS, :reverse => TYPE_BLOCKS }, Chris@0: TYPE_PRECEDES => { :name => :label_precedes, :sym_name => :label_follows, :order => 6, :sym => TYPE_FOLLOWS }, Chris@0: TYPE_FOLLOWS => { :name => :label_follows, :sym_name => :label_precedes, :order => 7, :sym => TYPE_PRECEDES, :reverse => TYPE_PRECEDES } Chris@0: }.freeze Chris@0: Chris@0: validates_presence_of :issue_from, :issue_to, :relation_type Chris@0: validates_inclusion_of :relation_type, :in => TYPES.keys Chris@0: validates_numericality_of :delay, :allow_nil => true Chris@0: validates_uniqueness_of :issue_to_id, :scope => :issue_from_id Chris@0: Chris@0: attr_protected :issue_from_id, :issue_to_id Chris@0: Chris@0: def validate Chris@0: if issue_from && issue_to Chris@0: errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id Chris@0: errors.add :issue_to_id, :not_same_project unless issue_from.project_id == issue_to.project_id || Setting.cross_project_issue_relations? Chris@0: errors.add_to_base :circular_dependency if issue_to.all_dependent_issues.include? issue_from Chris@0: errors.add_to_base :cant_link_an_issue_with_a_descendant if issue_from.is_descendant_of?(issue_to) || issue_from.is_ancestor_of?(issue_to) Chris@0: end Chris@0: end Chris@0: Chris@0: def other_issue(issue) Chris@0: (self.issue_from_id == issue.id) ? issue_to : issue_from Chris@0: end Chris@0: Chris@0: # Returns the relation type for +issue+ Chris@0: def relation_type_for(issue) Chris@0: if TYPES[relation_type] Chris@0: if self.issue_from_id == issue.id Chris@0: relation_type Chris@0: else Chris@0: TYPES[relation_type][:sym] Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: def label_for(issue) Chris@0: TYPES[relation_type] ? TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] : :unknow Chris@0: end Chris@0: Chris@0: def before_save Chris@0: reverse_if_needed Chris@0: Chris@0: if TYPE_PRECEDES == relation_type Chris@0: self.delay ||= 0 Chris@0: else Chris@0: self.delay = nil Chris@0: end Chris@0: set_issue_to_dates Chris@0: end Chris@0: Chris@0: def set_issue_to_dates Chris@0: soonest_start = self.successor_soonest_start Chris@117: if soonest_start && issue_to Chris@0: issue_to.reschedule_after(soonest_start) Chris@0: end Chris@0: end Chris@0: Chris@0: def successor_soonest_start Chris@117: if (TYPE_PRECEDES == self.relation_type) && delay && issue_from && (issue_from.start_date || issue_from.due_date) Chris@117: (issue_from.due_date || issue_from.start_date) + 1 + delay Chris@117: end Chris@0: end Chris@0: Chris@0: def <=>(relation) Chris@0: TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order] Chris@0: end Chris@0: Chris@0: private Chris@0: Chris@0: # Reverses the relation if needed so that it gets stored in the proper way Chris@0: def reverse_if_needed Chris@0: if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse] Chris@0: issue_tmp = issue_to Chris@0: self.issue_to = issue_from Chris@0: self.issue_from = issue_tmp Chris@0: self.relation_type = TYPES[relation_type][:reverse] Chris@0: end Chris@0: end Chris@0: end