diff app/models/issue_relation.rb @ 1115:433d4f72a19b redmine-2.2

Update to Redmine SVN revision 11137 on 2.2-stable branch
author Chris Cannam
date Mon, 07 Jan 2013 12:01:42 +0000
parents cbb26bc654de
children 622f24f53b42 261b3d9a4903
line wrap: on
line diff
--- a/app/models/issue_relation.rb	Wed Jun 27 14:54:18 2012 +0100
+++ b/app/models/issue_relation.rb	Mon Jan 07 12:01:42 2013 +0000
@@ -1,5 +1,5 @@
 # Redmine - project management software
-# Copyright (C) 2006-2011  Jean-Philippe Lang
+# Copyright (C) 2006-2012  Jean-Philippe Lang
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
@@ -15,6 +15,20 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
+# Class used to represent the relations of an issue
+class IssueRelations < Array
+  include Redmine::I18n
+
+  def initialize(issue, *args)
+    @issue = issue
+    super(*args)
+  end
+
+  def to_s(*args)
+    map {|relation| "#{l(relation.label_for(@issue))} ##{relation.other_issue(@issue).id}"}.join(', ')
+  end
+end
+
 class IssueRelation < ActiveRecord::Base
   belongs_to :issue_from, :class_name => 'Issue', :foreign_key => 'issue_from_id'
   belongs_to :issue_to, :class_name => 'Issue', :foreign_key => 'issue_to_id'
@@ -26,25 +40,37 @@
   TYPE_BLOCKED      = "blocked"
   TYPE_PRECEDES     = "precedes"
   TYPE_FOLLOWS      = "follows"
+  TYPE_COPIED_TO    = "copied_to"
+  TYPE_COPIED_FROM  = "copied_from"
 
-  TYPES = { TYPE_RELATES =>     { :name => :label_relates_to, :sym_name => :label_relates_to, :order => 1, :sym => TYPE_RELATES },
-            TYPE_DUPLICATES =>  { :name => :label_duplicates, :sym_name => :label_duplicated_by, :order => 2, :sym => TYPE_DUPLICATED },
-            TYPE_DUPLICATED =>  { :name => :label_duplicated_by, :sym_name => :label_duplicates, :order => 3, :sym => TYPE_DUPLICATES, :reverse => TYPE_DUPLICATES },
-            TYPE_BLOCKS =>      { :name => :label_blocks, :sym_name => :label_blocked_by, :order => 4, :sym => TYPE_BLOCKED },
-            TYPE_BLOCKED =>     { :name => :label_blocked_by, :sym_name => :label_blocks, :order => 5, :sym => TYPE_BLOCKS, :reverse => TYPE_BLOCKS },
-            TYPE_PRECEDES =>    { :name => :label_precedes, :sym_name => :label_follows, :order => 6, :sym => TYPE_FOLLOWS },
-            TYPE_FOLLOWS =>     { :name => :label_follows, :sym_name => :label_precedes, :order => 7, :sym => TYPE_PRECEDES, :reverse => TYPE_PRECEDES }
-          }.freeze
+  TYPES = {
+    TYPE_RELATES =>     { :name => :label_relates_to, :sym_name => :label_relates_to,
+                          :order => 1, :sym => TYPE_RELATES },
+    TYPE_DUPLICATES =>  { :name => :label_duplicates, :sym_name => :label_duplicated_by,
+                          :order => 2, :sym => TYPE_DUPLICATED },
+    TYPE_DUPLICATED =>  { :name => :label_duplicated_by, :sym_name => :label_duplicates,
+                          :order => 3, :sym => TYPE_DUPLICATES, :reverse => TYPE_DUPLICATES },
+    TYPE_BLOCKS =>      { :name => :label_blocks, :sym_name => :label_blocked_by,
+                          :order => 4, :sym => TYPE_BLOCKED },
+    TYPE_BLOCKED =>     { :name => :label_blocked_by, :sym_name => :label_blocks,
+                          :order => 5, :sym => TYPE_BLOCKS, :reverse => TYPE_BLOCKS },
+    TYPE_PRECEDES =>    { :name => :label_precedes, :sym_name => :label_follows,
+                          :order => 6, :sym => TYPE_FOLLOWS },
+    TYPE_FOLLOWS =>     { :name => :label_follows, :sym_name => :label_precedes,
+                          :order => 7, :sym => TYPE_PRECEDES, :reverse => TYPE_PRECEDES },
+    TYPE_COPIED_TO =>   { :name => :label_copied_to, :sym_name => :label_copied_from,
+                          :order => 8, :sym => TYPE_COPIED_FROM },
+    TYPE_COPIED_FROM => { :name => :label_copied_from, :sym_name => :label_copied_to,
+                          :order => 9, :sym => TYPE_COPIED_TO, :reverse => TYPE_COPIED_TO }
+  }.freeze
 
   validates_presence_of :issue_from, :issue_to, :relation_type
   validates_inclusion_of :relation_type, :in => TYPES.keys
   validates_numericality_of :delay, :allow_nil => true
   validates_uniqueness_of :issue_to_id, :scope => :issue_from_id
-
   validate :validate_issue_relation
 
   attr_protected :issue_from_id, :issue_to_id
-
   before_save :handle_issue_order
 
   def visible?(user=User.current)
@@ -57,7 +83,8 @@
         (issue_to.nil? || user.allowed_to?(:manage_issue_relations, issue_to.project)))
   end
 
-  def after_initialize
+  def initialize(attributes=nil, *args)
+    super
     if new_record?
       if relation_type.blank?
         self.relation_type = IssueRelation::TYPE_RELATES
@@ -68,14 +95,19 @@
   def validate_issue_relation
     if issue_from && issue_to
       errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id
-      errors.add :issue_to_id, :not_same_project unless issue_from.project_id == issue_to.project_id || Setting.cross_project_issue_relations?
-      #detect circular dependencies depending wether the relation should be reversed
+      unless issue_from.project_id == issue_to.project_id ||
+                Setting.cross_project_issue_relations?
+        errors.add :issue_to_id, :not_same_project
+      end
+      # detect circular dependencies depending wether the relation should be reversed
       if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
         errors.add :base, :circular_dependency if issue_from.all_dependent_issues.include? issue_to
       else
         errors.add :base, :circular_dependency if issue_to.all_dependent_issues.include? issue_from
       end
-      errors.add :base, :cant_link_an_issue_with_a_descendant if issue_from.is_descendant_of?(issue_to) || issue_from.is_ancestor_of?(issue_to)
+      if issue_from.is_descendant_of?(issue_to) || issue_from.is_ancestor_of?(issue_to)
+        errors.add :base, :cant_link_an_issue_with_a_descendant
+      end
     end
   end
 
@@ -95,7 +127,13 @@
   end
 
   def label_for(issue)
-    TYPES[relation_type] ? TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] : :unknow
+    TYPES[relation_type] ?
+        TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] :
+        :unknow
+  end
+
+  def css_classes_for(issue)
+    "rel-#{relation_type_for(issue)}"
   end
 
   def handle_issue_order
@@ -112,18 +150,20 @@
   def set_issue_to_dates
     soonest_start = self.successor_soonest_start
     if soonest_start && issue_to
-      issue_to.reschedule_after(soonest_start)
+      issue_to.reschedule_on!(soonest_start)
     end
   end
 
   def successor_soonest_start
-    if (TYPE_PRECEDES == self.relation_type) && delay && issue_from && (issue_from.start_date || issue_from.due_date)
+    if (TYPE_PRECEDES == self.relation_type) && delay && issue_from &&
+           (issue_from.start_date || issue_from.due_date)
       (issue_from.due_date || issue_from.start_date) + 1 + delay
     end
   end
 
   def <=>(relation)
-    TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order]
+    r = TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order]
+    r == 0 ? id <=> relation.id : r
   end
 
   private