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 @ 1533:59e13100ea95

History | View | Annotate | Download (7.78 KB)

1 909:cbb26bc654de Chris
# Redmine - project management software
2 1494:e248c7af89ec Chris
# Copyright (C) 2006-2014  Jean-Philippe Lang
3 0:513646585e45 Chris
#
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 909:cbb26bc654de Chris
#
9 0:513646585e45 Chris
# 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 909:cbb26bc654de Chris
#
14 0:513646585e45 Chris
# 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 1464:261b3d9a4903 Chris
class IssueRelation < ActiveRecord::Base
19
  # Class used to represent the relations of an issue
20
  class Relations < Array
21
    include Redmine::I18n
22 1115:433d4f72a19b Chris
23 1464:261b3d9a4903 Chris
    def initialize(issue, *args)
24
      @issue = issue
25
      super(*args)
26
    end
27
28
    def to_s(*args)
29
      map {|relation| "#{l(relation.label_for(@issue))} ##{relation.other_issue(@issue).id}"}.join(', ')
30
    end
31 1115:433d4f72a19b Chris
  end
32
33 0:513646585e45 Chris
  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 909:cbb26bc654de Chris
36 0:513646585e45 Chris
  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 1115:433d4f72a19b Chris
  TYPE_COPIED_TO    = "copied_to"
44
  TYPE_COPIED_FROM  = "copied_from"
45 909:cbb26bc654de Chris
46 1115:433d4f72a19b Chris
  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 909:cbb26bc654de Chris
67 0:513646585e45 Chris
  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 909:cbb26bc654de Chris
  validate :validate_issue_relation
72
73 0:513646585e45 Chris
  attr_protected :issue_from_id, :issue_to_id
74 909:cbb26bc654de Chris
  before_save :handle_issue_order
75 1464:261b3d9a4903 Chris
  after_create  :create_journal_after_create
76
  after_destroy :create_journal_after_delete
77 909:cbb26bc654de Chris
78
  def visible?(user=User.current)
79
    (issue_from.nil? || issue_from.visible?(user)) && (issue_to.nil? || issue_to.visible?(user))
80
  end
81
82
  def deletable?(user=User.current)
83
    visible?(user) &&
84
      ((issue_from.nil? || user.allowed_to?(:manage_issue_relations, issue_from.project)) ||
85
        (issue_to.nil? || user.allowed_to?(:manage_issue_relations, issue_to.project)))
86
  end
87
88 1115:433d4f72a19b Chris
  def initialize(attributes=nil, *args)
89
    super
90 909:cbb26bc654de Chris
    if new_record?
91
      if relation_type.blank?
92
        self.relation_type = IssueRelation::TYPE_RELATES
93
      end
94
    end
95
  end
96
97
  def validate_issue_relation
98 0:513646585e45 Chris
    if issue_from && issue_to
99
      errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id
100 1115:433d4f72a19b Chris
      unless issue_from.project_id == issue_to.project_id ||
101
                Setting.cross_project_issue_relations?
102
        errors.add :issue_to_id, :not_same_project
103
      end
104
      # detect circular dependencies depending wether the relation should be reversed
105 507:0c939c159af4 Chris
      if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
106 909:cbb26bc654de Chris
        errors.add :base, :circular_dependency if issue_from.all_dependent_issues.include? issue_to
107 507:0c939c159af4 Chris
      else
108 909:cbb26bc654de Chris
        errors.add :base, :circular_dependency if issue_to.all_dependent_issues.include? issue_from
109 507:0c939c159af4 Chris
      end
110 1115:433d4f72a19b Chris
      if issue_from.is_descendant_of?(issue_to) || issue_from.is_ancestor_of?(issue_to)
111
        errors.add :base, :cant_link_an_issue_with_a_descendant
112
      end
113 0:513646585e45 Chris
    end
114
  end
115 909:cbb26bc654de Chris
116 0:513646585e45 Chris
  def other_issue(issue)
117
    (self.issue_from_id == issue.id) ? issue_to : issue_from
118
  end
119 909:cbb26bc654de Chris
120 0:513646585e45 Chris
  # Returns the relation type for +issue+
121
  def relation_type_for(issue)
122
    if TYPES[relation_type]
123
      if self.issue_from_id == issue.id
124
        relation_type
125
      else
126
        TYPES[relation_type][:sym]
127
      end
128
    end
129
  end
130 909:cbb26bc654de Chris
131 0:513646585e45 Chris
  def label_for(issue)
132 1115:433d4f72a19b Chris
    TYPES[relation_type] ?
133
        TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] :
134
        :unknow
135
  end
136
137
  def css_classes_for(issue)
138
    "rel-#{relation_type_for(issue)}"
139 0:513646585e45 Chris
  end
140 909:cbb26bc654de Chris
141
  def handle_issue_order
142 0:513646585e45 Chris
    reverse_if_needed
143 909:cbb26bc654de Chris
144 0:513646585e45 Chris
    if TYPE_PRECEDES == relation_type
145
      self.delay ||= 0
146
    else
147
      self.delay = nil
148
    end
149
    set_issue_to_dates
150
  end
151 909:cbb26bc654de Chris
152 0:513646585e45 Chris
  def set_issue_to_dates
153
    soonest_start = self.successor_soonest_start
154 119:8661b858af72 Chris
    if soonest_start && issue_to
155 1115:433d4f72a19b Chris
      issue_to.reschedule_on!(soonest_start)
156 0:513646585e45 Chris
    end
157
  end
158 909:cbb26bc654de Chris
159 0:513646585e45 Chris
  def successor_soonest_start
160 1115:433d4f72a19b Chris
    if (TYPE_PRECEDES == self.relation_type) && delay && issue_from &&
161
           (issue_from.start_date || issue_from.due_date)
162 119:8661b858af72 Chris
      (issue_from.due_date || issue_from.start_date) + 1 + delay
163
    end
164 0:513646585e45 Chris
  end
165 909:cbb26bc654de Chris
166 0:513646585e45 Chris
  def <=>(relation)
167 1115:433d4f72a19b Chris
    r = TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order]
168
    r == 0 ? id <=> relation.id : r
169 0:513646585e45 Chris
  end
170 909:cbb26bc654de Chris
171 0:513646585e45 Chris
  private
172 909:cbb26bc654de Chris
173 0:513646585e45 Chris
  # Reverses the relation if needed so that it gets stored in the proper way
174 909:cbb26bc654de Chris
  # Should not be reversed before validation so that it can be displayed back
175
  # as entered on new relation form
176 0:513646585e45 Chris
  def reverse_if_needed
177
    if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
178
      issue_tmp = issue_to
179
      self.issue_to = issue_from
180
      self.issue_from = issue_tmp
181
      self.relation_type = TYPES[relation_type][:reverse]
182
    end
183
  end
184 1464:261b3d9a4903 Chris
185
  def create_journal_after_create
186
    journal = issue_from.init_journal(User.current)
187
    journal.details << JournalDetail.new(:property => 'relation',
188 1517:dffacf8a6908 Chris
                                         :prop_key => relation_type_for(issue_from),
189 1464:261b3d9a4903 Chris
                                         :value    => issue_to.id)
190
    journal.save
191
    journal = issue_to.init_journal(User.current)
192
    journal.details << JournalDetail.new(:property => 'relation',
193 1517:dffacf8a6908 Chris
                                         :prop_key => relation_type_for(issue_to),
194 1464:261b3d9a4903 Chris
                                         :value    => issue_from.id)
195
    journal.save
196
  end
197
198
  def create_journal_after_delete
199
    journal = issue_from.init_journal(User.current)
200
    journal.details << JournalDetail.new(:property  => 'relation',
201 1517:dffacf8a6908 Chris
                                         :prop_key  => relation_type_for(issue_from),
202 1464:261b3d9a4903 Chris
                                         :old_value => issue_to.id)
203
    journal.save
204
    journal = issue_to.init_journal(User.current)
205
    journal.details << JournalDetail.new(:property  => 'relation',
206 1517:dffacf8a6908 Chris
                                         :prop_key  => relation_type_for(issue_to),
207 1464:261b3d9a4903 Chris
                                         :old_value => issue_from.id)
208
    journal.save
209
  end
210 0:513646585e45 Chris
end