To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / models / issue_relation.rb @ 1361:7c0909052511

History | View | Annotate | Download (6.48 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2012  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 used to represent the relations of an issue
19
class IssueRelations < Array
20
  include Redmine::I18n
21

    
22
  def initialize(issue, *args)
23
    @issue = issue
24
    super(*args)
25
  end
26

    
27
  def to_s(*args)
28
    map {|relation| "#{l(relation.label_for(@issue))} ##{relation.other_issue(@issue).id}"}.join(', ')
29
  end
30
end
31

    
32
class IssueRelation < ActiveRecord::Base
33
  belongs_to :issue_from, :class_name => 'Issue', :foreign_key => 'issue_from_id'
34
  belongs_to :issue_to, :class_name => 'Issue', :foreign_key => 'issue_to_id'
35

    
36
  TYPE_RELATES      = "relates"
37
  TYPE_DUPLICATES   = "duplicates"
38
  TYPE_DUPLICATED   = "duplicated"
39
  TYPE_BLOCKS       = "blocks"
40
  TYPE_BLOCKED      = "blocked"
41
  TYPE_PRECEDES     = "precedes"
42
  TYPE_FOLLOWS      = "follows"
43
  TYPE_COPIED_TO    = "copied_to"
44
  TYPE_COPIED_FROM  = "copied_from"
45

    
46
  TYPES = {
47
    TYPE_RELATES =>     { :name => :label_relates_to, :sym_name => :label_relates_to,
48
                          :order => 1, :sym => TYPE_RELATES },
49
    TYPE_DUPLICATES =>  { :name => :label_duplicates, :sym_name => :label_duplicated_by,
50
                          :order => 2, :sym => TYPE_DUPLICATED },
51
    TYPE_DUPLICATED =>  { :name => :label_duplicated_by, :sym_name => :label_duplicates,
52
                          :order => 3, :sym => TYPE_DUPLICATES, :reverse => TYPE_DUPLICATES },
53
    TYPE_BLOCKS =>      { :name => :label_blocks, :sym_name => :label_blocked_by,
54
                          :order => 4, :sym => TYPE_BLOCKED },
55
    TYPE_BLOCKED =>     { :name => :label_blocked_by, :sym_name => :label_blocks,
56
                          :order => 5, :sym => TYPE_BLOCKS, :reverse => TYPE_BLOCKS },
57
    TYPE_PRECEDES =>    { :name => :label_precedes, :sym_name => :label_follows,
58
                          :order => 6, :sym => TYPE_FOLLOWS },
59
    TYPE_FOLLOWS =>     { :name => :label_follows, :sym_name => :label_precedes,
60
                          :order => 7, :sym => TYPE_PRECEDES, :reverse => TYPE_PRECEDES },
61
    TYPE_COPIED_TO =>   { :name => :label_copied_to, :sym_name => :label_copied_from,
62
                          :order => 8, :sym => TYPE_COPIED_FROM },
63
    TYPE_COPIED_FROM => { :name => :label_copied_from, :sym_name => :label_copied_to,
64
                          :order => 9, :sym => TYPE_COPIED_TO, :reverse => TYPE_COPIED_TO }
65
  }.freeze
66

    
67
  validates_presence_of :issue_from, :issue_to, :relation_type
68
  validates_inclusion_of :relation_type, :in => TYPES.keys
69
  validates_numericality_of :delay, :allow_nil => true
70
  validates_uniqueness_of :issue_to_id, :scope => :issue_from_id
71
  validate :validate_issue_relation
72

    
73
  attr_protected :issue_from_id, :issue_to_id
74
  before_save :handle_issue_order
75

    
76
  def visible?(user=User.current)
77
    (issue_from.nil? || issue_from.visible?(user)) && (issue_to.nil? || issue_to.visible?(user))
78
  end
79

    
80
  def deletable?(user=User.current)
81
    visible?(user) &&
82
      ((issue_from.nil? || user.allowed_to?(:manage_issue_relations, issue_from.project)) ||
83
        (issue_to.nil? || user.allowed_to?(:manage_issue_relations, issue_to.project)))
84
  end
85

    
86
  def initialize(attributes=nil, *args)
87
    super
88
    if new_record?
89
      if relation_type.blank?
90
        self.relation_type = IssueRelation::TYPE_RELATES
91
      end
92
    end
93
  end
94

    
95
  def validate_issue_relation
96
    if issue_from && issue_to
97
      errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id
98
      unless issue_from.project_id == issue_to.project_id ||
99
                Setting.cross_project_issue_relations?
100
        errors.add :issue_to_id, :not_same_project
101
      end
102
      # detect circular dependencies depending wether the relation should be reversed
103
      if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
104
        errors.add :base, :circular_dependency if issue_from.all_dependent_issues.include? issue_to
105
      else
106
        errors.add :base, :circular_dependency if issue_to.all_dependent_issues.include? issue_from
107
      end
108
      if issue_from.is_descendant_of?(issue_to) || issue_from.is_ancestor_of?(issue_to)
109
        errors.add :base, :cant_link_an_issue_with_a_descendant
110
      end
111
    end
112
  end
113

    
114
  def other_issue(issue)
115
    (self.issue_from_id == issue.id) ? issue_to : issue_from
116
  end
117

    
118
  # Returns the relation type for +issue+
119
  def relation_type_for(issue)
120
    if TYPES[relation_type]
121
      if self.issue_from_id == issue.id
122
        relation_type
123
      else
124
        TYPES[relation_type][:sym]
125
      end
126
    end
127
  end
128

    
129
  def label_for(issue)
130
    TYPES[relation_type] ?
131
        TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] :
132
        :unknow
133
  end
134

    
135
  def css_classes_for(issue)
136
    "rel-#{relation_type_for(issue)}"
137
  end
138

    
139
  def handle_issue_order
140
    reverse_if_needed
141

    
142
    if TYPE_PRECEDES == relation_type
143
      self.delay ||= 0
144
    else
145
      self.delay = nil
146
    end
147
    set_issue_to_dates
148
  end
149

    
150
  def set_issue_to_dates
151
    soonest_start = self.successor_soonest_start
152
    if soonest_start && issue_to
153
      issue_to.reschedule_on!(soonest_start)
154
    end
155
  end
156

    
157
  def successor_soonest_start
158
    if (TYPE_PRECEDES == self.relation_type) && delay && issue_from &&
159
           (issue_from.start_date || issue_from.due_date)
160
      (issue_from.due_date || issue_from.start_date) + 1 + delay
161
    end
162
  end
163

    
164
  def <=>(relation)
165
    r = TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order]
166
    r == 0 ? id <=> relation.id : r
167
  end
168

    
169
  private
170

    
171
  # Reverses the relation if needed so that it gets stored in the proper way
172
  # Should not be reversed before validation so that it can be displayed back
173
  # as entered on new relation form
174
  def reverse_if_needed
175
    if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
176
      issue_tmp = issue_to
177
      self.issue_to = issue_from
178
      self.issue_from = issue_tmp
179
      self.relation_type = TYPES[relation_type][:reverse]
180
    end
181
  end
182
end