@@ -1,131 +1,131 | |||
|
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 | has_many :time_entries | |
|
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_inclusion_of :done_ratio, :in => 0..100 |
|
42 | 42 | validates_associated :custom_values, :on => :update |
|
43 | 43 | |
|
44 | 44 | # set default status for new issues |
|
45 | 45 | def before_validation |
|
46 | 46 | self.status = IssueStatus.default if status.nil? |
|
47 | 47 | end |
|
48 | 48 | |
|
49 | 49 | def validate |
|
50 | 50 | if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty? |
|
51 | 51 | errors.add :due_date, :activerecord_error_not_a_date |
|
52 | 52 | end |
|
53 | 53 | |
|
54 | 54 | if self.due_date and self.start_date and self.due_date < self.start_date |
|
55 | 55 | errors.add :due_date, :activerecord_error_greater_than_start_date |
|
56 | 56 | end |
|
57 | 57 | |
|
58 | 58 | if start_date && soonest_start && start_date < soonest_start |
|
59 | 59 | errors.add :start_date, :activerecord_error_invalid |
|
60 | 60 | end |
|
61 | 61 | end |
|
62 | 62 | |
|
63 | 63 | def before_create |
|
64 | 64 | # default assignment based on category |
|
65 | 65 | if assigned_to.nil? && category && category.assigned_to |
|
66 | 66 | self.assigned_to = category.assigned_to |
|
67 | 67 | end |
|
68 | 68 | end |
|
69 | 69 | |
|
70 | 70 | def before_save |
|
71 | 71 | if @current_journal |
|
72 | 72 | # attributes changes |
|
73 | 73 | (Issue.column_names - %w(id description)).each {|c| |
|
74 | 74 | @current_journal.details << JournalDetail.new(:property => 'attr', |
|
75 | 75 | :prop_key => c, |
|
76 | 76 | :old_value => @issue_before_change.send(c), |
|
77 | 77 | :value => send(c)) unless send(c)==@issue_before_change.send(c) |
|
78 | 78 | } |
|
79 | 79 | # custom fields changes |
|
80 | 80 | custom_values.each {|c| |
|
81 | 81 | @current_journal.details << JournalDetail.new(:property => 'cf', |
|
82 | 82 | :prop_key => c.custom_field_id, |
|
83 | 83 | :old_value => @custom_values_before_change[c.custom_field_id], |
|
84 | 84 | :value => c.value) unless @custom_values_before_change[c.custom_field_id]==c.value |
|
85 | 85 | } |
|
86 | 86 | @current_journal.save unless @current_journal.details.empty? and @current_journal.notes.empty? |
|
87 | 87 | end |
|
88 | 88 | end |
|
89 | 89 | |
|
90 | 90 | def after_save |
|
91 | 91 | relations_from.each(&:set_issue_to_dates) |
|
92 | 92 | end |
|
93 | 93 | |
|
94 | 94 | def custom_value_for(custom_field) |
|
95 | 95 | self.custom_values.each {|v| return v if v.custom_field_id == custom_field.id } |
|
96 | 96 | return nil |
|
97 | 97 | end |
|
98 | 98 | |
|
99 | 99 | def init_journal(user, notes = "") |
|
100 | 100 | @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes) |
|
101 | 101 | @issue_before_change = self.clone |
|
102 | 102 | @custom_values_before_change = {} |
|
103 | 103 | self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value } |
|
104 | 104 | @current_journal |
|
105 | 105 | end |
|
106 | 106 | |
|
107 | 107 | def spent_hours |
|
108 | 108 | @spent_hours ||= time_entries.sum(:hours) || 0 |
|
109 | 109 | end |
|
110 | 110 | |
|
111 | 111 | def relations |
|
112 | 112 | (relations_from + relations_to).sort |
|
113 | 113 | end |
|
114 | 114 | |
|
115 | 115 | def all_dependent_issues |
|
116 | 116 | dependencies = [] |
|
117 | 117 | relations_from.each do |relation| |
|
118 | 118 | dependencies << relation.issue_to |
|
119 | 119 | dependencies += relation.issue_to.all_dependent_issues |
|
120 | 120 | end |
|
121 | 121 | dependencies |
|
122 | 122 | end |
|
123 | 123 | |
|
124 | 124 | def duration |
|
125 | 125 | (start_date && due_date) ? due_date - start_date : 0 |
|
126 | 126 | end |
|
127 | 127 | |
|
128 | 128 | def soonest_start |
|
129 | 129 | @soonest_start ||= relations_to.collect{|relation| relation.successor_soonest_start}.compact.min |
|
130 | 130 | end |
|
131 | 131 | end |
General Comments 0
You need to be logged in to leave comments.
Login now