To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / app / models / issue_relation.rb @ 1298:4f746d8966dd
History | View | Annotate | Download (6.49 KB)
| 1 | 909:cbb26bc654de | Chris | # Redmine - project management software
|
|---|---|---|---|
| 2 | 1295:622f24f53b42 | Chris | # Copyright (C) 2006-2013 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 | 1295:622f24f53b42 | 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 | 1295:622f24f53b42 | 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 | |||
| 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 | 1115:433d4f72a19b | Chris | def initialize(attributes=nil, *args) |
| 87 | super
|
||
| 88 | 909:cbb26bc654de | Chris | 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 | 0:513646585e45 | Chris | if issue_from && issue_to
|
| 97 | errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id |
||
| 98 | 1115:433d4f72a19b | Chris | 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 | 507:0c939c159af4 | Chris | if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse] |
| 104 | 909:cbb26bc654de | Chris | errors.add :base, :circular_dependency if issue_from.all_dependent_issues.include? issue_to |
| 105 | 507:0c939c159af4 | Chris | else
|
| 106 | 909:cbb26bc654de | Chris | errors.add :base, :circular_dependency if issue_to.all_dependent_issues.include? issue_from |
| 107 | 507:0c939c159af4 | Chris | end
|
| 108 | 1115:433d4f72a19b | Chris | 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 | 0:513646585e45 | Chris | end
|
| 112 | end
|
||
| 113 | 909:cbb26bc654de | Chris | |
| 114 | 0:513646585e45 | Chris | def other_issue(issue) |
| 115 | (self.issue_from_id == issue.id) ? issue_to : issue_from
|
||
| 116 | end
|
||
| 117 | 909:cbb26bc654de | Chris | |
| 118 | 0:513646585e45 | Chris | # 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 | 909:cbb26bc654de | Chris | |
| 129 | 0:513646585e45 | Chris | def label_for(issue) |
| 130 | 1115:433d4f72a19b | Chris | 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 | 0:513646585e45 | Chris | end
|
| 138 | 909:cbb26bc654de | Chris | |
| 139 | def handle_issue_order |
||
| 140 | 0:513646585e45 | Chris | reverse_if_needed |
| 141 | 909:cbb26bc654de | Chris | |
| 142 | 0:513646585e45 | Chris | 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 | 909:cbb26bc654de | Chris | |
| 150 | 0:513646585e45 | Chris | def set_issue_to_dates |
| 151 | soonest_start = self.successor_soonest_start
|
||
| 152 | 119:8661b858af72 | Chris | if soonest_start && issue_to
|
| 153 | 1115:433d4f72a19b | Chris | issue_to.reschedule_on!(soonest_start) |
| 154 | 0:513646585e45 | Chris | end
|
| 155 | end
|
||
| 156 | 909:cbb26bc654de | Chris | |
| 157 | 0:513646585e45 | Chris | def successor_soonest_start |
| 158 | 1115:433d4f72a19b | Chris | if (TYPE_PRECEDES == self.relation_type) && delay && issue_from && |
| 159 | (issue_from.start_date || issue_from.due_date) |
||
| 160 | 119:8661b858af72 | Chris | (issue_from.due_date || issue_from.start_date) + 1 + delay
|
| 161 | end
|
||
| 162 | 0:513646585e45 | Chris | end
|
| 163 | 909:cbb26bc654de | Chris | |
| 164 | 0:513646585e45 | Chris | def <=>(relation) |
| 165 | 1115:433d4f72a19b | Chris | r = TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order] |
| 166 | r == 0 ? id <=> relation.id : r
|
||
| 167 | 0:513646585e45 | Chris | end
|
| 168 | 909:cbb26bc654de | Chris | |
| 169 | 0:513646585e45 | Chris | private |
| 170 | 909:cbb26bc654de | Chris | |
| 171 | 0:513646585e45 | Chris | # Reverses the relation if needed so that it gets stored in the proper way
|
| 172 | 909:cbb26bc654de | Chris | # Should not be reversed before validation so that it can be displayed back
|
| 173 | # as entered on new relation form
|
||
| 174 | 0:513646585e45 | Chris | 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 |