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