##// END OF EJS Templates
Rails3: replace deprecated 'validate' method at IssueRelation model....
Toshi MARUYAMA -
r6817:86d6f2fe372e
parent child
Show More
@@ -1,138 +1,140
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class IssueRelation < ActiveRecord::Base
19 19 belongs_to :issue_from, :class_name => 'Issue', :foreign_key => 'issue_from_id'
20 20 belongs_to :issue_to, :class_name => 'Issue', :foreign_key => 'issue_to_id'
21 21
22 22 TYPE_RELATES = "relates"
23 23 TYPE_DUPLICATES = "duplicates"
24 24 TYPE_DUPLICATED = "duplicated"
25 25 TYPE_BLOCKS = "blocks"
26 26 TYPE_BLOCKED = "blocked"
27 27 TYPE_PRECEDES = "precedes"
28 28 TYPE_FOLLOWS = "follows"
29 29
30 30 TYPES = { TYPE_RELATES => { :name => :label_relates_to, :sym_name => :label_relates_to, :order => 1, :sym => TYPE_RELATES },
31 31 TYPE_DUPLICATES => { :name => :label_duplicates, :sym_name => :label_duplicated_by, :order => 2, :sym => TYPE_DUPLICATED },
32 32 TYPE_DUPLICATED => { :name => :label_duplicated_by, :sym_name => :label_duplicates, :order => 3, :sym => TYPE_DUPLICATES, :reverse => TYPE_DUPLICATES },
33 33 TYPE_BLOCKS => { :name => :label_blocks, :sym_name => :label_blocked_by, :order => 4, :sym => TYPE_BLOCKED },
34 34 TYPE_BLOCKED => { :name => :label_blocked_by, :sym_name => :label_blocks, :order => 5, :sym => TYPE_BLOCKS, :reverse => TYPE_BLOCKS },
35 35 TYPE_PRECEDES => { :name => :label_precedes, :sym_name => :label_follows, :order => 6, :sym => TYPE_FOLLOWS },
36 36 TYPE_FOLLOWS => { :name => :label_follows, :sym_name => :label_precedes, :order => 7, :sym => TYPE_PRECEDES, :reverse => TYPE_PRECEDES }
37 37 }.freeze
38 38
39 39 validates_presence_of :issue_from, :issue_to, :relation_type
40 40 validates_inclusion_of :relation_type, :in => TYPES.keys
41 41 validates_numericality_of :delay, :allow_nil => true
42 42 validates_uniqueness_of :issue_to_id, :scope => :issue_from_id
43 43
44 validate :validate_issue_relation
45
44 46 attr_protected :issue_from_id, :issue_to_id
45 47
46 48 def visible?(user=User.current)
47 49 (issue_from.nil? || issue_from.visible?(user)) && (issue_to.nil? || issue_to.visible?(user))
48 50 end
49 51
50 52 def deletable?(user=User.current)
51 53 visible?(user) &&
52 54 ((issue_from.nil? || user.allowed_to?(:manage_issue_relations, issue_from.project)) ||
53 55 (issue_to.nil? || user.allowed_to?(:manage_issue_relations, issue_to.project)))
54 56 end
55 57
56 58 def after_initialize
57 59 if new_record?
58 60 if relation_type.blank?
59 61 self.relation_type = IssueRelation::TYPE_RELATES
60 62 end
61 63 end
62 64 end
63 65
64 def validate
66 def validate_issue_relation
65 67 if issue_from && issue_to
66 68 errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id
67 69 errors.add :issue_to_id, :not_same_project unless issue_from.project_id == issue_to.project_id || Setting.cross_project_issue_relations?
68 70 #detect circular dependencies depending wether the relation should be reversed
69 71 if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
70 72 errors.add_to_base :circular_dependency if issue_from.all_dependent_issues.include? issue_to
71 73 else
72 74 errors.add_to_base :circular_dependency if issue_to.all_dependent_issues.include? issue_from
73 75 end
74 76 errors.add_to_base :cant_link_an_issue_with_a_descendant if issue_from.is_descendant_of?(issue_to) || issue_from.is_ancestor_of?(issue_to)
75 77 end
76 78 end
77 79
78 80 def other_issue(issue)
79 81 (self.issue_from_id == issue.id) ? issue_to : issue_from
80 82 end
81 83
82 84 # Returns the relation type for +issue+
83 85 def relation_type_for(issue)
84 86 if TYPES[relation_type]
85 87 if self.issue_from_id == issue.id
86 88 relation_type
87 89 else
88 90 TYPES[relation_type][:sym]
89 91 end
90 92 end
91 93 end
92 94
93 95 def label_for(issue)
94 96 TYPES[relation_type] ? TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] : :unknow
95 97 end
96 98
97 99 def before_save
98 100 reverse_if_needed
99 101
100 102 if TYPE_PRECEDES == relation_type
101 103 self.delay ||= 0
102 104 else
103 105 self.delay = nil
104 106 end
105 107 set_issue_to_dates
106 108 end
107 109
108 110 def set_issue_to_dates
109 111 soonest_start = self.successor_soonest_start
110 112 if soonest_start && issue_to
111 113 issue_to.reschedule_after(soonest_start)
112 114 end
113 115 end
114 116
115 117 def successor_soonest_start
116 118 if (TYPE_PRECEDES == self.relation_type) && delay && issue_from && (issue_from.start_date || issue_from.due_date)
117 119 (issue_from.due_date || issue_from.start_date) + 1 + delay
118 120 end
119 121 end
120 122
121 123 def <=>(relation)
122 124 TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order]
123 125 end
124 126
125 127 private
126 128
127 129 # Reverses the relation if needed so that it gets stored in the proper way
128 130 # Should not be reversed before validation so that it can be displayed back
129 131 # as entered on new relation form
130 132 def reverse_if_needed
131 133 if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
132 134 issue_tmp = issue_to
133 135 self.issue_to = issue_from
134 136 self.issue_from = issue_tmp
135 137 self.relation_type = TYPES[relation_type][:reverse]
136 138 end
137 139 end
138 140 end
General Comments 0
You need to be logged in to leave comments. Login now