issue_relation.rb

sunday walker, 2008-02-11 14:16

Download (3.42 KB)

 
1
# redMine - project management software
2
# Copyright (C) 2006-2007  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
# 
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
# 
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

    
18
class IssueRelation < ActiveRecord::Base
19
  belongs_to :issue_from, :class_name => 'Issue', :foreign_key => 'issue_from_id'
20
  belongs_to :issue_to, :class_name => 'Issue', :foreign_key => 'issue_to_id'
21
  
22
  TYPE_RELATES      = "relates"
23
  TYPE_DUPLICATES   = "duplicates"
24
  TYPE_BLOCKS       = "blocks"
25
  TYPE_PRECEDES     = "precedes"
26
  TYPE_PARENTS     = "parents"
27
  
28
  TYPES = { TYPE_RELATES =>     { :name => :label_relates_to, :sym_name => :label_relates_to, :order => 1 },
29
            TYPE_DUPLICATES =>  { :name => :label_duplicates, :sym_name => :label_duplicates, :order => 2 },
30
            TYPE_BLOCKS =>      { :name => :label_blocks, :sym_name => :label_blocked_by, :order => 3 },
31
            TYPE_PRECEDES =>    { :name => :label_precedes, :sym_name => :label_follows, :order => 4 },
32
            TYPE_PARENTS =>    { :name => :label_parents, :sym_name => :label_children, :order => 5 },
33
          }.freeze
34
  
35
  validates_presence_of :issue_from, :issue_to, :relation_type
36
  validates_inclusion_of :relation_type, :in => TYPES.keys
37
  validates_numericality_of :delay, :allow_nil => true
38
  validates_uniqueness_of :issue_to_id, :scope => :issue_from_id
39
  
40
  def validate
41
    if issue_from && issue_to
42
      errors.add :issue_to_id, :activerecord_error_invalid if issue_from_id == issue_to_id
43
      errors.add :issue_to_id, :activerecord_error_not_same_project unless issue_from.project_id == issue_to.project_id || Setting.cross_project_issue_relations?
44
      errors.add_to_base :activerecord_error_circular_dependency if issue_to.all_dependent_issues.include? issue_from
45
    end
46
  end
47
  
48
  def other_issue(issue)
49
    (self.issue_from_id == issue.id) ? issue_to : issue_from
50
  end
51
  
52
  def label_for(issue)
53
    TYPES[relation_type] ? TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] : :unknow
54
  end
55
  
56
  def before_save
57
    if TYPE_PRECEDES == relation_type
58
      self.delay ||= 0
59
    else
60
      self.delay = nil
61
    end
62
    set_issue_to_dates
63
  end
64
  
65
  def set_issue_to_dates
66
    soonest_start = self.successor_soonest_start
67
    if soonest_start && (!issue_to.start_date || issue_to.start_date < soonest_start)
68
      issue_to.start_date, issue_to.due_date = successor_soonest_start, successor_soonest_start + issue_to.duration
69
      issue_to.save
70
    end
71
  end
72
  
73
  def successor_soonest_start
74
    return nil unless (TYPE_PRECEDES == self.relation_type) && (issue_from.start_date || issue_from.due_date)
75
    (issue_from.due_date || issue_from.start_date) + 1 + delay
76
  end
77
  
78
  def <=>(relation)
79
    TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order]
80
  end
81
end