@@ -1,142 +1,164 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 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 Issue < ActiveRecord::Base |
|
19 | 19 | belongs_to :project |
|
20 | 20 | belongs_to :tracker |
|
21 | 21 | belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id' |
|
22 | 22 | belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' |
|
23 | 23 | belongs_to :assigned_to, :class_name => 'User', :foreign_key => 'assigned_to_id' |
|
24 | 24 | belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id' |
|
25 | 25 | belongs_to :priority, :class_name => 'Enumeration', :foreign_key => 'priority_id' |
|
26 | 26 | belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id' |
|
27 | 27 | |
|
28 | 28 | has_many :journals, :as => :journalized, :dependent => :destroy |
|
29 | 29 | has_many :attachments, :as => :container, :dependent => :destroy |
|
30 | 30 | has_many :time_entries, :dependent => :nullify |
|
31 | 31 | has_many :custom_values, :dependent => :delete_all, :as => :customized |
|
32 | 32 | has_many :custom_fields, :through => :custom_values |
|
33 | 33 | has_and_belongs_to_many :changesets, :order => "revision ASC" |
|
34 | 34 | |
|
35 | 35 | has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all |
|
36 | 36 | has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all |
|
37 | 37 | |
|
38 | 38 | acts_as_watchable |
|
39 | 39 | |
|
40 | 40 | validates_presence_of :subject, :description, :priority, :tracker, :author, :status |
|
41 | 41 | validates_length_of :subject, :maximum => 255 |
|
42 | 42 | validates_inclusion_of :done_ratio, :in => 0..100 |
|
43 | 43 | validates_associated :custom_values, :on => :update |
|
44 | 44 | |
|
45 | 45 | # set default status for new issues |
|
46 | 46 | def before_validation |
|
47 | 47 | self.status = IssueStatus.default if status.nil? |
|
48 | 48 | end |
|
49 | 49 | |
|
50 | 50 | def validate |
|
51 | 51 | if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty? |
|
52 | 52 | errors.add :due_date, :activerecord_error_not_a_date |
|
53 | 53 | end |
|
54 | 54 | |
|
55 | 55 | if self.due_date and self.start_date and self.due_date < self.start_date |
|
56 | 56 | errors.add :due_date, :activerecord_error_greater_than_start_date |
|
57 | 57 | end |
|
58 | 58 | |
|
59 | 59 | if start_date && soonest_start && start_date < soonest_start |
|
60 | 60 | errors.add :start_date, :activerecord_error_invalid |
|
61 | 61 | end |
|
62 | 62 | |
|
63 | 63 | # validate assignment |
|
64 | 64 | if assigned_to && !assignable_users.include?(assigned_to) |
|
65 | 65 | errors.add :assigned_to_id, :activerecord_error_invalid |
|
66 | 66 | end |
|
67 | 67 | end |
|
68 | 68 | |
|
69 | 69 | def before_create |
|
70 | 70 | # default assignment based on category |
|
71 | 71 | if assigned_to.nil? && category && category.assigned_to |
|
72 | 72 | self.assigned_to = category.assigned_to |
|
73 | 73 | end |
|
74 | 74 | end |
|
75 | 75 | |
|
76 | 76 | def before_save |
|
77 | 77 | if @current_journal |
|
78 | 78 | # attributes changes |
|
79 | 79 | (Issue.column_names - %w(id description)).each {|c| |
|
80 | 80 | @current_journal.details << JournalDetail.new(:property => 'attr', |
|
81 | 81 | :prop_key => c, |
|
82 | 82 | :old_value => @issue_before_change.send(c), |
|
83 | 83 | :value => send(c)) unless send(c)==@issue_before_change.send(c) |
|
84 | 84 | } |
|
85 | 85 | # custom fields changes |
|
86 | 86 | custom_values.each {|c| |
|
87 | 87 | @current_journal.details << JournalDetail.new(:property => 'cf', |
|
88 | 88 | :prop_key => c.custom_field_id, |
|
89 | 89 | :old_value => @custom_values_before_change[c.custom_field_id], |
|
90 | 90 | :value => c.value) unless @custom_values_before_change[c.custom_field_id]==c.value |
|
91 | 91 | } |
|
92 | 92 | @current_journal.save unless @current_journal.details.empty? and @current_journal.notes.empty? |
|
93 | 93 | end |
|
94 | 94 | end |
|
95 | 95 | |
|
96 | 96 | def after_save |
|
97 | # Update start/due dates of following issues | |
|
97 | 98 | relations_from.each(&:set_issue_to_dates) |
|
99 | ||
|
100 | # Close duplicates if the issue was closed | |
|
101 | if @issue_before_change && !@issue_before_change.closed? && self.closed? | |
|
102 | duplicates.each do |duplicate| | |
|
103 | # Don't re-close it if it's already closed | |
|
104 | next if duplicate.closed? | |
|
105 | # Same user and notes | |
|
106 | duplicate.init_journal(@current_journal.user, @current_journal.notes) | |
|
107 | duplicate.update_attribute :status, self.status | |
|
108 | end | |
|
109 | end | |
|
98 | 110 | end |
|
99 | 111 | |
|
100 | 112 | def custom_value_for(custom_field) |
|
101 | 113 | self.custom_values.each {|v| return v if v.custom_field_id == custom_field.id } |
|
102 | 114 | return nil |
|
103 | 115 | end |
|
104 | 116 | |
|
105 | 117 | def init_journal(user, notes = "") |
|
106 | 118 | @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes) |
|
107 | 119 | @issue_before_change = self.clone |
|
108 | 120 | @custom_values_before_change = {} |
|
109 | 121 | self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value } |
|
110 | 122 | @current_journal |
|
111 | 123 | end |
|
112 | 124 | |
|
125 | # Return true if the issue is closed, otherwise false | |
|
126 | def closed? | |
|
127 | self.status.is_closed? | |
|
128 | end | |
|
129 | ||
|
113 | 130 | # Users the issue can be assigned to |
|
114 | 131 | def assignable_users |
|
115 | 132 | project.members.select {|m| m.role.assignable?}.collect {|m| m.user} |
|
116 | 133 | end |
|
117 | 134 | |
|
118 | 135 | def spent_hours |
|
119 | 136 | @spent_hours ||= time_entries.sum(:hours) || 0 |
|
120 | 137 | end |
|
121 | 138 | |
|
122 | 139 | def relations |
|
123 | 140 | (relations_from + relations_to).sort |
|
124 | 141 | end |
|
125 | 142 | |
|
126 | 143 | def all_dependent_issues |
|
127 | 144 | dependencies = [] |
|
128 | 145 | relations_from.each do |relation| |
|
129 | 146 | dependencies << relation.issue_to |
|
130 | 147 | dependencies += relation.issue_to.all_dependent_issues |
|
131 | 148 | end |
|
132 | 149 | dependencies |
|
133 | 150 | end |
|
134 | 151 | |
|
152 | # Returns an array of the duplicate issues | |
|
153 | def duplicates | |
|
154 | relations.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.other_issue(self)} | |
|
155 | end | |
|
156 | ||
|
135 | 157 | def duration |
|
136 | 158 | (start_date && due_date) ? due_date - start_date : 0 |
|
137 | 159 | end |
|
138 | 160 | |
|
139 | 161 | def soonest_start |
|
140 | 162 | @soonest_start ||= relations_to.collect{|relation| relation.successor_soonest_start}.compact.min |
|
141 | 163 | end |
|
142 | 164 | end |
@@ -1,27 +1,52 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 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 | require File.dirname(__FILE__) + '/../test_helper' |
|
19 | 19 | |
|
20 | 20 | class IssueTest < Test::Unit::TestCase |
|
21 | 21 | fixtures :projects, :users, :members, :trackers, :issue_statuses, :issue_categories, :enumerations, :issues |
|
22 | 22 | |
|
23 | 23 | def test_category_based_assignment |
|
24 | 24 | issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'Assignment test', :description => 'Assignment test', :category_id => 1) |
|
25 | 25 | assert_equal IssueCategory.find(1).assigned_to, issue.assigned_to |
|
26 | 26 | end |
|
27 | ||
|
28 | def test_close_duplicates | |
|
29 | # Create 3 issues | |
|
30 | issue1 = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'Duplicates test', :description => 'Duplicates test') | |
|
31 | assert issue1.save | |
|
32 | issue2 = issue1.clone | |
|
33 | assert issue2.save | |
|
34 | issue3 = issue1.clone | |
|
35 | assert issue3.save | |
|
36 | ||
|
37 | # 2 is a dupe of 1 | |
|
38 | IssueRelation.create(:issue_from => issue1, :issue_to => issue2, :relation_type => IssueRelation::TYPE_DUPLICATES) | |
|
39 | # And 3 is a dupe of 2 | |
|
40 | IssueRelation.create(:issue_from => issue2, :issue_to => issue3, :relation_type => IssueRelation::TYPE_DUPLICATES) | |
|
41 | ||
|
42 | assert issue1.reload.duplicates.include?(issue2) | |
|
43 | ||
|
44 | # Closing issue 1 | |
|
45 | issue1.init_journal(User.find(:first), "Closing issue1") | |
|
46 | issue1.status = IssueStatus.find :first, :conditions => {:is_closed => true} | |
|
47 | assert issue1.save | |
|
48 | # 2 and 3 should be also closed | |
|
49 | assert issue2.reload.closed? | |
|
50 | assert issue3.reload.closed? | |
|
51 | end | |
|
27 | 52 | end |
General Comments 0
You need to be logged in to leave comments.
Login now