annotate .svn/pristine/41/41bb9cc50bdf02df6fa0e55ba5d0a45d87f52436.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
rev   line source
Chris@1295 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@1295 3 #
Chris@1295 4 # This program is free software; you can redistribute it and/or
Chris@1295 5 # modify it under the terms of the GNU General Public License
Chris@1295 6 # as published by the Free Software Foundation; either version 2
Chris@1295 7 # of the License, or (at your option) any later version.
Chris@1295 8 #
Chris@1295 9 # This program is distributed in the hope that it will be useful,
Chris@1295 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 12 # GNU General Public License for more details.
Chris@1295 13 #
Chris@1295 14 # You should have received a copy of the GNU General Public License
Chris@1295 15 # along with this program; if not, write to the Free Software
Chris@1295 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 17
Chris@1295 18 class IssueRelation < ActiveRecord::Base
Chris@1295 19 # Class used to represent the relations of an issue
Chris@1295 20 class Relations < Array
Chris@1295 21 include Redmine::I18n
Chris@1295 22
Chris@1295 23 def initialize(issue, *args)
Chris@1295 24 @issue = issue
Chris@1295 25 super(*args)
Chris@1295 26 end
Chris@1295 27
Chris@1295 28 def to_s(*args)
Chris@1295 29 map {|relation| "#{l(relation.label_for(@issue))} ##{relation.other_issue(@issue).id}"}.join(', ')
Chris@1295 30 end
Chris@1295 31 end
Chris@1295 32
Chris@1295 33 belongs_to :issue_from, :class_name => 'Issue', :foreign_key => 'issue_from_id'
Chris@1295 34 belongs_to :issue_to, :class_name => 'Issue', :foreign_key => 'issue_to_id'
Chris@1295 35
Chris@1295 36 TYPE_RELATES = "relates"
Chris@1295 37 TYPE_DUPLICATES = "duplicates"
Chris@1295 38 TYPE_DUPLICATED = "duplicated"
Chris@1295 39 TYPE_BLOCKS = "blocks"
Chris@1295 40 TYPE_BLOCKED = "blocked"
Chris@1295 41 TYPE_PRECEDES = "precedes"
Chris@1295 42 TYPE_FOLLOWS = "follows"
Chris@1295 43 TYPE_COPIED_TO = "copied_to"
Chris@1295 44 TYPE_COPIED_FROM = "copied_from"
Chris@1295 45
Chris@1295 46 TYPES = {
Chris@1295 47 TYPE_RELATES => { :name => :label_relates_to, :sym_name => :label_relates_to,
Chris@1295 48 :order => 1, :sym => TYPE_RELATES },
Chris@1295 49 TYPE_DUPLICATES => { :name => :label_duplicates, :sym_name => :label_duplicated_by,
Chris@1295 50 :order => 2, :sym => TYPE_DUPLICATED },
Chris@1295 51 TYPE_DUPLICATED => { :name => :label_duplicated_by, :sym_name => :label_duplicates,
Chris@1295 52 :order => 3, :sym => TYPE_DUPLICATES, :reverse => TYPE_DUPLICATES },
Chris@1295 53 TYPE_BLOCKS => { :name => :label_blocks, :sym_name => :label_blocked_by,
Chris@1295 54 :order => 4, :sym => TYPE_BLOCKED },
Chris@1295 55 TYPE_BLOCKED => { :name => :label_blocked_by, :sym_name => :label_blocks,
Chris@1295 56 :order => 5, :sym => TYPE_BLOCKS, :reverse => TYPE_BLOCKS },
Chris@1295 57 TYPE_PRECEDES => { :name => :label_precedes, :sym_name => :label_follows,
Chris@1295 58 :order => 6, :sym => TYPE_FOLLOWS },
Chris@1295 59 TYPE_FOLLOWS => { :name => :label_follows, :sym_name => :label_precedes,
Chris@1295 60 :order => 7, :sym => TYPE_PRECEDES, :reverse => TYPE_PRECEDES },
Chris@1295 61 TYPE_COPIED_TO => { :name => :label_copied_to, :sym_name => :label_copied_from,
Chris@1295 62 :order => 8, :sym => TYPE_COPIED_FROM },
Chris@1295 63 TYPE_COPIED_FROM => { :name => :label_copied_from, :sym_name => :label_copied_to,
Chris@1295 64 :order => 9, :sym => TYPE_COPIED_TO, :reverse => TYPE_COPIED_TO }
Chris@1295 65 }.freeze
Chris@1295 66
Chris@1295 67 validates_presence_of :issue_from, :issue_to, :relation_type
Chris@1295 68 validates_inclusion_of :relation_type, :in => TYPES.keys
Chris@1295 69 validates_numericality_of :delay, :allow_nil => true
Chris@1295 70 validates_uniqueness_of :issue_to_id, :scope => :issue_from_id
Chris@1295 71 validate :validate_issue_relation
Chris@1295 72
Chris@1295 73 attr_protected :issue_from_id, :issue_to_id
Chris@1295 74 before_save :handle_issue_order
Chris@1295 75
Chris@1295 76 def visible?(user=User.current)
Chris@1295 77 (issue_from.nil? || issue_from.visible?(user)) && (issue_to.nil? || issue_to.visible?(user))
Chris@1295 78 end
Chris@1295 79
Chris@1295 80 def deletable?(user=User.current)
Chris@1295 81 visible?(user) &&
Chris@1295 82 ((issue_from.nil? || user.allowed_to?(:manage_issue_relations, issue_from.project)) ||
Chris@1295 83 (issue_to.nil? || user.allowed_to?(:manage_issue_relations, issue_to.project)))
Chris@1295 84 end
Chris@1295 85
Chris@1295 86 def initialize(attributes=nil, *args)
Chris@1295 87 super
Chris@1295 88 if new_record?
Chris@1295 89 if relation_type.blank?
Chris@1295 90 self.relation_type = IssueRelation::TYPE_RELATES
Chris@1295 91 end
Chris@1295 92 end
Chris@1295 93 end
Chris@1295 94
Chris@1295 95 def validate_issue_relation
Chris@1295 96 if issue_from && issue_to
Chris@1295 97 errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id
Chris@1295 98 unless issue_from.project_id == issue_to.project_id ||
Chris@1295 99 Setting.cross_project_issue_relations?
Chris@1295 100 errors.add :issue_to_id, :not_same_project
Chris@1295 101 end
Chris@1295 102 # detect circular dependencies depending wether the relation should be reversed
Chris@1295 103 if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
Chris@1295 104 errors.add :base, :circular_dependency if issue_from.all_dependent_issues.include? issue_to
Chris@1295 105 else
Chris@1295 106 errors.add :base, :circular_dependency if issue_to.all_dependent_issues.include? issue_from
Chris@1295 107 end
Chris@1295 108 if issue_from.is_descendant_of?(issue_to) || issue_from.is_ancestor_of?(issue_to)
Chris@1295 109 errors.add :base, :cant_link_an_issue_with_a_descendant
Chris@1295 110 end
Chris@1295 111 end
Chris@1295 112 end
Chris@1295 113
Chris@1295 114 def other_issue(issue)
Chris@1295 115 (self.issue_from_id == issue.id) ? issue_to : issue_from
Chris@1295 116 end
Chris@1295 117
Chris@1295 118 # Returns the relation type for +issue+
Chris@1295 119 def relation_type_for(issue)
Chris@1295 120 if TYPES[relation_type]
Chris@1295 121 if self.issue_from_id == issue.id
Chris@1295 122 relation_type
Chris@1295 123 else
Chris@1295 124 TYPES[relation_type][:sym]
Chris@1295 125 end
Chris@1295 126 end
Chris@1295 127 end
Chris@1295 128
Chris@1295 129 def label_for(issue)
Chris@1295 130 TYPES[relation_type] ?
Chris@1295 131 TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] :
Chris@1295 132 :unknow
Chris@1295 133 end
Chris@1295 134
Chris@1295 135 def css_classes_for(issue)
Chris@1295 136 "rel-#{relation_type_for(issue)}"
Chris@1295 137 end
Chris@1295 138
Chris@1295 139 def handle_issue_order
Chris@1295 140 reverse_if_needed
Chris@1295 141
Chris@1295 142 if TYPE_PRECEDES == relation_type
Chris@1295 143 self.delay ||= 0
Chris@1295 144 else
Chris@1295 145 self.delay = nil
Chris@1295 146 end
Chris@1295 147 set_issue_to_dates
Chris@1295 148 end
Chris@1295 149
Chris@1295 150 def set_issue_to_dates
Chris@1295 151 soonest_start = self.successor_soonest_start
Chris@1295 152 if soonest_start && issue_to
Chris@1295 153 issue_to.reschedule_on!(soonest_start)
Chris@1295 154 end
Chris@1295 155 end
Chris@1295 156
Chris@1295 157 def successor_soonest_start
Chris@1295 158 if (TYPE_PRECEDES == self.relation_type) && delay && issue_from &&
Chris@1295 159 (issue_from.start_date || issue_from.due_date)
Chris@1295 160 (issue_from.due_date || issue_from.start_date) + 1 + delay
Chris@1295 161 end
Chris@1295 162 end
Chris@1295 163
Chris@1295 164 def <=>(relation)
Chris@1295 165 r = TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order]
Chris@1295 166 r == 0 ? id <=> relation.id : r
Chris@1295 167 end
Chris@1295 168
Chris@1295 169 private
Chris@1295 170
Chris@1295 171 # Reverses the relation if needed so that it gets stored in the proper way
Chris@1295 172 # Should not be reversed before validation so that it can be displayed back
Chris@1295 173 # as entered on new relation form
Chris@1295 174 def reverse_if_needed
Chris@1295 175 if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
Chris@1295 176 issue_tmp = issue_to
Chris@1295 177 self.issue_to = issue_from
Chris@1295 178 self.issue_from = issue_tmp
Chris@1295 179 self.relation_type = TYPES[relation_type][:reverse]
Chris@1295 180 end
Chris@1295 181 end
Chris@1295 182 end