@@ -1,1038 +1,1039 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2011 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2011 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 | include Redmine::SafeAttributes |
|
19 | include Redmine::SafeAttributes | |
20 |
|
20 | |||
21 | belongs_to :project |
|
21 | belongs_to :project | |
22 | belongs_to :tracker |
|
22 | belongs_to :tracker | |
23 | belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id' |
|
23 | belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id' | |
24 | belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' |
|
24 | belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' | |
25 | belongs_to :assigned_to, :class_name => 'Principal', :foreign_key => 'assigned_to_id' |
|
25 | belongs_to :assigned_to, :class_name => 'Principal', :foreign_key => 'assigned_to_id' | |
26 | belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id' |
|
26 | belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id' | |
27 | belongs_to :priority, :class_name => 'IssuePriority', :foreign_key => 'priority_id' |
|
27 | belongs_to :priority, :class_name => 'IssuePriority', :foreign_key => 'priority_id' | |
28 | belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id' |
|
28 | belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id' | |
29 |
|
29 | |||
30 | has_many :journals, :as => :journalized, :dependent => :destroy |
|
30 | has_many :journals, :as => :journalized, :dependent => :destroy | |
31 | has_many :time_entries, :dependent => :delete_all |
|
31 | has_many :time_entries, :dependent => :delete_all | |
32 | has_and_belongs_to_many :changesets, :order => "#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC" |
|
32 | has_and_belongs_to_many :changesets, :order => "#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC" | |
33 |
|
33 | |||
34 | has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all |
|
34 | has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all | |
35 | has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all |
|
35 | has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all | |
36 |
|
36 | |||
37 | acts_as_nested_set :scope => 'root_id', :dependent => :destroy |
|
37 | acts_as_nested_set :scope => 'root_id', :dependent => :destroy | |
38 | acts_as_attachable :after_add => :attachment_added, :after_remove => :attachment_removed |
|
38 | acts_as_attachable :after_add => :attachment_added, :after_remove => :attachment_removed | |
39 | acts_as_customizable |
|
39 | acts_as_customizable | |
40 | acts_as_watchable |
|
40 | acts_as_watchable | |
41 | acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"], |
|
41 | acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"], | |
42 | :include => [:project, :journals], |
|
42 | :include => [:project, :journals], | |
43 | # sort by id so that limited eager loading doesn't break with postgresql |
|
43 | # sort by id so that limited eager loading doesn't break with postgresql | |
44 | :order_column => "#{table_name}.id" |
|
44 | :order_column => "#{table_name}.id" | |
45 | acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id} (#{o.status}): #{o.subject}"}, |
|
45 | acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id} (#{o.status}): #{o.subject}"}, | |
46 | :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}}, |
|
46 | :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}}, | |
47 | :type => Proc.new {|o| 'issue' + (o.closed? ? ' closed' : '') } |
|
47 | :type => Proc.new {|o| 'issue' + (o.closed? ? ' closed' : '') } | |
48 |
|
48 | |||
49 | acts_as_activity_provider :find_options => {:include => [:project, :author, :tracker]}, |
|
49 | acts_as_activity_provider :find_options => {:include => [:project, :author, :tracker]}, | |
50 | :author_key => :author_id |
|
50 | :author_key => :author_id | |
51 |
|
51 | |||
52 | DONE_RATIO_OPTIONS = %w(issue_field issue_status) |
|
52 | DONE_RATIO_OPTIONS = %w(issue_field issue_status) | |
53 |
|
53 | |||
54 | attr_reader :current_journal |
|
54 | attr_reader :current_journal | |
55 |
|
55 | |||
56 | validates_presence_of :subject, :priority, :project, :tracker, :author, :status |
|
56 | validates_presence_of :subject, :priority, :project, :tracker, :author, :status | |
57 |
|
57 | |||
58 | validates_length_of :subject, :maximum => 255 |
|
58 | validates_length_of :subject, :maximum => 255 | |
59 | validates_inclusion_of :done_ratio, :in => 0..100 |
|
59 | validates_inclusion_of :done_ratio, :in => 0..100 | |
60 | validates_numericality_of :estimated_hours, :allow_nil => true |
|
60 | validates_numericality_of :estimated_hours, :allow_nil => true | |
61 | validate :validate_issue |
|
61 | validate :validate_issue | |
62 |
|
62 | |||
63 | named_scope :visible, lambda {|*args| { :include => :project, |
|
63 | named_scope :visible, lambda {|*args| { :include => :project, | |
64 | :conditions => Issue.visible_condition(args.shift || User.current, *args) } } |
|
64 | :conditions => Issue.visible_condition(args.shift || User.current, *args) } } | |
65 |
|
65 | |||
66 | named_scope :open, lambda {|*args| |
|
66 | named_scope :open, lambda {|*args| | |
67 | is_closed = args.size > 0 ? !args.first : false |
|
67 | is_closed = args.size > 0 ? !args.first : false | |
68 | {:conditions => ["#{IssueStatus.table_name}.is_closed = ?", is_closed], :include => :status} |
|
68 | {:conditions => ["#{IssueStatus.table_name}.is_closed = ?", is_closed], :include => :status} | |
69 | } |
|
69 | } | |
70 |
|
70 | |||
71 | named_scope :recently_updated, :order => "#{Issue.table_name}.updated_on DESC" |
|
71 | named_scope :recently_updated, :order => "#{Issue.table_name}.updated_on DESC" | |
72 | named_scope :with_limit, lambda { |limit| { :limit => limit} } |
|
72 | named_scope :with_limit, lambda { |limit| { :limit => limit} } | |
73 | named_scope :on_active_project, :include => [:status, :project, :tracker], |
|
73 | named_scope :on_active_project, :include => [:status, :project, :tracker], | |
74 | :conditions => ["#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"] |
|
74 | :conditions => ["#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"] | |
75 |
|
75 | |||
76 | before_create :default_assign |
|
76 | before_create :default_assign | |
77 | before_save :close_duplicates, :update_done_ratio_from_issue_status |
|
77 | before_save :close_duplicates, :update_done_ratio_from_issue_status | |
78 | after_save {|issue| issue.send :after_project_change if !issue.id_changed? && issue.project_id_changed?} |
|
78 | after_save {|issue| issue.send :after_project_change if !issue.id_changed? && issue.project_id_changed?} | |
79 | after_save :reschedule_following_issues, :update_nested_set_attributes, :update_parent_attributes, :create_journal |
|
79 | after_save :reschedule_following_issues, :update_nested_set_attributes, :update_parent_attributes, :create_journal | |
80 | after_destroy :update_parent_attributes |
|
80 | after_destroy :update_parent_attributes | |
81 |
|
81 | |||
82 | # Returns a SQL conditions string used to find all issues visible by the specified user |
|
82 | # Returns a SQL conditions string used to find all issues visible by the specified user | |
83 | def self.visible_condition(user, options={}) |
|
83 | def self.visible_condition(user, options={}) | |
84 | Project.allowed_to_condition(user, :view_issues, options) do |role, user| |
|
84 | Project.allowed_to_condition(user, :view_issues, options) do |role, user| | |
85 | case role.issues_visibility |
|
85 | case role.issues_visibility | |
86 | when 'all' |
|
86 | when 'all' | |
87 | nil |
|
87 | nil | |
88 | when 'default' |
|
88 | when 'default' | |
89 | user_ids = [user.id] + user.groups.map(&:id) |
|
89 | user_ids = [user.id] + user.groups.map(&:id) | |
90 | "(#{table_name}.is_private = #{connection.quoted_false} OR #{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))" |
|
90 | "(#{table_name}.is_private = #{connection.quoted_false} OR #{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))" | |
91 | when 'own' |
|
91 | when 'own' | |
92 | user_ids = [user.id] + user.groups.map(&:id) |
|
92 | user_ids = [user.id] + user.groups.map(&:id) | |
93 | "(#{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))" |
|
93 | "(#{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))" | |
94 | else |
|
94 | else | |
95 | '1=0' |
|
95 | '1=0' | |
96 | end |
|
96 | end | |
97 | end |
|
97 | end | |
98 | end |
|
98 | end | |
99 |
|
99 | |||
100 | # Returns true if usr or current user is allowed to view the issue |
|
100 | # Returns true if usr or current user is allowed to view the issue | |
101 | def visible?(usr=nil) |
|
101 | def visible?(usr=nil) | |
102 | (usr || User.current).allowed_to?(:view_issues, self.project) do |role, user| |
|
102 | (usr || User.current).allowed_to?(:view_issues, self.project) do |role, user| | |
103 | case role.issues_visibility |
|
103 | case role.issues_visibility | |
104 | when 'all' |
|
104 | when 'all' | |
105 | true |
|
105 | true | |
106 | when 'default' |
|
106 | when 'default' | |
107 | !self.is_private? || self.author == user || user.is_or_belongs_to?(assigned_to) |
|
107 | !self.is_private? || self.author == user || user.is_or_belongs_to?(assigned_to) | |
108 | when 'own' |
|
108 | when 'own' | |
109 | self.author == user || user.is_or_belongs_to?(assigned_to) |
|
109 | self.author == user || user.is_or_belongs_to?(assigned_to) | |
110 | else |
|
110 | else | |
111 | false |
|
111 | false | |
112 | end |
|
112 | end | |
113 | end |
|
113 | end | |
114 | end |
|
114 | end | |
115 |
|
115 | |||
116 | def initialize(attributes=nil, *args) |
|
116 | def initialize(attributes=nil, *args) | |
117 | super |
|
117 | super | |
118 | if new_record? |
|
118 | if new_record? | |
119 | # set default values for new records only |
|
119 | # set default values for new records only | |
120 | self.status ||= IssueStatus.default |
|
120 | self.status ||= IssueStatus.default | |
121 | self.priority ||= IssuePriority.default |
|
121 | self.priority ||= IssuePriority.default | |
|
122 | self.watcher_user_ids = [] | |||
122 | end |
|
123 | end | |
123 | end |
|
124 | end | |
124 |
|
125 | |||
125 | # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields |
|
126 | # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields | |
126 | def available_custom_fields |
|
127 | def available_custom_fields | |
127 | (project && tracker) ? (project.all_issue_custom_fields & tracker.custom_fields.all) : [] |
|
128 | (project && tracker) ? (project.all_issue_custom_fields & tracker.custom_fields.all) : [] | |
128 | end |
|
129 | end | |
129 |
|
130 | |||
130 | # Copies attributes from another issue, arg can be an id or an Issue |
|
131 | # Copies attributes from another issue, arg can be an id or an Issue | |
131 | def copy_from(arg) |
|
132 | def copy_from(arg) | |
132 | issue = arg.is_a?(Issue) ? arg : Issue.visible.find(arg) |
|
133 | issue = arg.is_a?(Issue) ? arg : Issue.visible.find(arg) | |
133 | self.attributes = issue.attributes.dup.except("id", "root_id", "parent_id", "lft", "rgt", "created_on", "updated_on") |
|
134 | self.attributes = issue.attributes.dup.except("id", "root_id", "parent_id", "lft", "rgt", "created_on", "updated_on") | |
134 | self.custom_field_values = issue.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h} |
|
135 | self.custom_field_values = issue.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h} | |
135 | self.status = issue.status |
|
136 | self.status = issue.status | |
136 | self.author = User.current |
|
137 | self.author = User.current | |
137 | @copied_from = issue |
|
138 | @copied_from = issue | |
138 | self |
|
139 | self | |
139 | end |
|
140 | end | |
140 |
|
141 | |||
141 | # Returns an unsaved copy of the issue |
|
142 | # Returns an unsaved copy of the issue | |
142 | def copy(attributes=nil) |
|
143 | def copy(attributes=nil) | |
143 | copy = self.class.new.copy_from(self) |
|
144 | copy = self.class.new.copy_from(self) | |
144 | copy.attributes = attributes if attributes |
|
145 | copy.attributes = attributes if attributes | |
145 | copy |
|
146 | copy | |
146 | end |
|
147 | end | |
147 |
|
148 | |||
148 | # Returns true if the issue is a copy |
|
149 | # Returns true if the issue is a copy | |
149 | def copy? |
|
150 | def copy? | |
150 | @copied_from.present? |
|
151 | @copied_from.present? | |
151 | end |
|
152 | end | |
152 |
|
153 | |||
153 | # Moves/copies an issue to a new project and tracker |
|
154 | # Moves/copies an issue to a new project and tracker | |
154 | # Returns the moved/copied issue on success, false on failure |
|
155 | # Returns the moved/copied issue on success, false on failure | |
155 | def move_to_project(new_project, new_tracker=nil, options={}) |
|
156 | def move_to_project(new_project, new_tracker=nil, options={}) | |
156 | ActiveSupport::Deprecation.warn "Issue#move_to_project is deprecated, use #project= instead." |
|
157 | ActiveSupport::Deprecation.warn "Issue#move_to_project is deprecated, use #project= instead." | |
157 |
|
158 | |||
158 | if options[:copy] |
|
159 | if options[:copy] | |
159 | issue = self.copy |
|
160 | issue = self.copy | |
160 | else |
|
161 | else | |
161 | issue = self |
|
162 | issue = self | |
162 | end |
|
163 | end | |
163 |
|
164 | |||
164 | issue.init_journal(User.current, options[:notes]) |
|
165 | issue.init_journal(User.current, options[:notes]) | |
165 |
|
166 | |||
166 | # Preserve previous behaviour |
|
167 | # Preserve previous behaviour | |
167 | # #move_to_project doesn't change tracker automatically |
|
168 | # #move_to_project doesn't change tracker automatically | |
168 | issue.send :project=, new_project, true |
|
169 | issue.send :project=, new_project, true | |
169 | if new_tracker |
|
170 | if new_tracker | |
170 | issue.tracker = new_tracker |
|
171 | issue.tracker = new_tracker | |
171 | end |
|
172 | end | |
172 | # Allow bulk setting of attributes on the issue |
|
173 | # Allow bulk setting of attributes on the issue | |
173 | if options[:attributes] |
|
174 | if options[:attributes] | |
174 | issue.attributes = options[:attributes] |
|
175 | issue.attributes = options[:attributes] | |
175 | end |
|
176 | end | |
176 |
|
177 | |||
177 | issue.save ? issue : false |
|
178 | issue.save ? issue : false | |
178 | end |
|
179 | end | |
179 |
|
180 | |||
180 | def status_id=(sid) |
|
181 | def status_id=(sid) | |
181 | self.status = nil |
|
182 | self.status = nil | |
182 | write_attribute(:status_id, sid) |
|
183 | write_attribute(:status_id, sid) | |
183 | end |
|
184 | end | |
184 |
|
185 | |||
185 | def priority_id=(pid) |
|
186 | def priority_id=(pid) | |
186 | self.priority = nil |
|
187 | self.priority = nil | |
187 | write_attribute(:priority_id, pid) |
|
188 | write_attribute(:priority_id, pid) | |
188 | end |
|
189 | end | |
189 |
|
190 | |||
190 | def category_id=(cid) |
|
191 | def category_id=(cid) | |
191 | self.category = nil |
|
192 | self.category = nil | |
192 | write_attribute(:category_id, cid) |
|
193 | write_attribute(:category_id, cid) | |
193 | end |
|
194 | end | |
194 |
|
195 | |||
195 | def fixed_version_id=(vid) |
|
196 | def fixed_version_id=(vid) | |
196 | self.fixed_version = nil |
|
197 | self.fixed_version = nil | |
197 | write_attribute(:fixed_version_id, vid) |
|
198 | write_attribute(:fixed_version_id, vid) | |
198 | end |
|
199 | end | |
199 |
|
200 | |||
200 | def tracker_id=(tid) |
|
201 | def tracker_id=(tid) | |
201 | self.tracker = nil |
|
202 | self.tracker = nil | |
202 | result = write_attribute(:tracker_id, tid) |
|
203 | result = write_attribute(:tracker_id, tid) | |
203 | @custom_field_values = nil |
|
204 | @custom_field_values = nil | |
204 | result |
|
205 | result | |
205 | end |
|
206 | end | |
206 |
|
207 | |||
207 | def project_id=(project_id) |
|
208 | def project_id=(project_id) | |
208 | if project_id.to_s != self.project_id.to_s |
|
209 | if project_id.to_s != self.project_id.to_s | |
209 | self.project = (project_id.present? ? Project.find_by_id(project_id) : nil) |
|
210 | self.project = (project_id.present? ? Project.find_by_id(project_id) : nil) | |
210 | end |
|
211 | end | |
211 | end |
|
212 | end | |
212 |
|
213 | |||
213 | def project=(project, keep_tracker=false) |
|
214 | def project=(project, keep_tracker=false) | |
214 | project_was = self.project |
|
215 | project_was = self.project | |
215 | write_attribute(:project_id, project ? project.id : nil) |
|
216 | write_attribute(:project_id, project ? project.id : nil) | |
216 | association_instance_set('project', project) |
|
217 | association_instance_set('project', project) | |
217 | if project_was && project && project_was != project |
|
218 | if project_was && project && project_was != project | |
218 | unless keep_tracker || project.trackers.include?(tracker) |
|
219 | unless keep_tracker || project.trackers.include?(tracker) | |
219 | self.tracker = project.trackers.first |
|
220 | self.tracker = project.trackers.first | |
220 | end |
|
221 | end | |
221 | # Reassign to the category with same name if any |
|
222 | # Reassign to the category with same name if any | |
222 | if category |
|
223 | if category | |
223 | self.category = project.issue_categories.find_by_name(category.name) |
|
224 | self.category = project.issue_categories.find_by_name(category.name) | |
224 | end |
|
225 | end | |
225 | # Keep the fixed_version if it's still valid in the new_project |
|
226 | # Keep the fixed_version if it's still valid in the new_project | |
226 | if fixed_version && fixed_version.project != project && !project.shared_versions.include?(fixed_version) |
|
227 | if fixed_version && fixed_version.project != project && !project.shared_versions.include?(fixed_version) | |
227 | self.fixed_version = nil |
|
228 | self.fixed_version = nil | |
228 | end |
|
229 | end | |
229 | if parent && parent.project_id != project_id |
|
230 | if parent && parent.project_id != project_id | |
230 | self.parent_issue_id = nil |
|
231 | self.parent_issue_id = nil | |
231 | end |
|
232 | end | |
232 | @custom_field_values = nil |
|
233 | @custom_field_values = nil | |
233 | end |
|
234 | end | |
234 | end |
|
235 | end | |
235 |
|
236 | |||
236 | def description=(arg) |
|
237 | def description=(arg) | |
237 | if arg.is_a?(String) |
|
238 | if arg.is_a?(String) | |
238 | arg = arg.gsub(/(\r\n|\n|\r)/, "\r\n") |
|
239 | arg = arg.gsub(/(\r\n|\n|\r)/, "\r\n") | |
239 | end |
|
240 | end | |
240 | write_attribute(:description, arg) |
|
241 | write_attribute(:description, arg) | |
241 | end |
|
242 | end | |
242 |
|
243 | |||
243 | # Overrides attributes= so that project and tracker get assigned first |
|
244 | # Overrides attributes= so that project and tracker get assigned first | |
244 | def attributes_with_project_and_tracker_first=(new_attributes, *args) |
|
245 | def attributes_with_project_and_tracker_first=(new_attributes, *args) | |
245 | return if new_attributes.nil? |
|
246 | return if new_attributes.nil? | |
246 | attrs = new_attributes.dup |
|
247 | attrs = new_attributes.dup | |
247 | attrs.stringify_keys! |
|
248 | attrs.stringify_keys! | |
248 |
|
249 | |||
249 | %w(project project_id tracker tracker_id).each do |attr| |
|
250 | %w(project project_id tracker tracker_id).each do |attr| | |
250 | if attrs.has_key?(attr) |
|
251 | if attrs.has_key?(attr) | |
251 | send "#{attr}=", attrs.delete(attr) |
|
252 | send "#{attr}=", attrs.delete(attr) | |
252 | end |
|
253 | end | |
253 | end |
|
254 | end | |
254 | send :attributes_without_project_and_tracker_first=, attrs, *args |
|
255 | send :attributes_without_project_and_tracker_first=, attrs, *args | |
255 | end |
|
256 | end | |
256 | # Do not redefine alias chain on reload (see #4838) |
|
257 | # Do not redefine alias chain on reload (see #4838) | |
257 | alias_method_chain(:attributes=, :project_and_tracker_first) unless method_defined?(:attributes_without_project_and_tracker_first=) |
|
258 | alias_method_chain(:attributes=, :project_and_tracker_first) unless method_defined?(:attributes_without_project_and_tracker_first=) | |
258 |
|
259 | |||
259 | def estimated_hours=(h) |
|
260 | def estimated_hours=(h) | |
260 | write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h) |
|
261 | write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h) | |
261 | end |
|
262 | end | |
262 |
|
263 | |||
263 | safe_attributes 'project_id', |
|
264 | safe_attributes 'project_id', | |
264 | :if => lambda {|issue, user| |
|
265 | :if => lambda {|issue, user| | |
265 | if issue.new_record? |
|
266 | if issue.new_record? | |
266 | issue.copy? |
|
267 | issue.copy? | |
267 | elsif user.allowed_to?(:move_issues, issue.project) |
|
268 | elsif user.allowed_to?(:move_issues, issue.project) | |
268 | projects = Issue.allowed_target_projects_on_move(user) |
|
269 | projects = Issue.allowed_target_projects_on_move(user) | |
269 | projects.include?(issue.project) && projects.size > 1 |
|
270 | projects.include?(issue.project) && projects.size > 1 | |
270 | end |
|
271 | end | |
271 | } |
|
272 | } | |
272 |
|
273 | |||
273 | safe_attributes 'tracker_id', |
|
274 | safe_attributes 'tracker_id', | |
274 | 'status_id', |
|
275 | 'status_id', | |
275 | 'category_id', |
|
276 | 'category_id', | |
276 | 'assigned_to_id', |
|
277 | 'assigned_to_id', | |
277 | 'priority_id', |
|
278 | 'priority_id', | |
278 | 'fixed_version_id', |
|
279 | 'fixed_version_id', | |
279 | 'subject', |
|
280 | 'subject', | |
280 | 'description', |
|
281 | 'description', | |
281 | 'start_date', |
|
282 | 'start_date', | |
282 | 'due_date', |
|
283 | 'due_date', | |
283 | 'done_ratio', |
|
284 | 'done_ratio', | |
284 | 'estimated_hours', |
|
285 | 'estimated_hours', | |
285 | 'custom_field_values', |
|
286 | 'custom_field_values', | |
286 | 'custom_fields', |
|
287 | 'custom_fields', | |
287 | 'lock_version', |
|
288 | 'lock_version', | |
288 | :if => lambda {|issue, user| issue.new_record? || user.allowed_to?(:edit_issues, issue.project) } |
|
289 | :if => lambda {|issue, user| issue.new_record? || user.allowed_to?(:edit_issues, issue.project) } | |
289 |
|
290 | |||
290 | safe_attributes 'status_id', |
|
291 | safe_attributes 'status_id', | |
291 | 'assigned_to_id', |
|
292 | 'assigned_to_id', | |
292 | 'fixed_version_id', |
|
293 | 'fixed_version_id', | |
293 | 'done_ratio', |
|
294 | 'done_ratio', | |
294 | 'lock_version', |
|
295 | 'lock_version', | |
295 | :if => lambda {|issue, user| issue.new_statuses_allowed_to(user).any? } |
|
296 | :if => lambda {|issue, user| issue.new_statuses_allowed_to(user).any? } | |
296 |
|
297 | |||
297 | safe_attributes 'watcher_user_ids', |
|
298 | safe_attributes 'watcher_user_ids', | |
298 | :if => lambda {|issue, user| issue.new_record? && user.allowed_to?(:add_issue_watchers, issue.project)} |
|
299 | :if => lambda {|issue, user| issue.new_record? && user.allowed_to?(:add_issue_watchers, issue.project)} | |
299 |
|
300 | |||
300 | safe_attributes 'is_private', |
|
301 | safe_attributes 'is_private', | |
301 | :if => lambda {|issue, user| |
|
302 | :if => lambda {|issue, user| | |
302 | user.allowed_to?(:set_issues_private, issue.project) || |
|
303 | user.allowed_to?(:set_issues_private, issue.project) || | |
303 | (issue.author == user && user.allowed_to?(:set_own_issues_private, issue.project)) |
|
304 | (issue.author == user && user.allowed_to?(:set_own_issues_private, issue.project)) | |
304 | } |
|
305 | } | |
305 |
|
306 | |||
306 | safe_attributes 'parent_issue_id', |
|
307 | safe_attributes 'parent_issue_id', | |
307 | :if => lambda {|issue, user| (issue.new_record? || user.allowed_to?(:edit_issues, issue.project)) && |
|
308 | :if => lambda {|issue, user| (issue.new_record? || user.allowed_to?(:edit_issues, issue.project)) && | |
308 | user.allowed_to?(:manage_subtasks, issue.project)} |
|
309 | user.allowed_to?(:manage_subtasks, issue.project)} | |
309 |
|
310 | |||
310 | # Safely sets attributes |
|
311 | # Safely sets attributes | |
311 | # Should be called from controllers instead of #attributes= |
|
312 | # Should be called from controllers instead of #attributes= | |
312 | # attr_accessible is too rough because we still want things like |
|
313 | # attr_accessible is too rough because we still want things like | |
313 | # Issue.new(:project => foo) to work |
|
314 | # Issue.new(:project => foo) to work | |
314 | def safe_attributes=(attrs, user=User.current) |
|
315 | def safe_attributes=(attrs, user=User.current) | |
315 | return unless attrs.is_a?(Hash) |
|
316 | return unless attrs.is_a?(Hash) | |
316 |
|
317 | |||
317 | # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed |
|
318 | # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed | |
318 | attrs = delete_unsafe_attributes(attrs, user) |
|
319 | attrs = delete_unsafe_attributes(attrs, user) | |
319 | return if attrs.empty? |
|
320 | return if attrs.empty? | |
320 |
|
321 | |||
321 | # Project and Tracker must be set before since new_statuses_allowed_to depends on it. |
|
322 | # Project and Tracker must be set before since new_statuses_allowed_to depends on it. | |
322 | if p = attrs.delete('project_id') |
|
323 | if p = attrs.delete('project_id') | |
323 | if allowed_target_projects(user).collect(&:id).include?(p.to_i) |
|
324 | if allowed_target_projects(user).collect(&:id).include?(p.to_i) | |
324 | self.project_id = p |
|
325 | self.project_id = p | |
325 | end |
|
326 | end | |
326 | end |
|
327 | end | |
327 |
|
328 | |||
328 | if t = attrs.delete('tracker_id') |
|
329 | if t = attrs.delete('tracker_id') | |
329 | self.tracker_id = t |
|
330 | self.tracker_id = t | |
330 | end |
|
331 | end | |
331 |
|
332 | |||
332 | if attrs['status_id'] |
|
333 | if attrs['status_id'] | |
333 | unless new_statuses_allowed_to(user).collect(&:id).include?(attrs['status_id'].to_i) |
|
334 | unless new_statuses_allowed_to(user).collect(&:id).include?(attrs['status_id'].to_i) | |
334 | attrs.delete('status_id') |
|
335 | attrs.delete('status_id') | |
335 | end |
|
336 | end | |
336 | end |
|
337 | end | |
337 |
|
338 | |||
338 | unless leaf? |
|
339 | unless leaf? | |
339 | attrs.reject! {|k,v| %w(priority_id done_ratio start_date due_date estimated_hours).include?(k)} |
|
340 | attrs.reject! {|k,v| %w(priority_id done_ratio start_date due_date estimated_hours).include?(k)} | |
340 | end |
|
341 | end | |
341 |
|
342 | |||
342 | if attrs['parent_issue_id'].present? |
|
343 | if attrs['parent_issue_id'].present? | |
343 | attrs.delete('parent_issue_id') unless Issue.visible(user).exists?(attrs['parent_issue_id'].to_i) |
|
344 | attrs.delete('parent_issue_id') unless Issue.visible(user).exists?(attrs['parent_issue_id'].to_i) | |
344 | end |
|
345 | end | |
345 |
|
346 | |||
346 | # mass-assignment security bypass |
|
347 | # mass-assignment security bypass | |
347 | self.send :attributes=, attrs, false |
|
348 | self.send :attributes=, attrs, false | |
348 | end |
|
349 | end | |
349 |
|
350 | |||
350 | def done_ratio |
|
351 | def done_ratio | |
351 | if Issue.use_status_for_done_ratio? && status && status.default_done_ratio |
|
352 | if Issue.use_status_for_done_ratio? && status && status.default_done_ratio | |
352 | status.default_done_ratio |
|
353 | status.default_done_ratio | |
353 | else |
|
354 | else | |
354 | read_attribute(:done_ratio) |
|
355 | read_attribute(:done_ratio) | |
355 | end |
|
356 | end | |
356 | end |
|
357 | end | |
357 |
|
358 | |||
358 | def self.use_status_for_done_ratio? |
|
359 | def self.use_status_for_done_ratio? | |
359 | Setting.issue_done_ratio == 'issue_status' |
|
360 | Setting.issue_done_ratio == 'issue_status' | |
360 | end |
|
361 | end | |
361 |
|
362 | |||
362 | def self.use_field_for_done_ratio? |
|
363 | def self.use_field_for_done_ratio? | |
363 | Setting.issue_done_ratio == 'issue_field' |
|
364 | Setting.issue_done_ratio == 'issue_field' | |
364 | end |
|
365 | end | |
365 |
|
366 | |||
366 | def validate_issue |
|
367 | def validate_issue | |
367 | if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty? |
|
368 | if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty? | |
368 | errors.add :due_date, :not_a_date |
|
369 | errors.add :due_date, :not_a_date | |
369 | end |
|
370 | end | |
370 |
|
371 | |||
371 | if self.due_date and self.start_date and self.due_date < self.start_date |
|
372 | if self.due_date and self.start_date and self.due_date < self.start_date | |
372 | errors.add :due_date, :greater_than_start_date |
|
373 | errors.add :due_date, :greater_than_start_date | |
373 | end |
|
374 | end | |
374 |
|
375 | |||
375 | if start_date && soonest_start && start_date < soonest_start |
|
376 | if start_date && soonest_start && start_date < soonest_start | |
376 | errors.add :start_date, :invalid |
|
377 | errors.add :start_date, :invalid | |
377 | end |
|
378 | end | |
378 |
|
379 | |||
379 | if fixed_version |
|
380 | if fixed_version | |
380 | if !assignable_versions.include?(fixed_version) |
|
381 | if !assignable_versions.include?(fixed_version) | |
381 | errors.add :fixed_version_id, :inclusion |
|
382 | errors.add :fixed_version_id, :inclusion | |
382 | elsif reopened? && fixed_version.closed? |
|
383 | elsif reopened? && fixed_version.closed? | |
383 | errors.add :base, I18n.t(:error_can_not_reopen_issue_on_closed_version) |
|
384 | errors.add :base, I18n.t(:error_can_not_reopen_issue_on_closed_version) | |
384 | end |
|
385 | end | |
385 | end |
|
386 | end | |
386 |
|
387 | |||
387 | # Checks that the issue can not be added/moved to a disabled tracker |
|
388 | # Checks that the issue can not be added/moved to a disabled tracker | |
388 | if project && (tracker_id_changed? || project_id_changed?) |
|
389 | if project && (tracker_id_changed? || project_id_changed?) | |
389 | unless project.trackers.include?(tracker) |
|
390 | unless project.trackers.include?(tracker) | |
390 | errors.add :tracker_id, :inclusion |
|
391 | errors.add :tracker_id, :inclusion | |
391 | end |
|
392 | end | |
392 | end |
|
393 | end | |
393 |
|
394 | |||
394 | # Checks parent issue assignment |
|
395 | # Checks parent issue assignment | |
395 | if @parent_issue |
|
396 | if @parent_issue | |
396 | if @parent_issue.project_id != project_id |
|
397 | if @parent_issue.project_id != project_id | |
397 | errors.add :parent_issue_id, :not_same_project |
|
398 | errors.add :parent_issue_id, :not_same_project | |
398 | elsif !new_record? |
|
399 | elsif !new_record? | |
399 | # moving an existing issue |
|
400 | # moving an existing issue | |
400 | if @parent_issue.root_id != root_id |
|
401 | if @parent_issue.root_id != root_id | |
401 | # we can always move to another tree |
|
402 | # we can always move to another tree | |
402 | elsif move_possible?(@parent_issue) |
|
403 | elsif move_possible?(@parent_issue) | |
403 | # move accepted inside tree |
|
404 | # move accepted inside tree | |
404 | else |
|
405 | else | |
405 | errors.add :parent_issue_id, :not_a_valid_parent |
|
406 | errors.add :parent_issue_id, :not_a_valid_parent | |
406 | end |
|
407 | end | |
407 | end |
|
408 | end | |
408 | end |
|
409 | end | |
409 | end |
|
410 | end | |
410 |
|
411 | |||
411 | # Set the done_ratio using the status if that setting is set. This will keep the done_ratios |
|
412 | # Set the done_ratio using the status if that setting is set. This will keep the done_ratios | |
412 | # even if the user turns off the setting later |
|
413 | # even if the user turns off the setting later | |
413 | def update_done_ratio_from_issue_status |
|
414 | def update_done_ratio_from_issue_status | |
414 | if Issue.use_status_for_done_ratio? && status && status.default_done_ratio |
|
415 | if Issue.use_status_for_done_ratio? && status && status.default_done_ratio | |
415 | self.done_ratio = status.default_done_ratio |
|
416 | self.done_ratio = status.default_done_ratio | |
416 | end |
|
417 | end | |
417 | end |
|
418 | end | |
418 |
|
419 | |||
419 | def init_journal(user, notes = "") |
|
420 | def init_journal(user, notes = "") | |
420 | @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes) |
|
421 | @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes) | |
421 | if new_record? |
|
422 | if new_record? | |
422 | @current_journal.notify = false |
|
423 | @current_journal.notify = false | |
423 | else |
|
424 | else | |
424 | @attributes_before_change = attributes.dup |
|
425 | @attributes_before_change = attributes.dup | |
425 | @custom_values_before_change = {} |
|
426 | @custom_values_before_change = {} | |
426 | self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value } |
|
427 | self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value } | |
427 | end |
|
428 | end | |
428 | # Make sure updated_on is updated when adding a note. |
|
429 | # Make sure updated_on is updated when adding a note. | |
429 | updated_on_will_change! |
|
430 | updated_on_will_change! | |
430 | @current_journal |
|
431 | @current_journal | |
431 | end |
|
432 | end | |
432 |
|
433 | |||
433 | # Return true if the issue is closed, otherwise false |
|
434 | # Return true if the issue is closed, otherwise false | |
434 | def closed? |
|
435 | def closed? | |
435 | self.status.is_closed? |
|
436 | self.status.is_closed? | |
436 | end |
|
437 | end | |
437 |
|
438 | |||
438 | # Return true if the issue is being reopened |
|
439 | # Return true if the issue is being reopened | |
439 | def reopened? |
|
440 | def reopened? | |
440 | if !new_record? && status_id_changed? |
|
441 | if !new_record? && status_id_changed? | |
441 | status_was = IssueStatus.find_by_id(status_id_was) |
|
442 | status_was = IssueStatus.find_by_id(status_id_was) | |
442 | status_new = IssueStatus.find_by_id(status_id) |
|
443 | status_new = IssueStatus.find_by_id(status_id) | |
443 | if status_was && status_new && status_was.is_closed? && !status_new.is_closed? |
|
444 | if status_was && status_new && status_was.is_closed? && !status_new.is_closed? | |
444 | return true |
|
445 | return true | |
445 | end |
|
446 | end | |
446 | end |
|
447 | end | |
447 | false |
|
448 | false | |
448 | end |
|
449 | end | |
449 |
|
450 | |||
450 | # Return true if the issue is being closed |
|
451 | # Return true if the issue is being closed | |
451 | def closing? |
|
452 | def closing? | |
452 | if !new_record? && status_id_changed? |
|
453 | if !new_record? && status_id_changed? | |
453 | status_was = IssueStatus.find_by_id(status_id_was) |
|
454 | status_was = IssueStatus.find_by_id(status_id_was) | |
454 | status_new = IssueStatus.find_by_id(status_id) |
|
455 | status_new = IssueStatus.find_by_id(status_id) | |
455 | if status_was && status_new && !status_was.is_closed? && status_new.is_closed? |
|
456 | if status_was && status_new && !status_was.is_closed? && status_new.is_closed? | |
456 | return true |
|
457 | return true | |
457 | end |
|
458 | end | |
458 | end |
|
459 | end | |
459 | false |
|
460 | false | |
460 | end |
|
461 | end | |
461 |
|
462 | |||
462 | # Returns true if the issue is overdue |
|
463 | # Returns true if the issue is overdue | |
463 | def overdue? |
|
464 | def overdue? | |
464 | !due_date.nil? && (due_date < Date.today) && !status.is_closed? |
|
465 | !due_date.nil? && (due_date < Date.today) && !status.is_closed? | |
465 | end |
|
466 | end | |
466 |
|
467 | |||
467 | # Is the amount of work done less than it should for the due date |
|
468 | # Is the amount of work done less than it should for the due date | |
468 | def behind_schedule? |
|
469 | def behind_schedule? | |
469 | return false if start_date.nil? || due_date.nil? |
|
470 | return false if start_date.nil? || due_date.nil? | |
470 | done_date = start_date + ((due_date - start_date+1)* done_ratio/100).floor |
|
471 | done_date = start_date + ((due_date - start_date+1)* done_ratio/100).floor | |
471 | return done_date <= Date.today |
|
472 | return done_date <= Date.today | |
472 | end |
|
473 | end | |
473 |
|
474 | |||
474 | # Does this issue have children? |
|
475 | # Does this issue have children? | |
475 | def children? |
|
476 | def children? | |
476 | !leaf? |
|
477 | !leaf? | |
477 | end |
|
478 | end | |
478 |
|
479 | |||
479 | # Users the issue can be assigned to |
|
480 | # Users the issue can be assigned to | |
480 | def assignable_users |
|
481 | def assignable_users | |
481 | users = project.assignable_users |
|
482 | users = project.assignable_users | |
482 | users << author if author |
|
483 | users << author if author | |
483 | users << assigned_to if assigned_to |
|
484 | users << assigned_to if assigned_to | |
484 | users.uniq.sort |
|
485 | users.uniq.sort | |
485 | end |
|
486 | end | |
486 |
|
487 | |||
487 | # Versions that the issue can be assigned to |
|
488 | # Versions that the issue can be assigned to | |
488 | def assignable_versions |
|
489 | def assignable_versions | |
489 | @assignable_versions ||= (project.shared_versions.open + [Version.find_by_id(fixed_version_id_was)]).compact.uniq.sort |
|
490 | @assignable_versions ||= (project.shared_versions.open + [Version.find_by_id(fixed_version_id_was)]).compact.uniq.sort | |
490 | end |
|
491 | end | |
491 |
|
492 | |||
492 | # Returns true if this issue is blocked by another issue that is still open |
|
493 | # Returns true if this issue is blocked by another issue that is still open | |
493 | def blocked? |
|
494 | def blocked? | |
494 | !relations_to.detect {|ir| ir.relation_type == 'blocks' && !ir.issue_from.closed?}.nil? |
|
495 | !relations_to.detect {|ir| ir.relation_type == 'blocks' && !ir.issue_from.closed?}.nil? | |
495 | end |
|
496 | end | |
496 |
|
497 | |||
497 | # Returns an array of status that user is able to apply |
|
498 | # Returns an array of status that user is able to apply | |
498 | def new_statuses_allowed_to(user, include_default=false) |
|
499 | def new_statuses_allowed_to(user, include_default=false) | |
499 | statuses = status.find_new_statuses_allowed_to( |
|
500 | statuses = status.find_new_statuses_allowed_to( | |
500 | user.roles_for_project(project), |
|
501 | user.roles_for_project(project), | |
501 | tracker, |
|
502 | tracker, | |
502 | author == user, |
|
503 | author == user, | |
503 | assigned_to_id_changed? ? assigned_to_id_was == user.id : assigned_to_id == user.id |
|
504 | assigned_to_id_changed? ? assigned_to_id_was == user.id : assigned_to_id == user.id | |
504 | ) |
|
505 | ) | |
505 | statuses << status unless statuses.empty? |
|
506 | statuses << status unless statuses.empty? | |
506 | statuses << IssueStatus.default if include_default |
|
507 | statuses << IssueStatus.default if include_default | |
507 | statuses = statuses.uniq.sort |
|
508 | statuses = statuses.uniq.sort | |
508 | blocked? ? statuses.reject {|s| s.is_closed?} : statuses |
|
509 | blocked? ? statuses.reject {|s| s.is_closed?} : statuses | |
509 | end |
|
510 | end | |
510 |
|
511 | |||
511 | # Returns the mail adresses of users that should be notified |
|
512 | # Returns the mail adresses of users that should be notified | |
512 | def recipients |
|
513 | def recipients | |
513 | notified = project.notified_users |
|
514 | notified = project.notified_users | |
514 | # Author and assignee are always notified unless they have been |
|
515 | # Author and assignee are always notified unless they have been | |
515 | # locked or don't want to be notified |
|
516 | # locked or don't want to be notified | |
516 | notified << author if author && author.active? && author.notify_about?(self) |
|
517 | notified << author if author && author.active? && author.notify_about?(self) | |
517 | if assigned_to |
|
518 | if assigned_to | |
518 | if assigned_to.is_a?(Group) |
|
519 | if assigned_to.is_a?(Group) | |
519 | notified += assigned_to.users.select {|u| u.active? && u.notify_about?(self)} |
|
520 | notified += assigned_to.users.select {|u| u.active? && u.notify_about?(self)} | |
520 | else |
|
521 | else | |
521 | notified << assigned_to if assigned_to.active? && assigned_to.notify_about?(self) |
|
522 | notified << assigned_to if assigned_to.active? && assigned_to.notify_about?(self) | |
522 | end |
|
523 | end | |
523 | end |
|
524 | end | |
524 | notified.uniq! |
|
525 | notified.uniq! | |
525 | # Remove users that can not view the issue |
|
526 | # Remove users that can not view the issue | |
526 | notified.reject! {|user| !visible?(user)} |
|
527 | notified.reject! {|user| !visible?(user)} | |
527 | notified.collect(&:mail) |
|
528 | notified.collect(&:mail) | |
528 | end |
|
529 | end | |
529 |
|
530 | |||
530 | # Returns the number of hours spent on this issue |
|
531 | # Returns the number of hours spent on this issue | |
531 | def spent_hours |
|
532 | def spent_hours | |
532 | @spent_hours ||= time_entries.sum(:hours) || 0 |
|
533 | @spent_hours ||= time_entries.sum(:hours) || 0 | |
533 | end |
|
534 | end | |
534 |
|
535 | |||
535 | # Returns the total number of hours spent on this issue and its descendants |
|
536 | # Returns the total number of hours spent on this issue and its descendants | |
536 | # |
|
537 | # | |
537 | # Example: |
|
538 | # Example: | |
538 | # spent_hours => 0.0 |
|
539 | # spent_hours => 0.0 | |
539 | # spent_hours => 50.2 |
|
540 | # spent_hours => 50.2 | |
540 | def total_spent_hours |
|
541 | def total_spent_hours | |
541 | @total_spent_hours ||= self_and_descendants.sum("#{TimeEntry.table_name}.hours", |
|
542 | @total_spent_hours ||= self_and_descendants.sum("#{TimeEntry.table_name}.hours", | |
542 | :joins => "LEFT JOIN #{TimeEntry.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id").to_f || 0.0 |
|
543 | :joins => "LEFT JOIN #{TimeEntry.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id").to_f || 0.0 | |
543 | end |
|
544 | end | |
544 |
|
545 | |||
545 | def relations |
|
546 | def relations | |
546 | @relations ||= (relations_from + relations_to).sort |
|
547 | @relations ||= (relations_from + relations_to).sort | |
547 | end |
|
548 | end | |
548 |
|
549 | |||
549 | # Preloads relations for a collection of issues |
|
550 | # Preloads relations for a collection of issues | |
550 | def self.load_relations(issues) |
|
551 | def self.load_relations(issues) | |
551 | if issues.any? |
|
552 | if issues.any? | |
552 | relations = IssueRelation.all(:conditions => ["issue_from_id IN (:ids) OR issue_to_id IN (:ids)", {:ids => issues.map(&:id)}]) |
|
553 | relations = IssueRelation.all(:conditions => ["issue_from_id IN (:ids) OR issue_to_id IN (:ids)", {:ids => issues.map(&:id)}]) | |
553 | issues.each do |issue| |
|
554 | issues.each do |issue| | |
554 | issue.instance_variable_set "@relations", relations.select {|r| r.issue_from_id == issue.id || r.issue_to_id == issue.id} |
|
555 | issue.instance_variable_set "@relations", relations.select {|r| r.issue_from_id == issue.id || r.issue_to_id == issue.id} | |
555 | end |
|
556 | end | |
556 | end |
|
557 | end | |
557 | end |
|
558 | end | |
558 |
|
559 | |||
559 | # Preloads visible spent time for a collection of issues |
|
560 | # Preloads visible spent time for a collection of issues | |
560 | def self.load_visible_spent_hours(issues, user=User.current) |
|
561 | def self.load_visible_spent_hours(issues, user=User.current) | |
561 | if issues.any? |
|
562 | if issues.any? | |
562 | hours_by_issue_id = TimeEntry.visible(user).sum(:hours, :group => :issue_id) |
|
563 | hours_by_issue_id = TimeEntry.visible(user).sum(:hours, :group => :issue_id) | |
563 | issues.each do |issue| |
|
564 | issues.each do |issue| | |
564 | issue.instance_variable_set "@spent_hours", (hours_by_issue_id[issue.id] || 0) |
|
565 | issue.instance_variable_set "@spent_hours", (hours_by_issue_id[issue.id] || 0) | |
565 | end |
|
566 | end | |
566 | end |
|
567 | end | |
567 | end |
|
568 | end | |
568 |
|
569 | |||
569 | # Finds an issue relation given its id. |
|
570 | # Finds an issue relation given its id. | |
570 | def find_relation(relation_id) |
|
571 | def find_relation(relation_id) | |
571 | IssueRelation.find(relation_id, :conditions => ["issue_to_id = ? OR issue_from_id = ?", id, id]) |
|
572 | IssueRelation.find(relation_id, :conditions => ["issue_to_id = ? OR issue_from_id = ?", id, id]) | |
572 | end |
|
573 | end | |
573 |
|
574 | |||
574 | def all_dependent_issues(except=[]) |
|
575 | def all_dependent_issues(except=[]) | |
575 | except << self |
|
576 | except << self | |
576 | dependencies = [] |
|
577 | dependencies = [] | |
577 | relations_from.each do |relation| |
|
578 | relations_from.each do |relation| | |
578 | if relation.issue_to && !except.include?(relation.issue_to) |
|
579 | if relation.issue_to && !except.include?(relation.issue_to) | |
579 | dependencies << relation.issue_to |
|
580 | dependencies << relation.issue_to | |
580 | dependencies += relation.issue_to.all_dependent_issues(except) |
|
581 | dependencies += relation.issue_to.all_dependent_issues(except) | |
581 | end |
|
582 | end | |
582 | end |
|
583 | end | |
583 | dependencies |
|
584 | dependencies | |
584 | end |
|
585 | end | |
585 |
|
586 | |||
586 | # Returns an array of issues that duplicate this one |
|
587 | # Returns an array of issues that duplicate this one | |
587 | def duplicates |
|
588 | def duplicates | |
588 | relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from} |
|
589 | relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from} | |
589 | end |
|
590 | end | |
590 |
|
591 | |||
591 | # Returns the due date or the target due date if any |
|
592 | # Returns the due date or the target due date if any | |
592 | # Used on gantt chart |
|
593 | # Used on gantt chart | |
593 | def due_before |
|
594 | def due_before | |
594 | due_date || (fixed_version ? fixed_version.effective_date : nil) |
|
595 | due_date || (fixed_version ? fixed_version.effective_date : nil) | |
595 | end |
|
596 | end | |
596 |
|
597 | |||
597 | # Returns the time scheduled for this issue. |
|
598 | # Returns the time scheduled for this issue. | |
598 | # |
|
599 | # | |
599 | # Example: |
|
600 | # Example: | |
600 | # Start Date: 2/26/09, End Date: 3/04/09 |
|
601 | # Start Date: 2/26/09, End Date: 3/04/09 | |
601 | # duration => 6 |
|
602 | # duration => 6 | |
602 | def duration |
|
603 | def duration | |
603 | (start_date && due_date) ? due_date - start_date : 0 |
|
604 | (start_date && due_date) ? due_date - start_date : 0 | |
604 | end |
|
605 | end | |
605 |
|
606 | |||
606 | def soonest_start |
|
607 | def soonest_start | |
607 | @soonest_start ||= ( |
|
608 | @soonest_start ||= ( | |
608 | relations_to.collect{|relation| relation.successor_soonest_start} + |
|
609 | relations_to.collect{|relation| relation.successor_soonest_start} + | |
609 | ancestors.collect(&:soonest_start) |
|
610 | ancestors.collect(&:soonest_start) | |
610 | ).compact.max |
|
611 | ).compact.max | |
611 | end |
|
612 | end | |
612 |
|
613 | |||
613 | def reschedule_after(date) |
|
614 | def reschedule_after(date) | |
614 | return if date.nil? |
|
615 | return if date.nil? | |
615 | if leaf? |
|
616 | if leaf? | |
616 | if start_date.nil? || start_date < date |
|
617 | if start_date.nil? || start_date < date | |
617 | self.start_date, self.due_date = date, date + duration |
|
618 | self.start_date, self.due_date = date, date + duration | |
618 | save |
|
619 | save | |
619 | end |
|
620 | end | |
620 | else |
|
621 | else | |
621 | leaves.each do |leaf| |
|
622 | leaves.each do |leaf| | |
622 | leaf.reschedule_after(date) |
|
623 | leaf.reschedule_after(date) | |
623 | end |
|
624 | end | |
624 | end |
|
625 | end | |
625 | end |
|
626 | end | |
626 |
|
627 | |||
627 | def <=>(issue) |
|
628 | def <=>(issue) | |
628 | if issue.nil? |
|
629 | if issue.nil? | |
629 | -1 |
|
630 | -1 | |
630 | elsif root_id != issue.root_id |
|
631 | elsif root_id != issue.root_id | |
631 | (root_id || 0) <=> (issue.root_id || 0) |
|
632 | (root_id || 0) <=> (issue.root_id || 0) | |
632 | else |
|
633 | else | |
633 | (lft || 0) <=> (issue.lft || 0) |
|
634 | (lft || 0) <=> (issue.lft || 0) | |
634 | end |
|
635 | end | |
635 | end |
|
636 | end | |
636 |
|
637 | |||
637 | def to_s |
|
638 | def to_s | |
638 | "#{tracker} ##{id}: #{subject}" |
|
639 | "#{tracker} ##{id}: #{subject}" | |
639 | end |
|
640 | end | |
640 |
|
641 | |||
641 | # Returns a string of css classes that apply to the issue |
|
642 | # Returns a string of css classes that apply to the issue | |
642 | def css_classes |
|
643 | def css_classes | |
643 | s = "issue status-#{status.position} priority-#{priority.position}" |
|
644 | s = "issue status-#{status.position} priority-#{priority.position}" | |
644 | s << ' closed' if closed? |
|
645 | s << ' closed' if closed? | |
645 | s << ' overdue' if overdue? |
|
646 | s << ' overdue' if overdue? | |
646 | s << ' child' if child? |
|
647 | s << ' child' if child? | |
647 | s << ' parent' unless leaf? |
|
648 | s << ' parent' unless leaf? | |
648 | s << ' private' if is_private? |
|
649 | s << ' private' if is_private? | |
649 | s << ' created-by-me' if User.current.logged? && author_id == User.current.id |
|
650 | s << ' created-by-me' if User.current.logged? && author_id == User.current.id | |
650 | s << ' assigned-to-me' if User.current.logged? && assigned_to_id == User.current.id |
|
651 | s << ' assigned-to-me' if User.current.logged? && assigned_to_id == User.current.id | |
651 | s |
|
652 | s | |
652 | end |
|
653 | end | |
653 |
|
654 | |||
654 | # Saves an issue, time_entry, attachments, and a journal from the parameters |
|
655 | # Saves an issue, time_entry, attachments, and a journal from the parameters | |
655 | # Returns false if save fails |
|
656 | # Returns false if save fails | |
656 | def save_issue_with_child_records(params, existing_time_entry=nil) |
|
657 | def save_issue_with_child_records(params, existing_time_entry=nil) | |
657 | Issue.transaction do |
|
658 | Issue.transaction do | |
658 | if params[:time_entry] && (params[:time_entry][:hours].present? || params[:time_entry][:comments].present?) && User.current.allowed_to?(:log_time, project) |
|
659 | if params[:time_entry] && (params[:time_entry][:hours].present? || params[:time_entry][:comments].present?) && User.current.allowed_to?(:log_time, project) | |
659 | @time_entry = existing_time_entry || TimeEntry.new |
|
660 | @time_entry = existing_time_entry || TimeEntry.new | |
660 | @time_entry.project = project |
|
661 | @time_entry.project = project | |
661 | @time_entry.issue = self |
|
662 | @time_entry.issue = self | |
662 | @time_entry.user = User.current |
|
663 | @time_entry.user = User.current | |
663 | @time_entry.spent_on = User.current.today |
|
664 | @time_entry.spent_on = User.current.today | |
664 | @time_entry.attributes = params[:time_entry] |
|
665 | @time_entry.attributes = params[:time_entry] | |
665 | self.time_entries << @time_entry |
|
666 | self.time_entries << @time_entry | |
666 | end |
|
667 | end | |
667 |
|
668 | |||
668 | if valid? |
|
669 | if valid? | |
669 | attachments = Attachment.attach_files(self, params[:attachments]) |
|
670 | attachments = Attachment.attach_files(self, params[:attachments]) | |
670 | # TODO: Rename hook |
|
671 | # TODO: Rename hook | |
671 | Redmine::Hook.call_hook(:controller_issues_edit_before_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal}) |
|
672 | Redmine::Hook.call_hook(:controller_issues_edit_before_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal}) | |
672 | begin |
|
673 | begin | |
673 | if save |
|
674 | if save | |
674 | # TODO: Rename hook |
|
675 | # TODO: Rename hook | |
675 | Redmine::Hook.call_hook(:controller_issues_edit_after_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal}) |
|
676 | Redmine::Hook.call_hook(:controller_issues_edit_after_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal}) | |
676 | else |
|
677 | else | |
677 | raise ActiveRecord::Rollback |
|
678 | raise ActiveRecord::Rollback | |
678 | end |
|
679 | end | |
679 | rescue ActiveRecord::StaleObjectError |
|
680 | rescue ActiveRecord::StaleObjectError | |
680 | attachments[:files].each(&:destroy) |
|
681 | attachments[:files].each(&:destroy) | |
681 | errors.add :base, l(:notice_locking_conflict) |
|
682 | errors.add :base, l(:notice_locking_conflict) | |
682 | raise ActiveRecord::Rollback |
|
683 | raise ActiveRecord::Rollback | |
683 | end |
|
684 | end | |
684 | end |
|
685 | end | |
685 | end |
|
686 | end | |
686 | end |
|
687 | end | |
687 |
|
688 | |||
688 | # Unassigns issues from +version+ if it's no longer shared with issue's project |
|
689 | # Unassigns issues from +version+ if it's no longer shared with issue's project | |
689 | def self.update_versions_from_sharing_change(version) |
|
690 | def self.update_versions_from_sharing_change(version) | |
690 | # Update issues assigned to the version |
|
691 | # Update issues assigned to the version | |
691 | update_versions(["#{Issue.table_name}.fixed_version_id = ?", version.id]) |
|
692 | update_versions(["#{Issue.table_name}.fixed_version_id = ?", version.id]) | |
692 | end |
|
693 | end | |
693 |
|
694 | |||
694 | # Unassigns issues from versions that are no longer shared |
|
695 | # Unassigns issues from versions that are no longer shared | |
695 | # after +project+ was moved |
|
696 | # after +project+ was moved | |
696 | def self.update_versions_from_hierarchy_change(project) |
|
697 | def self.update_versions_from_hierarchy_change(project) | |
697 | moved_project_ids = project.self_and_descendants.reload.collect(&:id) |
|
698 | moved_project_ids = project.self_and_descendants.reload.collect(&:id) | |
698 | # Update issues of the moved projects and issues assigned to a version of a moved project |
|
699 | # Update issues of the moved projects and issues assigned to a version of a moved project | |
699 | Issue.update_versions(["#{Version.table_name}.project_id IN (?) OR #{Issue.table_name}.project_id IN (?)", moved_project_ids, moved_project_ids]) |
|
700 | Issue.update_versions(["#{Version.table_name}.project_id IN (?) OR #{Issue.table_name}.project_id IN (?)", moved_project_ids, moved_project_ids]) | |
700 | end |
|
701 | end | |
701 |
|
702 | |||
702 | def parent_issue_id=(arg) |
|
703 | def parent_issue_id=(arg) | |
703 | parent_issue_id = arg.blank? ? nil : arg.to_i |
|
704 | parent_issue_id = arg.blank? ? nil : arg.to_i | |
704 | if parent_issue_id && @parent_issue = Issue.find_by_id(parent_issue_id) |
|
705 | if parent_issue_id && @parent_issue = Issue.find_by_id(parent_issue_id) | |
705 | @parent_issue.id |
|
706 | @parent_issue.id | |
706 | else |
|
707 | else | |
707 | @parent_issue = nil |
|
708 | @parent_issue = nil | |
708 | nil |
|
709 | nil | |
709 | end |
|
710 | end | |
710 | end |
|
711 | end | |
711 |
|
712 | |||
712 | def parent_issue_id |
|
713 | def parent_issue_id | |
713 | if instance_variable_defined? :@parent_issue |
|
714 | if instance_variable_defined? :@parent_issue | |
714 | @parent_issue.nil? ? nil : @parent_issue.id |
|
715 | @parent_issue.nil? ? nil : @parent_issue.id | |
715 | else |
|
716 | else | |
716 | parent_id |
|
717 | parent_id | |
717 | end |
|
718 | end | |
718 | end |
|
719 | end | |
719 |
|
720 | |||
720 | # Extracted from the ReportsController. |
|
721 | # Extracted from the ReportsController. | |
721 | def self.by_tracker(project) |
|
722 | def self.by_tracker(project) | |
722 | count_and_group_by(:project => project, |
|
723 | count_and_group_by(:project => project, | |
723 | :field => 'tracker_id', |
|
724 | :field => 'tracker_id', | |
724 | :joins => Tracker.table_name) |
|
725 | :joins => Tracker.table_name) | |
725 | end |
|
726 | end | |
726 |
|
727 | |||
727 | def self.by_version(project) |
|
728 | def self.by_version(project) | |
728 | count_and_group_by(:project => project, |
|
729 | count_and_group_by(:project => project, | |
729 | :field => 'fixed_version_id', |
|
730 | :field => 'fixed_version_id', | |
730 | :joins => Version.table_name) |
|
731 | :joins => Version.table_name) | |
731 | end |
|
732 | end | |
732 |
|
733 | |||
733 | def self.by_priority(project) |
|
734 | def self.by_priority(project) | |
734 | count_and_group_by(:project => project, |
|
735 | count_and_group_by(:project => project, | |
735 | :field => 'priority_id', |
|
736 | :field => 'priority_id', | |
736 | :joins => IssuePriority.table_name) |
|
737 | :joins => IssuePriority.table_name) | |
737 | end |
|
738 | end | |
738 |
|
739 | |||
739 | def self.by_category(project) |
|
740 | def self.by_category(project) | |
740 | count_and_group_by(:project => project, |
|
741 | count_and_group_by(:project => project, | |
741 | :field => 'category_id', |
|
742 | :field => 'category_id', | |
742 | :joins => IssueCategory.table_name) |
|
743 | :joins => IssueCategory.table_name) | |
743 | end |
|
744 | end | |
744 |
|
745 | |||
745 | def self.by_assigned_to(project) |
|
746 | def self.by_assigned_to(project) | |
746 | count_and_group_by(:project => project, |
|
747 | count_and_group_by(:project => project, | |
747 | :field => 'assigned_to_id', |
|
748 | :field => 'assigned_to_id', | |
748 | :joins => User.table_name) |
|
749 | :joins => User.table_name) | |
749 | end |
|
750 | end | |
750 |
|
751 | |||
751 | def self.by_author(project) |
|
752 | def self.by_author(project) | |
752 | count_and_group_by(:project => project, |
|
753 | count_and_group_by(:project => project, | |
753 | :field => 'author_id', |
|
754 | :field => 'author_id', | |
754 | :joins => User.table_name) |
|
755 | :joins => User.table_name) | |
755 | end |
|
756 | end | |
756 |
|
757 | |||
757 | def self.by_subproject(project) |
|
758 | def self.by_subproject(project) | |
758 | ActiveRecord::Base.connection.select_all("select s.id as status_id, |
|
759 | ActiveRecord::Base.connection.select_all("select s.id as status_id, | |
759 | s.is_closed as closed, |
|
760 | s.is_closed as closed, | |
760 | #{Issue.table_name}.project_id as project_id, |
|
761 | #{Issue.table_name}.project_id as project_id, | |
761 | count(#{Issue.table_name}.id) as total |
|
762 | count(#{Issue.table_name}.id) as total | |
762 | from |
|
763 | from | |
763 | #{Issue.table_name}, #{Project.table_name}, #{IssueStatus.table_name} s |
|
764 | #{Issue.table_name}, #{Project.table_name}, #{IssueStatus.table_name} s | |
764 | where |
|
765 | where | |
765 | #{Issue.table_name}.status_id=s.id |
|
766 | #{Issue.table_name}.status_id=s.id | |
766 | and #{Issue.table_name}.project_id = #{Project.table_name}.id |
|
767 | and #{Issue.table_name}.project_id = #{Project.table_name}.id | |
767 | and #{visible_condition(User.current, :project => project, :with_subprojects => true)} |
|
768 | and #{visible_condition(User.current, :project => project, :with_subprojects => true)} | |
768 | and #{Issue.table_name}.project_id <> #{project.id} |
|
769 | and #{Issue.table_name}.project_id <> #{project.id} | |
769 | group by s.id, s.is_closed, #{Issue.table_name}.project_id") if project.descendants.active.any? |
|
770 | group by s.id, s.is_closed, #{Issue.table_name}.project_id") if project.descendants.active.any? | |
770 | end |
|
771 | end | |
771 | # End ReportsController extraction |
|
772 | # End ReportsController extraction | |
772 |
|
773 | |||
773 | # Returns an array of projects that user can assign the issue to |
|
774 | # Returns an array of projects that user can assign the issue to | |
774 | def allowed_target_projects(user=User.current) |
|
775 | def allowed_target_projects(user=User.current) | |
775 | if new_record? |
|
776 | if new_record? | |
776 | Project.all(:conditions => Project.allowed_to_condition(user, :add_issues)) |
|
777 | Project.all(:conditions => Project.allowed_to_condition(user, :add_issues)) | |
777 | else |
|
778 | else | |
778 | self.class.allowed_target_projects_on_move(user) |
|
779 | self.class.allowed_target_projects_on_move(user) | |
779 | end |
|
780 | end | |
780 | end |
|
781 | end | |
781 |
|
782 | |||
782 | # Returns an array of projects that user can move issues to |
|
783 | # Returns an array of projects that user can move issues to | |
783 | def self.allowed_target_projects_on_move(user=User.current) |
|
784 | def self.allowed_target_projects_on_move(user=User.current) | |
784 | projects = [] |
|
785 | projects = [] | |
785 | if user.admin? |
|
786 | if user.admin? | |
786 | # admin is allowed to move issues to any active (visible) project |
|
787 | # admin is allowed to move issues to any active (visible) project | |
787 | projects = Project.visible(user).all |
|
788 | projects = Project.visible(user).all | |
788 | elsif user.logged? |
|
789 | elsif user.logged? | |
789 | if Role.non_member.allowed_to?(:move_issues) |
|
790 | if Role.non_member.allowed_to?(:move_issues) | |
790 | projects = Project.visible(user).all |
|
791 | projects = Project.visible(user).all | |
791 | else |
|
792 | else | |
792 | user.memberships.each {|m| projects << m.project if m.roles.detect {|r| r.allowed_to?(:move_issues)}} |
|
793 | user.memberships.each {|m| projects << m.project if m.roles.detect {|r| r.allowed_to?(:move_issues)}} | |
793 | end |
|
794 | end | |
794 | end |
|
795 | end | |
795 | projects |
|
796 | projects | |
796 | end |
|
797 | end | |
797 |
|
798 | |||
798 | private |
|
799 | private | |
799 |
|
800 | |||
800 | def after_project_change |
|
801 | def after_project_change | |
801 | # Update project_id on related time entries |
|
802 | # Update project_id on related time entries | |
802 | TimeEntry.update_all(["project_id = ?", project_id], {:issue_id => id}) |
|
803 | TimeEntry.update_all(["project_id = ?", project_id], {:issue_id => id}) | |
803 |
|
804 | |||
804 | # Delete issue relations |
|
805 | # Delete issue relations | |
805 | unless Setting.cross_project_issue_relations? |
|
806 | unless Setting.cross_project_issue_relations? | |
806 | relations_from.clear |
|
807 | relations_from.clear | |
807 | relations_to.clear |
|
808 | relations_to.clear | |
808 | end |
|
809 | end | |
809 |
|
810 | |||
810 | # Move subtasks |
|
811 | # Move subtasks | |
811 | children.each do |child| |
|
812 | children.each do |child| | |
812 | # Change project and keep project |
|
813 | # Change project and keep project | |
813 | child.send :project=, project, true |
|
814 | child.send :project=, project, true | |
814 | unless child.save |
|
815 | unless child.save | |
815 | raise ActiveRecord::Rollback |
|
816 | raise ActiveRecord::Rollback | |
816 | end |
|
817 | end | |
817 | end |
|
818 | end | |
818 | end |
|
819 | end | |
819 |
|
820 | |||
820 | def update_nested_set_attributes |
|
821 | def update_nested_set_attributes | |
821 | if root_id.nil? |
|
822 | if root_id.nil? | |
822 | # issue was just created |
|
823 | # issue was just created | |
823 | self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id) |
|
824 | self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id) | |
824 | set_default_left_and_right |
|
825 | set_default_left_and_right | |
825 | Issue.update_all("root_id = #{root_id}, lft = #{lft}, rgt = #{rgt}", ["id = ?", id]) |
|
826 | Issue.update_all("root_id = #{root_id}, lft = #{lft}, rgt = #{rgt}", ["id = ?", id]) | |
826 | if @parent_issue |
|
827 | if @parent_issue | |
827 | move_to_child_of(@parent_issue) |
|
828 | move_to_child_of(@parent_issue) | |
828 | end |
|
829 | end | |
829 | reload |
|
830 | reload | |
830 | elsif parent_issue_id != parent_id |
|
831 | elsif parent_issue_id != parent_id | |
831 | former_parent_id = parent_id |
|
832 | former_parent_id = parent_id | |
832 | # moving an existing issue |
|
833 | # moving an existing issue | |
833 | if @parent_issue && @parent_issue.root_id == root_id |
|
834 | if @parent_issue && @parent_issue.root_id == root_id | |
834 | # inside the same tree |
|
835 | # inside the same tree | |
835 | move_to_child_of(@parent_issue) |
|
836 | move_to_child_of(@parent_issue) | |
836 | else |
|
837 | else | |
837 | # to another tree |
|
838 | # to another tree | |
838 | unless root? |
|
839 | unless root? | |
839 | move_to_right_of(root) |
|
840 | move_to_right_of(root) | |
840 | reload |
|
841 | reload | |
841 | end |
|
842 | end | |
842 | old_root_id = root_id |
|
843 | old_root_id = root_id | |
843 | self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id ) |
|
844 | self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id ) | |
844 | target_maxright = nested_set_scope.maximum(right_column_name) || 0 |
|
845 | target_maxright = nested_set_scope.maximum(right_column_name) || 0 | |
845 | offset = target_maxright + 1 - lft |
|
846 | offset = target_maxright + 1 - lft | |
846 | Issue.update_all("root_id = #{root_id}, lft = lft + #{offset}, rgt = rgt + #{offset}", |
|
847 | Issue.update_all("root_id = #{root_id}, lft = lft + #{offset}, rgt = rgt + #{offset}", | |
847 | ["root_id = ? AND lft >= ? AND rgt <= ? ", old_root_id, lft, rgt]) |
|
848 | ["root_id = ? AND lft >= ? AND rgt <= ? ", old_root_id, lft, rgt]) | |
848 | self[left_column_name] = lft + offset |
|
849 | self[left_column_name] = lft + offset | |
849 | self[right_column_name] = rgt + offset |
|
850 | self[right_column_name] = rgt + offset | |
850 | if @parent_issue |
|
851 | if @parent_issue | |
851 | move_to_child_of(@parent_issue) |
|
852 | move_to_child_of(@parent_issue) | |
852 | end |
|
853 | end | |
853 | end |
|
854 | end | |
854 | reload |
|
855 | reload | |
855 | # delete invalid relations of all descendants |
|
856 | # delete invalid relations of all descendants | |
856 | self_and_descendants.each do |issue| |
|
857 | self_and_descendants.each do |issue| | |
857 | issue.relations.each do |relation| |
|
858 | issue.relations.each do |relation| | |
858 | relation.destroy unless relation.valid? |
|
859 | relation.destroy unless relation.valid? | |
859 | end |
|
860 | end | |
860 | end |
|
861 | end | |
861 | # update former parent |
|
862 | # update former parent | |
862 | recalculate_attributes_for(former_parent_id) if former_parent_id |
|
863 | recalculate_attributes_for(former_parent_id) if former_parent_id | |
863 | end |
|
864 | end | |
864 | remove_instance_variable(:@parent_issue) if instance_variable_defined?(:@parent_issue) |
|
865 | remove_instance_variable(:@parent_issue) if instance_variable_defined?(:@parent_issue) | |
865 | end |
|
866 | end | |
866 |
|
867 | |||
867 | def update_parent_attributes |
|
868 | def update_parent_attributes | |
868 | recalculate_attributes_for(parent_id) if parent_id |
|
869 | recalculate_attributes_for(parent_id) if parent_id | |
869 | end |
|
870 | end | |
870 |
|
871 | |||
871 | def recalculate_attributes_for(issue_id) |
|
872 | def recalculate_attributes_for(issue_id) | |
872 | if issue_id && p = Issue.find_by_id(issue_id) |
|
873 | if issue_id && p = Issue.find_by_id(issue_id) | |
873 | # priority = highest priority of children |
|
874 | # priority = highest priority of children | |
874 | if priority_position = p.children.maximum("#{IssuePriority.table_name}.position", :joins => :priority) |
|
875 | if priority_position = p.children.maximum("#{IssuePriority.table_name}.position", :joins => :priority) | |
875 | p.priority = IssuePriority.find_by_position(priority_position) |
|
876 | p.priority = IssuePriority.find_by_position(priority_position) | |
876 | end |
|
877 | end | |
877 |
|
878 | |||
878 | # start/due dates = lowest/highest dates of children |
|
879 | # start/due dates = lowest/highest dates of children | |
879 | p.start_date = p.children.minimum(:start_date) |
|
880 | p.start_date = p.children.minimum(:start_date) | |
880 | p.due_date = p.children.maximum(:due_date) |
|
881 | p.due_date = p.children.maximum(:due_date) | |
881 | if p.start_date && p.due_date && p.due_date < p.start_date |
|
882 | if p.start_date && p.due_date && p.due_date < p.start_date | |
882 | p.start_date, p.due_date = p.due_date, p.start_date |
|
883 | p.start_date, p.due_date = p.due_date, p.start_date | |
883 | end |
|
884 | end | |
884 |
|
885 | |||
885 | # done ratio = weighted average ratio of leaves |
|
886 | # done ratio = weighted average ratio of leaves | |
886 | unless Issue.use_status_for_done_ratio? && p.status && p.status.default_done_ratio |
|
887 | unless Issue.use_status_for_done_ratio? && p.status && p.status.default_done_ratio | |
887 | leaves_count = p.leaves.count |
|
888 | leaves_count = p.leaves.count | |
888 | if leaves_count > 0 |
|
889 | if leaves_count > 0 | |
889 | average = p.leaves.average(:estimated_hours).to_f |
|
890 | average = p.leaves.average(:estimated_hours).to_f | |
890 | if average == 0 |
|
891 | if average == 0 | |
891 | average = 1 |
|
892 | average = 1 | |
892 | end |
|
893 | end | |
893 | done = p.leaves.sum("COALESCE(estimated_hours, #{average}) * (CASE WHEN is_closed = #{connection.quoted_true} THEN 100 ELSE COALESCE(done_ratio, 0) END)", :joins => :status).to_f |
|
894 | done = p.leaves.sum("COALESCE(estimated_hours, #{average}) * (CASE WHEN is_closed = #{connection.quoted_true} THEN 100 ELSE COALESCE(done_ratio, 0) END)", :joins => :status).to_f | |
894 | progress = done / (average * leaves_count) |
|
895 | progress = done / (average * leaves_count) | |
895 | p.done_ratio = progress.round |
|
896 | p.done_ratio = progress.round | |
896 | end |
|
897 | end | |
897 | end |
|
898 | end | |
898 |
|
899 | |||
899 | # estimate = sum of leaves estimates |
|
900 | # estimate = sum of leaves estimates | |
900 | p.estimated_hours = p.leaves.sum(:estimated_hours).to_f |
|
901 | p.estimated_hours = p.leaves.sum(:estimated_hours).to_f | |
901 | p.estimated_hours = nil if p.estimated_hours == 0.0 |
|
902 | p.estimated_hours = nil if p.estimated_hours == 0.0 | |
902 |
|
903 | |||
903 | # ancestors will be recursively updated |
|
904 | # ancestors will be recursively updated | |
904 | p.save(false) |
|
905 | p.save(false) | |
905 | end |
|
906 | end | |
906 | end |
|
907 | end | |
907 |
|
908 | |||
908 | # Update issues so their versions are not pointing to a |
|
909 | # Update issues so their versions are not pointing to a | |
909 | # fixed_version that is not shared with the issue's project |
|
910 | # fixed_version that is not shared with the issue's project | |
910 | def self.update_versions(conditions=nil) |
|
911 | def self.update_versions(conditions=nil) | |
911 | # Only need to update issues with a fixed_version from |
|
912 | # Only need to update issues with a fixed_version from | |
912 | # a different project and that is not systemwide shared |
|
913 | # a different project and that is not systemwide shared | |
913 | Issue.scoped(:conditions => conditions).all( |
|
914 | Issue.scoped(:conditions => conditions).all( | |
914 | :conditions => "#{Issue.table_name}.fixed_version_id IS NOT NULL" + |
|
915 | :conditions => "#{Issue.table_name}.fixed_version_id IS NOT NULL" + | |
915 | " AND #{Issue.table_name}.project_id <> #{Version.table_name}.project_id" + |
|
916 | " AND #{Issue.table_name}.project_id <> #{Version.table_name}.project_id" + | |
916 | " AND #{Version.table_name}.sharing <> 'system'", |
|
917 | " AND #{Version.table_name}.sharing <> 'system'", | |
917 | :include => [:project, :fixed_version] |
|
918 | :include => [:project, :fixed_version] | |
918 | ).each do |issue| |
|
919 | ).each do |issue| | |
919 | next if issue.project.nil? || issue.fixed_version.nil? |
|
920 | next if issue.project.nil? || issue.fixed_version.nil? | |
920 | unless issue.project.shared_versions.include?(issue.fixed_version) |
|
921 | unless issue.project.shared_versions.include?(issue.fixed_version) | |
921 | issue.init_journal(User.current) |
|
922 | issue.init_journal(User.current) | |
922 | issue.fixed_version = nil |
|
923 | issue.fixed_version = nil | |
923 | issue.save |
|
924 | issue.save | |
924 | end |
|
925 | end | |
925 | end |
|
926 | end | |
926 | end |
|
927 | end | |
927 |
|
928 | |||
928 | # Callback on attachment deletion |
|
929 | # Callback on attachment deletion | |
929 | def attachment_added(obj) |
|
930 | def attachment_added(obj) | |
930 | if @current_journal && !obj.new_record? |
|
931 | if @current_journal && !obj.new_record? | |
931 | @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => obj.id, :value => obj.filename) |
|
932 | @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => obj.id, :value => obj.filename) | |
932 | end |
|
933 | end | |
933 | end |
|
934 | end | |
934 |
|
935 | |||
935 | # Callback on attachment deletion |
|
936 | # Callback on attachment deletion | |
936 | def attachment_removed(obj) |
|
937 | def attachment_removed(obj) | |
937 | journal = init_journal(User.current) |
|
938 | journal = init_journal(User.current) | |
938 | journal.details << JournalDetail.new(:property => 'attachment', |
|
939 | journal.details << JournalDetail.new(:property => 'attachment', | |
939 | :prop_key => obj.id, |
|
940 | :prop_key => obj.id, | |
940 | :old_value => obj.filename) |
|
941 | :old_value => obj.filename) | |
941 | journal.save |
|
942 | journal.save | |
942 | end |
|
943 | end | |
943 |
|
944 | |||
944 | # Default assignment based on category |
|
945 | # Default assignment based on category | |
945 | def default_assign |
|
946 | def default_assign | |
946 | if assigned_to.nil? && category && category.assigned_to |
|
947 | if assigned_to.nil? && category && category.assigned_to | |
947 | self.assigned_to = category.assigned_to |
|
948 | self.assigned_to = category.assigned_to | |
948 | end |
|
949 | end | |
949 | end |
|
950 | end | |
950 |
|
951 | |||
951 | # Updates start/due dates of following issues |
|
952 | # Updates start/due dates of following issues | |
952 | def reschedule_following_issues |
|
953 | def reschedule_following_issues | |
953 | if start_date_changed? || due_date_changed? |
|
954 | if start_date_changed? || due_date_changed? | |
954 | relations_from.each do |relation| |
|
955 | relations_from.each do |relation| | |
955 | relation.set_issue_to_dates |
|
956 | relation.set_issue_to_dates | |
956 | end |
|
957 | end | |
957 | end |
|
958 | end | |
958 | end |
|
959 | end | |
959 |
|
960 | |||
960 | # Closes duplicates if the issue is being closed |
|
961 | # Closes duplicates if the issue is being closed | |
961 | def close_duplicates |
|
962 | def close_duplicates | |
962 | if closing? |
|
963 | if closing? | |
963 | duplicates.each do |duplicate| |
|
964 | duplicates.each do |duplicate| | |
964 | # Reload is need in case the duplicate was updated by a previous duplicate |
|
965 | # Reload is need in case the duplicate was updated by a previous duplicate | |
965 | duplicate.reload |
|
966 | duplicate.reload | |
966 | # Don't re-close it if it's already closed |
|
967 | # Don't re-close it if it's already closed | |
967 | next if duplicate.closed? |
|
968 | next if duplicate.closed? | |
968 | # Same user and notes |
|
969 | # Same user and notes | |
969 | if @current_journal |
|
970 | if @current_journal | |
970 | duplicate.init_journal(@current_journal.user, @current_journal.notes) |
|
971 | duplicate.init_journal(@current_journal.user, @current_journal.notes) | |
971 | end |
|
972 | end | |
972 | duplicate.update_attribute :status, self.status |
|
973 | duplicate.update_attribute :status, self.status | |
973 | end |
|
974 | end | |
974 | end |
|
975 | end | |
975 | end |
|
976 | end | |
976 |
|
977 | |||
977 | # Saves the changes in a Journal |
|
978 | # Saves the changes in a Journal | |
978 | # Called after_save |
|
979 | # Called after_save | |
979 | def create_journal |
|
980 | def create_journal | |
980 | if @current_journal |
|
981 | if @current_journal | |
981 | # attributes changes |
|
982 | # attributes changes | |
982 | if @attributes_before_change |
|
983 | if @attributes_before_change | |
983 | (Issue.column_names - %w(id root_id lft rgt lock_version created_on updated_on)).each {|c| |
|
984 | (Issue.column_names - %w(id root_id lft rgt lock_version created_on updated_on)).each {|c| | |
984 | before = @attributes_before_change[c] |
|
985 | before = @attributes_before_change[c] | |
985 | after = send(c) |
|
986 | after = send(c) | |
986 | next if before == after || (before.blank? && after.blank?) |
|
987 | next if before == after || (before.blank? && after.blank?) | |
987 | @current_journal.details << JournalDetail.new(:property => 'attr', |
|
988 | @current_journal.details << JournalDetail.new(:property => 'attr', | |
988 | :prop_key => c, |
|
989 | :prop_key => c, | |
989 | :old_value => before, |
|
990 | :old_value => before, | |
990 | :value => after) |
|
991 | :value => after) | |
991 | } |
|
992 | } | |
992 | end |
|
993 | end | |
993 | if @custom_values_before_change |
|
994 | if @custom_values_before_change | |
994 | # custom fields changes |
|
995 | # custom fields changes | |
995 | custom_values.each {|c| |
|
996 | custom_values.each {|c| | |
996 | before = @custom_values_before_change[c.custom_field_id] |
|
997 | before = @custom_values_before_change[c.custom_field_id] | |
997 | after = c.value |
|
998 | after = c.value | |
998 | next if before == after || (before.blank? && after.blank?) |
|
999 | next if before == after || (before.blank? && after.blank?) | |
999 | @current_journal.details << JournalDetail.new(:property => 'cf', |
|
1000 | @current_journal.details << JournalDetail.new(:property => 'cf', | |
1000 | :prop_key => c.custom_field_id, |
|
1001 | :prop_key => c.custom_field_id, | |
1001 | :old_value => before, |
|
1002 | :old_value => before, | |
1002 | :value => after) |
|
1003 | :value => after) | |
1003 | } |
|
1004 | } | |
1004 | end |
|
1005 | end | |
1005 | @current_journal.save |
|
1006 | @current_journal.save | |
1006 | # reset current journal |
|
1007 | # reset current journal | |
1007 | init_journal @current_journal.user, @current_journal.notes |
|
1008 | init_journal @current_journal.user, @current_journal.notes | |
1008 | end |
|
1009 | end | |
1009 | end |
|
1010 | end | |
1010 |
|
1011 | |||
1011 | # Query generator for selecting groups of issue counts for a project |
|
1012 | # Query generator for selecting groups of issue counts for a project | |
1012 | # based on specific criteria |
|
1013 | # based on specific criteria | |
1013 | # |
|
1014 | # | |
1014 | # Options |
|
1015 | # Options | |
1015 | # * project - Project to search in. |
|
1016 | # * project - Project to search in. | |
1016 | # * field - String. Issue field to key off of in the grouping. |
|
1017 | # * field - String. Issue field to key off of in the grouping. | |
1017 | # * joins - String. The table name to join against. |
|
1018 | # * joins - String. The table name to join against. | |
1018 | def self.count_and_group_by(options) |
|
1019 | def self.count_and_group_by(options) | |
1019 | project = options.delete(:project) |
|
1020 | project = options.delete(:project) | |
1020 | select_field = options.delete(:field) |
|
1021 | select_field = options.delete(:field) | |
1021 | joins = options.delete(:joins) |
|
1022 | joins = options.delete(:joins) | |
1022 |
|
1023 | |||
1023 | where = "#{Issue.table_name}.#{select_field}=j.id" |
|
1024 | where = "#{Issue.table_name}.#{select_field}=j.id" | |
1024 |
|
1025 | |||
1025 | ActiveRecord::Base.connection.select_all("select s.id as status_id, |
|
1026 | ActiveRecord::Base.connection.select_all("select s.id as status_id, | |
1026 | s.is_closed as closed, |
|
1027 | s.is_closed as closed, | |
1027 | j.id as #{select_field}, |
|
1028 | j.id as #{select_field}, | |
1028 | count(#{Issue.table_name}.id) as total |
|
1029 | count(#{Issue.table_name}.id) as total | |
1029 | from |
|
1030 | from | |
1030 | #{Issue.table_name}, #{Project.table_name}, #{IssueStatus.table_name} s, #{joins} j |
|
1031 | #{Issue.table_name}, #{Project.table_name}, #{IssueStatus.table_name} s, #{joins} j | |
1031 | where |
|
1032 | where | |
1032 | #{Issue.table_name}.status_id=s.id |
|
1033 | #{Issue.table_name}.status_id=s.id | |
1033 | and #{where} |
|
1034 | and #{where} | |
1034 | and #{Issue.table_name}.project_id=#{Project.table_name}.id |
|
1035 | and #{Issue.table_name}.project_id=#{Project.table_name}.id | |
1035 | and #{visible_condition(User.current, :project => project)} |
|
1036 | and #{visible_condition(User.current, :project => project)} | |
1036 | group by s.id, s.is_closed, j.id") |
|
1037 | group by s.id, s.is_closed, j.id") | |
1037 | end |
|
1038 | end | |
1038 | end |
|
1039 | end |
General Comments 0
You need to be logged in to leave comments.
Login now