annotate .svn/pristine/08/08f914636b6cd922b89f9ce8cb2268cea53be558.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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