@@ -5,12 +5,12 | |||||
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. | |
@@ -18,7 +18,7 | |||||
18 | class IssueRelation < ActiveRecord::Base |
|
18 | class IssueRelation < ActiveRecord::Base | |
19 | belongs_to :issue_from, :class_name => 'Issue', :foreign_key => 'issue_from_id' |
|
19 | belongs_to :issue_from, :class_name => 'Issue', :foreign_key => 'issue_from_id' | |
20 | belongs_to :issue_to, :class_name => 'Issue', :foreign_key => 'issue_to_id' |
|
20 | belongs_to :issue_to, :class_name => 'Issue', :foreign_key => 'issue_to_id' | |
21 |
|
21 | |||
22 | TYPE_RELATES = "relates" |
|
22 | TYPE_RELATES = "relates" | |
23 | TYPE_DUPLICATES = "duplicates" |
|
23 | TYPE_DUPLICATES = "duplicates" | |
24 | TYPE_DUPLICATED = "duplicated" |
|
24 | TYPE_DUPLICATED = "duplicated" | |
@@ -26,7 +26,7 class IssueRelation < ActiveRecord::Base | |||||
26 | TYPE_BLOCKED = "blocked" |
|
26 | TYPE_BLOCKED = "blocked" | |
27 | TYPE_PRECEDES = "precedes" |
|
27 | TYPE_PRECEDES = "precedes" | |
28 | TYPE_FOLLOWS = "follows" |
|
28 | TYPE_FOLLOWS = "follows" | |
29 |
|
29 | |||
30 | TYPES = { TYPE_RELATES => { :name => :label_relates_to, :sym_name => :label_relates_to, :order => 1, :sym => TYPE_RELATES }, |
|
30 | TYPES = { TYPE_RELATES => { :name => :label_relates_to, :sym_name => :label_relates_to, :order => 1, :sym => TYPE_RELATES }, | |
31 | TYPE_DUPLICATES => { :name => :label_duplicates, :sym_name => :label_duplicated_by, :order => 2, :sym => TYPE_DUPLICATED }, |
|
31 | TYPE_DUPLICATES => { :name => :label_duplicates, :sym_name => :label_duplicated_by, :order => 2, :sym => TYPE_DUPLICATED }, | |
32 | TYPE_DUPLICATED => { :name => :label_duplicated_by, :sym_name => :label_duplicates, :order => 3, :sym => TYPE_DUPLICATES, :reverse => TYPE_DUPLICATES }, |
|
32 | TYPE_DUPLICATED => { :name => :label_duplicated_by, :sym_name => :label_duplicates, :order => 3, :sym => TYPE_DUPLICATES, :reverse => TYPE_DUPLICATES }, | |
@@ -35,24 +35,24 class IssueRelation < ActiveRecord::Base | |||||
35 | TYPE_PRECEDES => { :name => :label_precedes, :sym_name => :label_follows, :order => 6, :sym => TYPE_FOLLOWS }, |
|
35 | TYPE_PRECEDES => { :name => :label_precedes, :sym_name => :label_follows, :order => 6, :sym => TYPE_FOLLOWS }, | |
36 | TYPE_FOLLOWS => { :name => :label_follows, :sym_name => :label_precedes, :order => 7, :sym => TYPE_PRECEDES, :reverse => TYPE_PRECEDES } |
|
36 | TYPE_FOLLOWS => { :name => :label_follows, :sym_name => :label_precedes, :order => 7, :sym => TYPE_PRECEDES, :reverse => TYPE_PRECEDES } | |
37 | }.freeze |
|
37 | }.freeze | |
38 |
|
38 | |||
39 | validates_presence_of :issue_from, :issue_to, :relation_type |
|
39 | validates_presence_of :issue_from, :issue_to, :relation_type | |
40 | validates_inclusion_of :relation_type, :in => TYPES.keys |
|
40 | validates_inclusion_of :relation_type, :in => TYPES.keys | |
41 | validates_numericality_of :delay, :allow_nil => true |
|
41 | validates_numericality_of :delay, :allow_nil => true | |
42 | validates_uniqueness_of :issue_to_id, :scope => :issue_from_id |
|
42 | validates_uniqueness_of :issue_to_id, :scope => :issue_from_id | |
43 |
|
43 | |||
44 | attr_protected :issue_from_id, :issue_to_id |
|
44 | attr_protected :issue_from_id, :issue_to_id | |
45 |
|
45 | |||
46 | def visible?(user=User.current) |
|
46 | def visible?(user=User.current) | |
47 | (issue_from.nil? || issue_from.visible?(user)) && (issue_to.nil? || issue_to.visible?(user)) |
|
47 | (issue_from.nil? || issue_from.visible?(user)) && (issue_to.nil? || issue_to.visible?(user)) | |
48 | end |
|
48 | end | |
49 |
|
49 | |||
50 | def deletable?(user=User.current) |
|
50 | def deletable?(user=User.current) | |
51 | visible?(user) && |
|
51 | visible?(user) && | |
52 | ((issue_from.nil? || user.allowed_to?(:manage_issue_relations, issue_from.project)) || |
|
52 | ((issue_from.nil? || user.allowed_to?(:manage_issue_relations, issue_from.project)) || | |
53 | (issue_to.nil? || user.allowed_to?(:manage_issue_relations, issue_to.project))) |
|
53 | (issue_to.nil? || user.allowed_to?(:manage_issue_relations, issue_to.project))) | |
54 | end |
|
54 | end | |
55 |
|
55 | |||
56 | def after_initialize |
|
56 | def after_initialize | |
57 | if new_record? |
|
57 | if new_record? | |
58 | if relation_type.blank? |
|
58 | if relation_type.blank? | |
@@ -60,7 +60,7 class IssueRelation < ActiveRecord::Base | |||||
60 | end |
|
60 | end | |
61 | end |
|
61 | end | |
62 | end |
|
62 | end | |
63 |
|
63 | |||
64 | def validate |
|
64 | def validate | |
65 | if issue_from && issue_to |
|
65 | if issue_from && issue_to | |
66 | errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id |
|
66 | errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id | |
@@ -74,11 +74,11 class IssueRelation < ActiveRecord::Base | |||||
74 | 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) |
|
74 | 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 | end |
|
75 | end | |
76 | end |
|
76 | end | |
77 |
|
77 | |||
78 | def other_issue(issue) |
|
78 | def other_issue(issue) | |
79 | (self.issue_from_id == issue.id) ? issue_to : issue_from |
|
79 | (self.issue_from_id == issue.id) ? issue_to : issue_from | |
80 | end |
|
80 | end | |
81 |
|
81 | |||
82 | # Returns the relation type for +issue+ |
|
82 | # Returns the relation type for +issue+ | |
83 | def relation_type_for(issue) |
|
83 | def relation_type_for(issue) | |
84 | if TYPES[relation_type] |
|
84 | if TYPES[relation_type] | |
@@ -89,14 +89,14 class IssueRelation < ActiveRecord::Base | |||||
89 | end |
|
89 | end | |
90 | end |
|
90 | end | |
91 | end |
|
91 | end | |
92 |
|
92 | |||
93 | def label_for(issue) |
|
93 | def label_for(issue) | |
94 | TYPES[relation_type] ? TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] : :unknow |
|
94 | TYPES[relation_type] ? TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] : :unknow | |
95 | end |
|
95 | end | |
96 |
|
96 | |||
97 | def before_save |
|
97 | def before_save | |
98 | reverse_if_needed |
|
98 | reverse_if_needed | |
99 |
|
99 | |||
100 | if TYPE_PRECEDES == relation_type |
|
100 | if TYPE_PRECEDES == relation_type | |
101 | self.delay ||= 0 |
|
101 | self.delay ||= 0 | |
102 | else |
|
102 | else | |
@@ -104,26 +104,26 class IssueRelation < ActiveRecord::Base | |||||
104 | end |
|
104 | end | |
105 | set_issue_to_dates |
|
105 | set_issue_to_dates | |
106 | end |
|
106 | end | |
107 |
|
107 | |||
108 | def set_issue_to_dates |
|
108 | def set_issue_to_dates | |
109 | soonest_start = self.successor_soonest_start |
|
109 | soonest_start = self.successor_soonest_start | |
110 | if soonest_start && issue_to |
|
110 | if soonest_start && issue_to | |
111 | issue_to.reschedule_after(soonest_start) |
|
111 | issue_to.reschedule_after(soonest_start) | |
112 | end |
|
112 | end | |
113 | end |
|
113 | end | |
114 |
|
114 | |||
115 | def successor_soonest_start |
|
115 | def successor_soonest_start | |
116 | if (TYPE_PRECEDES == self.relation_type) && delay && issue_from && (issue_from.start_date || issue_from.due_date) |
|
116 | if (TYPE_PRECEDES == self.relation_type) && delay && issue_from && (issue_from.start_date || issue_from.due_date) | |
117 | (issue_from.due_date || issue_from.start_date) + 1 + delay |
|
117 | (issue_from.due_date || issue_from.start_date) + 1 + delay | |
118 | end |
|
118 | end | |
119 | end |
|
119 | end | |
120 |
|
120 | |||
121 | def <=>(relation) |
|
121 | def <=>(relation) | |
122 | TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order] |
|
122 | TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order] | |
123 | end |
|
123 | end | |
124 |
|
124 | |||
125 | private |
|
125 | private | |
126 |
|
126 | |||
127 | # Reverses the relation if needed so that it gets stored in the proper way |
|
127 | # Reverses the relation if needed so that it gets stored in the proper way | |
128 | # Should not be reversed before validation so that it can be displayed back |
|
128 | # Should not be reversed before validation so that it can be displayed back | |
129 | # as entered on new relation form |
|
129 | # as entered on new relation form |
General Comments 0
You need to be logged in to leave comments.
Login now