annotate .svn/pristine/d9/d91ecb3f88fd014f5736d4fb398c7e45250670a7.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

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