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