##// END OF EJS Templates
Fixed: issue_id not nullified on time entries when deleting the issue...
Jean-Philippe Lang -
r578:384aa50be384
parent child
Show More
@@ -1,131 +1,131
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 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 Issue < ActiveRecord::Base
18 class Issue < ActiveRecord::Base
19 belongs_to :project
19 belongs_to :project
20 belongs_to :tracker
20 belongs_to :tracker
21 belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id'
21 belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id'
22 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
22 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
23 belongs_to :assigned_to, :class_name => 'User', :foreign_key => 'assigned_to_id'
23 belongs_to :assigned_to, :class_name => 'User', :foreign_key => 'assigned_to_id'
24 belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id'
24 belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id'
25 belongs_to :priority, :class_name => 'Enumeration', :foreign_key => 'priority_id'
25 belongs_to :priority, :class_name => 'Enumeration', :foreign_key => 'priority_id'
26 belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id'
26 belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id'
27
27
28 has_many :journals, :as => :journalized, :dependent => :destroy
28 has_many :journals, :as => :journalized, :dependent => :destroy
29 has_many :attachments, :as => :container, :dependent => :destroy
29 has_many :attachments, :as => :container, :dependent => :destroy
30 has_many :time_entries
30 has_many :time_entries, :dependent => :nullify
31 has_many :custom_values, :dependent => :delete_all, :as => :customized
31 has_many :custom_values, :dependent => :delete_all, :as => :customized
32 has_many :custom_fields, :through => :custom_values
32 has_many :custom_fields, :through => :custom_values
33 has_and_belongs_to_many :changesets, :order => "revision ASC"
33 has_and_belongs_to_many :changesets, :order => "revision ASC"
34
34
35 has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all
35 has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all
36 has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all
36 has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all
37
37
38 acts_as_watchable
38 acts_as_watchable
39
39
40 validates_presence_of :subject, :description, :priority, :tracker, :author, :status
40 validates_presence_of :subject, :description, :priority, :tracker, :author, :status
41 validates_inclusion_of :done_ratio, :in => 0..100
41 validates_inclusion_of :done_ratio, :in => 0..100
42 validates_associated :custom_values, :on => :update
42 validates_associated :custom_values, :on => :update
43
43
44 # set default status for new issues
44 # set default status for new issues
45 def before_validation
45 def before_validation
46 self.status = IssueStatus.default if status.nil?
46 self.status = IssueStatus.default if status.nil?
47 end
47 end
48
48
49 def validate
49 def validate
50 if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty?
50 if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty?
51 errors.add :due_date, :activerecord_error_not_a_date
51 errors.add :due_date, :activerecord_error_not_a_date
52 end
52 end
53
53
54 if self.due_date and self.start_date and self.due_date < self.start_date
54 if self.due_date and self.start_date and self.due_date < self.start_date
55 errors.add :due_date, :activerecord_error_greater_than_start_date
55 errors.add :due_date, :activerecord_error_greater_than_start_date
56 end
56 end
57
57
58 if start_date && soonest_start && start_date < soonest_start
58 if start_date && soonest_start && start_date < soonest_start
59 errors.add :start_date, :activerecord_error_invalid
59 errors.add :start_date, :activerecord_error_invalid
60 end
60 end
61 end
61 end
62
62
63 def before_create
63 def before_create
64 # default assignment based on category
64 # default assignment based on category
65 if assigned_to.nil? && category && category.assigned_to
65 if assigned_to.nil? && category && category.assigned_to
66 self.assigned_to = category.assigned_to
66 self.assigned_to = category.assigned_to
67 end
67 end
68 end
68 end
69
69
70 def before_save
70 def before_save
71 if @current_journal
71 if @current_journal
72 # attributes changes
72 # attributes changes
73 (Issue.column_names - %w(id description)).each {|c|
73 (Issue.column_names - %w(id description)).each {|c|
74 @current_journal.details << JournalDetail.new(:property => 'attr',
74 @current_journal.details << JournalDetail.new(:property => 'attr',
75 :prop_key => c,
75 :prop_key => c,
76 :old_value => @issue_before_change.send(c),
76 :old_value => @issue_before_change.send(c),
77 :value => send(c)) unless send(c)==@issue_before_change.send(c)
77 :value => send(c)) unless send(c)==@issue_before_change.send(c)
78 }
78 }
79 # custom fields changes
79 # custom fields changes
80 custom_values.each {|c|
80 custom_values.each {|c|
81 @current_journal.details << JournalDetail.new(:property => 'cf',
81 @current_journal.details << JournalDetail.new(:property => 'cf',
82 :prop_key => c.custom_field_id,
82 :prop_key => c.custom_field_id,
83 :old_value => @custom_values_before_change[c.custom_field_id],
83 :old_value => @custom_values_before_change[c.custom_field_id],
84 :value => c.value) unless @custom_values_before_change[c.custom_field_id]==c.value
84 :value => c.value) unless @custom_values_before_change[c.custom_field_id]==c.value
85 }
85 }
86 @current_journal.save unless @current_journal.details.empty? and @current_journal.notes.empty?
86 @current_journal.save unless @current_journal.details.empty? and @current_journal.notes.empty?
87 end
87 end
88 end
88 end
89
89
90 def after_save
90 def after_save
91 relations_from.each(&:set_issue_to_dates)
91 relations_from.each(&:set_issue_to_dates)
92 end
92 end
93
93
94 def custom_value_for(custom_field)
94 def custom_value_for(custom_field)
95 self.custom_values.each {|v| return v if v.custom_field_id == custom_field.id }
95 self.custom_values.each {|v| return v if v.custom_field_id == custom_field.id }
96 return nil
96 return nil
97 end
97 end
98
98
99 def init_journal(user, notes = "")
99 def init_journal(user, notes = "")
100 @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
100 @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
101 @issue_before_change = self.clone
101 @issue_before_change = self.clone
102 @custom_values_before_change = {}
102 @custom_values_before_change = {}
103 self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
103 self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
104 @current_journal
104 @current_journal
105 end
105 end
106
106
107 def spent_hours
107 def spent_hours
108 @spent_hours ||= time_entries.sum(:hours) || 0
108 @spent_hours ||= time_entries.sum(:hours) || 0
109 end
109 end
110
110
111 def relations
111 def relations
112 (relations_from + relations_to).sort
112 (relations_from + relations_to).sort
113 end
113 end
114
114
115 def all_dependent_issues
115 def all_dependent_issues
116 dependencies = []
116 dependencies = []
117 relations_from.each do |relation|
117 relations_from.each do |relation|
118 dependencies << relation.issue_to
118 dependencies << relation.issue_to
119 dependencies += relation.issue_to.all_dependent_issues
119 dependencies += relation.issue_to.all_dependent_issues
120 end
120 end
121 dependencies
121 dependencies
122 end
122 end
123
123
124 def duration
124 def duration
125 (start_date && due_date) ? due_date - start_date : 0
125 (start_date && due_date) ? due_date - start_date : 0
126 end
126 end
127
127
128 def soonest_start
128 def soonest_start
129 @soonest_start ||= relations_to.collect{|relation| relation.successor_soonest_start}.compact.min
129 @soonest_start ||= relations_to.collect{|relation| relation.successor_soonest_start}.compact.min
130 end
130 end
131 end
131 end
General Comments 0
You need to be logged in to leave comments. Login now