kanbanworkflowstimelinescrumrubyroadmapproject-planningproject-managementopenprojectangularissue-trackerifcgantt-chartganttbug-trackerboardsbcf
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
5.1 KiB
139 lines
5.1 KiB
13 years ago
|
#-- encoding: UTF-8
|
||
14 years ago
|
#-- copyright
|
||
12 years ago
|
# OpenProject is a project management system.
|
||
11 years ago
|
# Copyright (C) 2012-2013 the OpenProject Foundation (OPF)
|
||
14 years ago
|
#
|
||
14 years ago
|
# This program is free software; you can redistribute it and/or
|
||
12 years ago
|
# modify it under the terms of the GNU General Public License version 3.
|
||
14 years ago
|
#
|
||
11 years ago
|
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
|
||
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
||
|
# Copyright (C) 2010-2013 the ChiliProject Team
|
||
|
#
|
||
|
# This program is free software; you can redistribute it and/or
|
||
|
# modify it under the terms of the GNU General Public License
|
||
|
# as published by the Free Software Foundation; either version 2
|
||
|
# of the License, or (at your option) any later version.
|
||
|
#
|
||
|
# This program is distributed in the hope that it will be useful,
|
||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
# GNU General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU General Public License
|
||
|
# along with this program; if not, write to the Free Software
|
||
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
|
#
|
||
14 years ago
|
# See doc/COPYRIGHT.rdoc for more details.
|
||
|
#++
|
||
|
|
||
18 years ago
|
class IssueRelation < ActiveRecord::Base
|
||
11 years ago
|
belongs_to :from, :class_name => 'WorkPackage', :foreign_key => 'from_id'
|
||
11 years ago
|
belongs_to :to, :class_name => 'WorkPackage', :foreign_key => 'to_id'
|
||
14 years ago
|
|
||
11 years ago
|
scope :of_issue, ->(issue) { where('from_id = ? OR to_id = ?', issue, issue) }
|
||
12 years ago
|
|
||
18 years ago
|
TYPE_RELATES = "relates"
|
||
|
TYPE_DUPLICATES = "duplicates"
|
||
15 years ago
|
TYPE_DUPLICATED = "duplicated"
|
||
18 years ago
|
TYPE_BLOCKS = "blocks"
|
||
15 years ago
|
TYPE_BLOCKED = "blocked"
|
||
18 years ago
|
TYPE_PRECEDES = "precedes"
|
||
15 years ago
|
TYPE_FOLLOWS = "follows"
|
||
14 years ago
|
|
||
15 years ago
|
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 }
|
||
18 years ago
|
}.freeze
|
||
14 years ago
|
|
||
11 years ago
|
validates_presence_of :from, :to, :relation_type
|
||
15 years ago
|
validates_inclusion_of :relation_type, :in => TYPES.keys
|
||
18 years ago
|
validates_numericality_of :delay, :allow_nil => true
|
||
11 years ago
|
validates_uniqueness_of :to_id, :scope => :from_id
|
||
14 years ago
|
|
||
12 years ago
|
validate :validate_sanity_of_relation
|
||
|
|
||
12 years ago
|
before_save :update_schedule
|
||
|
|
||
11 years ago
|
attr_protected :from_id, :to_id
|
||
14 years ago
|
|
||
12 years ago
|
def validate_sanity_of_relation
|
||
11 years ago
|
if from && to
|
||
|
errors.add :to_id, :invalid if from_id == to_id
|
||
|
errors.add :to_id, :not_same_project unless from.project_id == to.project_id || Setting.cross_project_issue_relations?
|
||
|
errors.add :base, :circular_dependency if to.all_dependent_issues.include? from
|
||
|
errors.add :base, :cant_link_a_work_package_with_a_descendant if from.is_descendant_of?(to) || from.is_ancestor_of?(to)
|
||
18 years ago
|
end
|
||
|
end
|
||
14 years ago
|
|
||
18 years ago
|
def other_issue(issue)
|
||
11 years ago
|
(self.from_id == issue.id) ? to : from
|
||
18 years ago
|
end
|
||
14 years ago
|
|
||
15 years ago
|
# Returns the relation type for +issue+
|
||
|
def relation_type_for(issue)
|
||
|
if TYPES[relation_type]
|
||
11 years ago
|
if self.from_id == issue.id
|
||
15 years ago
|
relation_type
|
||
|
else
|
||
|
TYPES[relation_type][:sym]
|
||
|
end
|
||
|
end
|
||
|
end
|
||
14 years ago
|
|
||
18 years ago
|
def label_for(issue)
|
||
11 years ago
|
TYPES[relation_type] ? TYPES[relation_type][(self.from_id == issue.id) ? :name : :sym_name] : :unknow
|
||
18 years ago
|
end
|
||
14 years ago
|
|
||
12 years ago
|
def update_schedule
|
||
15 years ago
|
reverse_if_needed
|
||
14 years ago
|
|
||
18 years ago
|
if TYPE_PRECEDES == relation_type
|
||
|
self.delay ||= 0
|
||
|
else
|
||
|
self.delay = nil
|
||
|
end
|
||
11 years ago
|
set_dates_of_target
|
||
18 years ago
|
end
|
||
14 years ago
|
|
||
11 years ago
|
def set_dates_of_target
|
||
18 years ago
|
soonest_start = self.successor_soonest_start
|
||
11 years ago
|
if soonest_start && to
|
||
|
to.reschedule_after(soonest_start)
|
||
18 years ago
|
end
|
||
|
end
|
||
14 years ago
|
|
||
18 years ago
|
def successor_soonest_start
|
||
11 years ago
|
if (TYPE_PRECEDES == self.relation_type) && delay && from && (from.start_date || from.due_date)
|
||
|
(from.due_date || from.start_date) + 1 + delay
|
||
14 years ago
|
end
|
||
18 years ago
|
end
|
||
14 years ago
|
|
||
18 years ago
|
def <=>(relation)
|
||
|
TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order]
|
||
|
end
|
||
14 years ago
|
|
||
12 years ago
|
# delay is an attribute of IssueRelation but its getter is masked by delayed_job's #delay method
|
||
|
# here we overwrite dj's delay method with the one reading the attribute
|
||
|
# since we don't plan to use dj with IssueRelation objects, this should be fine
|
||
|
def delay
|
||
|
self[:delay]
|
||
|
end
|
||
|
|
||
15 years ago
|
private
|
||
14 years ago
|
|
||
15 years ago
|
# Reverses the relation if needed so that it gets stored in the proper way
|
||
15 years ago
|
def reverse_if_needed
|
||
15 years ago
|
if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
|
||
11 years ago
|
issue_tmp = to
|
||
|
self.to = from
|
||
11 years ago
|
self.from = issue_tmp
|
||
15 years ago
|
self.relation_type = TYPES[relation_type][:reverse]
|
||
15 years ago
|
end
|
||
|
end
|
||
18 years ago
|
end
|