@@ -1,1603 +1,1606 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2014 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class Issue < ActiveRecord::Base |
|
19 | 19 | include Redmine::SafeAttributes |
|
20 | 20 | include Redmine::Utils::DateCalculation |
|
21 | 21 | include Redmine::I18n |
|
22 | 22 | |
|
23 | 23 | belongs_to :project |
|
24 | 24 | belongs_to :tracker |
|
25 | 25 | belongs_to :status, :class_name => 'IssueStatus' |
|
26 | 26 | belongs_to :author, :class_name => 'User' |
|
27 | 27 | belongs_to :assigned_to, :class_name => 'Principal' |
|
28 | 28 | belongs_to :fixed_version, :class_name => 'Version' |
|
29 | 29 | belongs_to :priority, :class_name => 'IssuePriority' |
|
30 | 30 | belongs_to :category, :class_name => 'IssueCategory' |
|
31 | 31 | |
|
32 | 32 | has_many :journals, :as => :journalized, :dependent => :destroy |
|
33 | 33 | has_many :visible_journals, |
|
34 | 34 | lambda {where(["(#{Journal.table_name}.private_notes = ? OR (#{Project.allowed_to_condition(User.current, :view_private_notes)}))", false])}, |
|
35 | 35 | :class_name => 'Journal', |
|
36 | 36 | :as => :journalized |
|
37 | 37 | |
|
38 | 38 | has_many :time_entries, :dependent => :destroy |
|
39 | 39 | has_and_belongs_to_many :changesets, lambda {order("#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC")} |
|
40 | 40 | |
|
41 | 41 | has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all |
|
42 | 42 | has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all |
|
43 | 43 | |
|
44 | 44 | acts_as_nested_set :scope => 'root_id', :dependent => :destroy |
|
45 | 45 | acts_as_attachable :after_add => :attachment_added, :after_remove => :attachment_removed |
|
46 | 46 | acts_as_customizable |
|
47 | 47 | acts_as_watchable |
|
48 | 48 | acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"], |
|
49 | 49 | # sort by id so that limited eager loading doesn't break with postgresql |
|
50 | 50 | :order_column => "#{table_name}.id", |
|
51 | 51 | :scope => lambda { joins(:project). |
|
52 | 52 | joins("LEFT OUTER JOIN #{Journal.table_name} ON #{Journal.table_name}.journalized_type='Issue'" + |
|
53 | 53 | " AND #{Journal.table_name}.journalized_id = #{Issue.table_name}.id" + |
|
54 | 54 | " AND (#{Journal.table_name}.private_notes = #{connection.quoted_false}" + |
|
55 | 55 | " OR (#{Project.allowed_to_condition(User.current, :view_private_notes)}))") } |
|
56 | 56 | |
|
57 | 57 | acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id} (#{o.status}): #{o.subject}"}, |
|
58 | 58 | :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}}, |
|
59 | 59 | :type => Proc.new {|o| 'issue' + (o.closed? ? ' closed' : '') } |
|
60 | 60 | |
|
61 | 61 | acts_as_activity_provider :scope => preload(:project, :author, :tracker), |
|
62 | 62 | :author_key => :author_id |
|
63 | 63 | |
|
64 | 64 | DONE_RATIO_OPTIONS = %w(issue_field issue_status) |
|
65 | 65 | |
|
66 | 66 | attr_reader :current_journal |
|
67 | 67 | delegate :notes, :notes=, :private_notes, :private_notes=, :to => :current_journal, :allow_nil => true |
|
68 | 68 | |
|
69 |
validates_presence_of :subject, |
|
|
69 | validates_presence_of :subject, :project, :tracker | |
|
70 | validates_presence_of :priority, :if => Proc.new {|issue| issue.new_record? || issue.priority_id_changed?} | |
|
71 | validates_presence_of :status, :if => Proc.new {|issue| issue.new_record? || issue.status_id_changed?} | |
|
72 | validates_presence_of :author, :if => Proc.new {|issue| issue.new_record? || issue.author_id_changed?} | |
|
70 | 73 | |
|
71 | 74 | validates_length_of :subject, :maximum => 255 |
|
72 | 75 | validates_inclusion_of :done_ratio, :in => 0..100 |
|
73 | 76 | validates :estimated_hours, :numericality => {:greater_than_or_equal_to => 0, :allow_nil => true, :message => :invalid} |
|
74 | 77 | validates :start_date, :date => true |
|
75 | 78 | validates :due_date, :date => true |
|
76 | 79 | validate :validate_issue, :validate_required_fields |
|
77 | 80 | attr_protected :id |
|
78 | 81 | |
|
79 | 82 | scope :visible, lambda {|*args| |
|
80 | 83 | includes(:project). |
|
81 | 84 | references(:project). |
|
82 | 85 | where(Issue.visible_condition(args.shift || User.current, *args)) |
|
83 | 86 | } |
|
84 | 87 | |
|
85 | 88 | scope :open, lambda {|*args| |
|
86 | 89 | is_closed = args.size > 0 ? !args.first : false |
|
87 | 90 | joins(:status). |
|
88 | 91 | where("#{IssueStatus.table_name}.is_closed = ?", is_closed) |
|
89 | 92 | } |
|
90 | 93 | |
|
91 | 94 | scope :recently_updated, lambda { order("#{Issue.table_name}.updated_on DESC") } |
|
92 | 95 | scope :on_active_project, lambda { |
|
93 | 96 | joins(:project). |
|
94 | 97 | where("#{Project.table_name}.status = ?", Project::STATUS_ACTIVE) |
|
95 | 98 | } |
|
96 | 99 | scope :fixed_version, lambda {|versions| |
|
97 | 100 | ids = [versions].flatten.compact.map {|v| v.is_a?(Version) ? v.id : v} |
|
98 | 101 | ids.any? ? where(:fixed_version_id => ids) : where('1=0') |
|
99 | 102 | } |
|
100 | 103 | |
|
101 | 104 | before_create :default_assign |
|
102 | 105 | before_save :close_duplicates, :update_done_ratio_from_issue_status, |
|
103 | 106 | :force_updated_on_change, :update_closed_on, :set_assigned_to_was |
|
104 | 107 | after_save {|issue| issue.send :after_project_change if !issue.id_changed? && issue.project_id_changed?} |
|
105 | 108 | after_save :reschedule_following_issues, :update_nested_set_attributes, |
|
106 | 109 | :update_parent_attributes, :create_journal |
|
107 | 110 | # Should be after_create but would be called before previous after_save callbacks |
|
108 | 111 | after_save :after_create_from_copy |
|
109 | 112 | after_destroy :update_parent_attributes |
|
110 | 113 | after_create :send_notification |
|
111 | 114 | # Keep it at the end of after_save callbacks |
|
112 | 115 | after_save :clear_assigned_to_was |
|
113 | 116 | |
|
114 | 117 | # Returns a SQL conditions string used to find all issues visible by the specified user |
|
115 | 118 | def self.visible_condition(user, options={}) |
|
116 | 119 | Project.allowed_to_condition(user, :view_issues, options) do |role, user| |
|
117 | 120 | if user.id && user.logged? |
|
118 | 121 | case role.issues_visibility |
|
119 | 122 | when 'all' |
|
120 | 123 | nil |
|
121 | 124 | when 'default' |
|
122 | 125 | user_ids = [user.id] + user.groups.map(&:id).compact |
|
123 | 126 | "(#{table_name}.is_private = #{connection.quoted_false} OR #{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))" |
|
124 | 127 | when 'own' |
|
125 | 128 | user_ids = [user.id] + user.groups.map(&:id).compact |
|
126 | 129 | "(#{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))" |
|
127 | 130 | else |
|
128 | 131 | '1=0' |
|
129 | 132 | end |
|
130 | 133 | else |
|
131 | 134 | "(#{table_name}.is_private = #{connection.quoted_false})" |
|
132 | 135 | end |
|
133 | 136 | end |
|
134 | 137 | end |
|
135 | 138 | |
|
136 | 139 | # Returns true if usr or current user is allowed to view the issue |
|
137 | 140 | def visible?(usr=nil) |
|
138 | 141 | (usr || User.current).allowed_to?(:view_issues, self.project) do |role, user| |
|
139 | 142 | if user.logged? |
|
140 | 143 | case role.issues_visibility |
|
141 | 144 | when 'all' |
|
142 | 145 | true |
|
143 | 146 | when 'default' |
|
144 | 147 | !self.is_private? || (self.author == user || user.is_or_belongs_to?(assigned_to)) |
|
145 | 148 | when 'own' |
|
146 | 149 | self.author == user || user.is_or_belongs_to?(assigned_to) |
|
147 | 150 | else |
|
148 | 151 | false |
|
149 | 152 | end |
|
150 | 153 | else |
|
151 | 154 | !self.is_private? |
|
152 | 155 | end |
|
153 | 156 | end |
|
154 | 157 | end |
|
155 | 158 | |
|
156 | 159 | # Returns true if user or current user is allowed to edit or add a note to the issue |
|
157 | 160 | def editable?(user=User.current) |
|
158 | 161 | user.allowed_to?(:edit_issues, project) || user.allowed_to?(:add_issue_notes, project) |
|
159 | 162 | end |
|
160 | 163 | |
|
161 | 164 | def initialize(attributes=nil, *args) |
|
162 | 165 | super |
|
163 | 166 | if new_record? |
|
164 | 167 | # set default values for new records only |
|
165 | 168 | self.priority ||= IssuePriority.default |
|
166 | 169 | self.watcher_user_ids = [] |
|
167 | 170 | end |
|
168 | 171 | end |
|
169 | 172 | |
|
170 | 173 | def create_or_update |
|
171 | 174 | super |
|
172 | 175 | ensure |
|
173 | 176 | @status_was = nil |
|
174 | 177 | end |
|
175 | 178 | private :create_or_update |
|
176 | 179 | |
|
177 | 180 | # AR#Persistence#destroy would raise and RecordNotFound exception |
|
178 | 181 | # if the issue was already deleted or updated (non matching lock_version). |
|
179 | 182 | # This is a problem when bulk deleting issues or deleting a project |
|
180 | 183 | # (because an issue may already be deleted if its parent was deleted |
|
181 | 184 | # first). |
|
182 | 185 | # The issue is reloaded by the nested_set before being deleted so |
|
183 | 186 | # the lock_version condition should not be an issue but we handle it. |
|
184 | 187 | def destroy |
|
185 | 188 | super |
|
186 | 189 | rescue ActiveRecord::RecordNotFound |
|
187 | 190 | # Stale or already deleted |
|
188 | 191 | begin |
|
189 | 192 | reload |
|
190 | 193 | rescue ActiveRecord::RecordNotFound |
|
191 | 194 | # The issue was actually already deleted |
|
192 | 195 | @destroyed = true |
|
193 | 196 | return freeze |
|
194 | 197 | end |
|
195 | 198 | # The issue was stale, retry to destroy |
|
196 | 199 | super |
|
197 | 200 | end |
|
198 | 201 | |
|
199 | 202 | alias :base_reload :reload |
|
200 | 203 | def reload(*args) |
|
201 | 204 | @workflow_rule_by_attribute = nil |
|
202 | 205 | @assignable_versions = nil |
|
203 | 206 | @relations = nil |
|
204 | 207 | @spent_hours = nil |
|
205 | 208 | base_reload(*args) |
|
206 | 209 | end |
|
207 | 210 | |
|
208 | 211 | # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields |
|
209 | 212 | def available_custom_fields |
|
210 | 213 | (project && tracker) ? (project.all_issue_custom_fields & tracker.custom_fields) : [] |
|
211 | 214 | end |
|
212 | 215 | |
|
213 | 216 | def visible_custom_field_values(user=nil) |
|
214 | 217 | user_real = user || User.current |
|
215 | 218 | custom_field_values.select do |value| |
|
216 | 219 | value.custom_field.visible_by?(project, user_real) |
|
217 | 220 | end |
|
218 | 221 | end |
|
219 | 222 | |
|
220 | 223 | # Copies attributes from another issue, arg can be an id or an Issue |
|
221 | 224 | def copy_from(arg, options={}) |
|
222 | 225 | issue = arg.is_a?(Issue) ? arg : Issue.visible.find(arg) |
|
223 | 226 | self.attributes = issue.attributes.dup.except("id", "root_id", "parent_id", "lft", "rgt", "created_on", "updated_on") |
|
224 | 227 | self.custom_field_values = issue.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h} |
|
225 | 228 | self.status = issue.status |
|
226 | 229 | self.author = User.current |
|
227 | 230 | unless options[:attachments] == false |
|
228 | 231 | self.attachments = issue.attachments.map do |attachement| |
|
229 | 232 | attachement.copy(:container => self) |
|
230 | 233 | end |
|
231 | 234 | end |
|
232 | 235 | @copied_from = issue |
|
233 | 236 | @copy_options = options |
|
234 | 237 | self |
|
235 | 238 | end |
|
236 | 239 | |
|
237 | 240 | # Returns an unsaved copy of the issue |
|
238 | 241 | def copy(attributes=nil, copy_options={}) |
|
239 | 242 | copy = self.class.new.copy_from(self, copy_options) |
|
240 | 243 | copy.attributes = attributes if attributes |
|
241 | 244 | copy |
|
242 | 245 | end |
|
243 | 246 | |
|
244 | 247 | # Returns true if the issue is a copy |
|
245 | 248 | def copy? |
|
246 | 249 | @copied_from.present? |
|
247 | 250 | end |
|
248 | 251 | |
|
249 | 252 | def status_id=(status_id) |
|
250 | 253 | if status_id.to_s != self.status_id.to_s |
|
251 | 254 | self.status = (status_id.present? ? IssueStatus.find_by_id(status_id) : nil) |
|
252 | 255 | end |
|
253 | 256 | self.status_id |
|
254 | 257 | end |
|
255 | 258 | |
|
256 | 259 | # Sets the status. |
|
257 | 260 | def status=(status) |
|
258 | 261 | if status != self.status |
|
259 | 262 | @workflow_rule_by_attribute = nil |
|
260 | 263 | end |
|
261 | 264 | association(:status).writer(status) |
|
262 | 265 | end |
|
263 | 266 | |
|
264 | 267 | def priority_id=(pid) |
|
265 | 268 | self.priority = nil |
|
266 | 269 | write_attribute(:priority_id, pid) |
|
267 | 270 | end |
|
268 | 271 | |
|
269 | 272 | def category_id=(cid) |
|
270 | 273 | self.category = nil |
|
271 | 274 | write_attribute(:category_id, cid) |
|
272 | 275 | end |
|
273 | 276 | |
|
274 | 277 | def fixed_version_id=(vid) |
|
275 | 278 | self.fixed_version = nil |
|
276 | 279 | write_attribute(:fixed_version_id, vid) |
|
277 | 280 | end |
|
278 | 281 | |
|
279 | 282 | def tracker_id=(tracker_id) |
|
280 | 283 | if tracker_id.to_s != self.tracker_id.to_s |
|
281 | 284 | self.tracker = (tracker_id.present? ? Tracker.find_by_id(tracker_id) : nil) |
|
282 | 285 | end |
|
283 | 286 | self.tracker_id |
|
284 | 287 | end |
|
285 | 288 | |
|
286 | 289 | # Sets the tracker. |
|
287 | 290 | # This will set the status to the default status of the new tracker if: |
|
288 | 291 | # * the status was the default for the previous tracker |
|
289 | 292 | # * or if the status was not part of the new tracker statuses |
|
290 | 293 | # * or the status was nil |
|
291 | 294 | def tracker=(tracker) |
|
292 | 295 | if tracker != self.tracker |
|
293 | 296 | if status == default_status |
|
294 | 297 | self.status = nil |
|
295 | 298 | elsif status && tracker && !tracker.issue_status_ids.include?(status.id) |
|
296 | 299 | self.status = nil |
|
297 | 300 | end |
|
298 | 301 | @custom_field_values = nil |
|
299 | 302 | @workflow_rule_by_attribute = nil |
|
300 | 303 | end |
|
301 | 304 | association(:tracker).writer(tracker) |
|
302 | 305 | self.status ||= default_status |
|
303 | 306 | self.tracker |
|
304 | 307 | end |
|
305 | 308 | |
|
306 | 309 | def project_id=(project_id) |
|
307 | 310 | if project_id.to_s != self.project_id.to_s |
|
308 | 311 | self.project = (project_id.present? ? Project.find_by_id(project_id) : nil) |
|
309 | 312 | end |
|
310 | 313 | self.project_id |
|
311 | 314 | end |
|
312 | 315 | |
|
313 | 316 | # Sets the project. |
|
314 | 317 | # Unless keep_tracker argument is set to true, this will change the tracker |
|
315 | 318 | # to the first tracker of the new project if the previous tracker is not part |
|
316 | 319 | # of the new project trackers. |
|
317 | 320 | # This will clear the fixed_version is it's no longer valid for the new project. |
|
318 | 321 | # This will clear the parent issue if it's no longer valid for the new project. |
|
319 | 322 | # This will set the category to the category with the same name in the new |
|
320 | 323 | # project if it exists, or clear it if it doesn't. |
|
321 | 324 | def project=(project, keep_tracker=false) |
|
322 | 325 | project_was = self.project |
|
323 | 326 | association(:project).writer(project) |
|
324 | 327 | if project_was && project && project_was != project |
|
325 | 328 | @assignable_versions = nil |
|
326 | 329 | |
|
327 | 330 | unless keep_tracker || project.trackers.include?(tracker) |
|
328 | 331 | self.tracker = project.trackers.first |
|
329 | 332 | end |
|
330 | 333 | # Reassign to the category with same name if any |
|
331 | 334 | if category |
|
332 | 335 | self.category = project.issue_categories.find_by_name(category.name) |
|
333 | 336 | end |
|
334 | 337 | # Keep the fixed_version if it's still valid in the new_project |
|
335 | 338 | if fixed_version && fixed_version.project != project && !project.shared_versions.include?(fixed_version) |
|
336 | 339 | self.fixed_version = nil |
|
337 | 340 | end |
|
338 | 341 | # Clear the parent task if it's no longer valid |
|
339 | 342 | unless valid_parent_project? |
|
340 | 343 | self.parent_issue_id = nil |
|
341 | 344 | end |
|
342 | 345 | @custom_field_values = nil |
|
343 | 346 | @workflow_rule_by_attribute = nil |
|
344 | 347 | end |
|
345 | 348 | self.project |
|
346 | 349 | end |
|
347 | 350 | |
|
348 | 351 | def description=(arg) |
|
349 | 352 | if arg.is_a?(String) |
|
350 | 353 | arg = arg.gsub(/(\r\n|\n|\r)/, "\r\n") |
|
351 | 354 | end |
|
352 | 355 | write_attribute(:description, arg) |
|
353 | 356 | end |
|
354 | 357 | |
|
355 | 358 | # Overrides assign_attributes so that project and tracker get assigned first |
|
356 | 359 | def assign_attributes_with_project_and_tracker_first(new_attributes, *args) |
|
357 | 360 | return if new_attributes.nil? |
|
358 | 361 | attrs = new_attributes.dup |
|
359 | 362 | attrs.stringify_keys! |
|
360 | 363 | |
|
361 | 364 | %w(project project_id tracker tracker_id).each do |attr| |
|
362 | 365 | if attrs.has_key?(attr) |
|
363 | 366 | send "#{attr}=", attrs.delete(attr) |
|
364 | 367 | end |
|
365 | 368 | end |
|
366 | 369 | send :assign_attributes_without_project_and_tracker_first, attrs, *args |
|
367 | 370 | end |
|
368 | 371 | # Do not redefine alias chain on reload (see #4838) |
|
369 | 372 | alias_method_chain(:assign_attributes, :project_and_tracker_first) unless method_defined?(:assign_attributes_without_project_and_tracker_first) |
|
370 | 373 | |
|
371 | 374 | def attributes=(new_attributes) |
|
372 | 375 | assign_attributes new_attributes |
|
373 | 376 | end |
|
374 | 377 | |
|
375 | 378 | def estimated_hours=(h) |
|
376 | 379 | write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h) |
|
377 | 380 | end |
|
378 | 381 | |
|
379 | 382 | safe_attributes 'project_id', |
|
380 | 383 | :if => lambda {|issue, user| |
|
381 | 384 | if issue.new_record? |
|
382 | 385 | issue.copy? |
|
383 | 386 | elsif user.allowed_to?(:move_issues, issue.project) |
|
384 | 387 | Issue.allowed_target_projects_on_move.count > 1 |
|
385 | 388 | end |
|
386 | 389 | } |
|
387 | 390 | |
|
388 | 391 | safe_attributes 'tracker_id', |
|
389 | 392 | 'status_id', |
|
390 | 393 | 'category_id', |
|
391 | 394 | 'assigned_to_id', |
|
392 | 395 | 'priority_id', |
|
393 | 396 | 'fixed_version_id', |
|
394 | 397 | 'subject', |
|
395 | 398 | 'description', |
|
396 | 399 | 'start_date', |
|
397 | 400 | 'due_date', |
|
398 | 401 | 'done_ratio', |
|
399 | 402 | 'estimated_hours', |
|
400 | 403 | 'custom_field_values', |
|
401 | 404 | 'custom_fields', |
|
402 | 405 | 'lock_version', |
|
403 | 406 | 'notes', |
|
404 | 407 | :if => lambda {|issue, user| issue.new_record? || user.allowed_to?(:edit_issues, issue.project) } |
|
405 | 408 | |
|
406 | 409 | safe_attributes 'status_id', |
|
407 | 410 | 'assigned_to_id', |
|
408 | 411 | 'fixed_version_id', |
|
409 | 412 | 'done_ratio', |
|
410 | 413 | 'lock_version', |
|
411 | 414 | 'notes', |
|
412 | 415 | :if => lambda {|issue, user| issue.new_statuses_allowed_to(user).any? } |
|
413 | 416 | |
|
414 | 417 | safe_attributes 'notes', |
|
415 | 418 | :if => lambda {|issue, user| user.allowed_to?(:add_issue_notes, issue.project)} |
|
416 | 419 | |
|
417 | 420 | safe_attributes 'private_notes', |
|
418 | 421 | :if => lambda {|issue, user| !issue.new_record? && user.allowed_to?(:set_notes_private, issue.project)} |
|
419 | 422 | |
|
420 | 423 | safe_attributes 'watcher_user_ids', |
|
421 | 424 | :if => lambda {|issue, user| issue.new_record? && user.allowed_to?(:add_issue_watchers, issue.project)} |
|
422 | 425 | |
|
423 | 426 | safe_attributes 'is_private', |
|
424 | 427 | :if => lambda {|issue, user| |
|
425 | 428 | user.allowed_to?(:set_issues_private, issue.project) || |
|
426 | 429 | (issue.author_id == user.id && user.allowed_to?(:set_own_issues_private, issue.project)) |
|
427 | 430 | } |
|
428 | 431 | |
|
429 | 432 | safe_attributes 'parent_issue_id', |
|
430 | 433 | :if => lambda {|issue, user| (issue.new_record? || user.allowed_to?(:edit_issues, issue.project)) && |
|
431 | 434 | user.allowed_to?(:manage_subtasks, issue.project)} |
|
432 | 435 | |
|
433 | 436 | def safe_attribute_names(user=nil) |
|
434 | 437 | names = super |
|
435 | 438 | names -= disabled_core_fields |
|
436 | 439 | names -= read_only_attribute_names(user) |
|
437 | 440 | names |
|
438 | 441 | end |
|
439 | 442 | |
|
440 | 443 | # Safely sets attributes |
|
441 | 444 | # Should be called from controllers instead of #attributes= |
|
442 | 445 | # attr_accessible is too rough because we still want things like |
|
443 | 446 | # Issue.new(:project => foo) to work |
|
444 | 447 | def safe_attributes=(attrs, user=User.current) |
|
445 | 448 | return unless attrs.is_a?(Hash) |
|
446 | 449 | |
|
447 | 450 | attrs = attrs.deep_dup |
|
448 | 451 | |
|
449 | 452 | # Project and Tracker must be set before since new_statuses_allowed_to depends on it. |
|
450 | 453 | if (p = attrs.delete('project_id')) && safe_attribute?('project_id') |
|
451 | 454 | if allowed_target_projects(user).where(:id => p.to_i).exists? |
|
452 | 455 | self.project_id = p |
|
453 | 456 | end |
|
454 | 457 | end |
|
455 | 458 | |
|
456 | 459 | if (t = attrs.delete('tracker_id')) && safe_attribute?('tracker_id') |
|
457 | 460 | self.tracker_id = t |
|
458 | 461 | end |
|
459 | 462 | |
|
460 | 463 | if (s = attrs.delete('status_id')) && safe_attribute?('status_id') |
|
461 | 464 | if new_statuses_allowed_to(user).collect(&:id).include?(s.to_i) |
|
462 | 465 | self.status_id = s |
|
463 | 466 | end |
|
464 | 467 | end |
|
465 | 468 | |
|
466 | 469 | attrs = delete_unsafe_attributes(attrs, user) |
|
467 | 470 | return if attrs.empty? |
|
468 | 471 | |
|
469 | 472 | unless leaf? |
|
470 | 473 | attrs.reject! {|k,v| %w(priority_id done_ratio start_date due_date estimated_hours).include?(k)} |
|
471 | 474 | end |
|
472 | 475 | |
|
473 | 476 | if attrs['parent_issue_id'].present? |
|
474 | 477 | s = attrs['parent_issue_id'].to_s |
|
475 | 478 | unless (m = s.match(%r{\A#?(\d+)\z})) && (m[1] == parent_id.to_s || Issue.visible(user).exists?(m[1])) |
|
476 | 479 | @invalid_parent_issue_id = attrs.delete('parent_issue_id') |
|
477 | 480 | end |
|
478 | 481 | end |
|
479 | 482 | |
|
480 | 483 | if attrs['custom_field_values'].present? |
|
481 | 484 | editable_custom_field_ids = editable_custom_field_values(user).map {|v| v.custom_field_id.to_s} |
|
482 | 485 | attrs['custom_field_values'].select! {|k, v| editable_custom_field_ids.include?(k.to_s)} |
|
483 | 486 | end |
|
484 | 487 | |
|
485 | 488 | if attrs['custom_fields'].present? |
|
486 | 489 | editable_custom_field_ids = editable_custom_field_values(user).map {|v| v.custom_field_id.to_s} |
|
487 | 490 | attrs['custom_fields'].select! {|c| editable_custom_field_ids.include?(c['id'].to_s)} |
|
488 | 491 | end |
|
489 | 492 | |
|
490 | 493 | # mass-assignment security bypass |
|
491 | 494 | assign_attributes attrs, :without_protection => true |
|
492 | 495 | end |
|
493 | 496 | |
|
494 | 497 | def disabled_core_fields |
|
495 | 498 | tracker ? tracker.disabled_core_fields : [] |
|
496 | 499 | end |
|
497 | 500 | |
|
498 | 501 | # Returns the custom_field_values that can be edited by the given user |
|
499 | 502 | def editable_custom_field_values(user=nil) |
|
500 | 503 | visible_custom_field_values(user).reject do |value| |
|
501 | 504 | read_only_attribute_names(user).include?(value.custom_field_id.to_s) |
|
502 | 505 | end |
|
503 | 506 | end |
|
504 | 507 | |
|
505 | 508 | # Returns the custom fields that can be edited by the given user |
|
506 | 509 | def editable_custom_fields(user=nil) |
|
507 | 510 | editable_custom_field_values(user).map(&:custom_field).uniq |
|
508 | 511 | end |
|
509 | 512 | |
|
510 | 513 | # Returns the names of attributes that are read-only for user or the current user |
|
511 | 514 | # For users with multiple roles, the read-only fields are the intersection of |
|
512 | 515 | # read-only fields of each role |
|
513 | 516 | # The result is an array of strings where sustom fields are represented with their ids |
|
514 | 517 | # |
|
515 | 518 | # Examples: |
|
516 | 519 | # issue.read_only_attribute_names # => ['due_date', '2'] |
|
517 | 520 | # issue.read_only_attribute_names(user) # => [] |
|
518 | 521 | def read_only_attribute_names(user=nil) |
|
519 | 522 | workflow_rule_by_attribute(user).reject {|attr, rule| rule != 'readonly'}.keys |
|
520 | 523 | end |
|
521 | 524 | |
|
522 | 525 | # Returns the names of required attributes for user or the current user |
|
523 | 526 | # For users with multiple roles, the required fields are the intersection of |
|
524 | 527 | # required fields of each role |
|
525 | 528 | # The result is an array of strings where sustom fields are represented with their ids |
|
526 | 529 | # |
|
527 | 530 | # Examples: |
|
528 | 531 | # issue.required_attribute_names # => ['due_date', '2'] |
|
529 | 532 | # issue.required_attribute_names(user) # => [] |
|
530 | 533 | def required_attribute_names(user=nil) |
|
531 | 534 | workflow_rule_by_attribute(user).reject {|attr, rule| rule != 'required'}.keys |
|
532 | 535 | end |
|
533 | 536 | |
|
534 | 537 | # Returns true if the attribute is required for user |
|
535 | 538 | def required_attribute?(name, user=nil) |
|
536 | 539 | required_attribute_names(user).include?(name.to_s) |
|
537 | 540 | end |
|
538 | 541 | |
|
539 | 542 | # Returns a hash of the workflow rule by attribute for the given user |
|
540 | 543 | # |
|
541 | 544 | # Examples: |
|
542 | 545 | # issue.workflow_rule_by_attribute # => {'due_date' => 'required', 'start_date' => 'readonly'} |
|
543 | 546 | def workflow_rule_by_attribute(user=nil) |
|
544 | 547 | return @workflow_rule_by_attribute if @workflow_rule_by_attribute && user.nil? |
|
545 | 548 | |
|
546 | 549 | user_real = user || User.current |
|
547 | 550 | roles = user_real.admin ? Role.all.to_a : user_real.roles_for_project(project) |
|
548 | 551 | return {} if roles.empty? |
|
549 | 552 | |
|
550 | 553 | result = {} |
|
551 | 554 | workflow_permissions = WorkflowPermission.where(:tracker_id => tracker_id, :old_status_id => status_id, :role_id => roles.map(&:id)).to_a |
|
552 | 555 | if workflow_permissions.any? |
|
553 | 556 | workflow_rules = workflow_permissions.inject({}) do |h, wp| |
|
554 | 557 | h[wp.field_name] ||= [] |
|
555 | 558 | h[wp.field_name] << wp.rule |
|
556 | 559 | h |
|
557 | 560 | end |
|
558 | 561 | workflow_rules.each do |attr, rules| |
|
559 | 562 | next if rules.size < roles.size |
|
560 | 563 | uniq_rules = rules.uniq |
|
561 | 564 | if uniq_rules.size == 1 |
|
562 | 565 | result[attr] = uniq_rules.first |
|
563 | 566 | else |
|
564 | 567 | result[attr] = 'required' |
|
565 | 568 | end |
|
566 | 569 | end |
|
567 | 570 | end |
|
568 | 571 | @workflow_rule_by_attribute = result if user.nil? |
|
569 | 572 | result |
|
570 | 573 | end |
|
571 | 574 | private :workflow_rule_by_attribute |
|
572 | 575 | |
|
573 | 576 | def done_ratio |
|
574 | 577 | if Issue.use_status_for_done_ratio? && status && status.default_done_ratio |
|
575 | 578 | status.default_done_ratio |
|
576 | 579 | else |
|
577 | 580 | read_attribute(:done_ratio) |
|
578 | 581 | end |
|
579 | 582 | end |
|
580 | 583 | |
|
581 | 584 | def self.use_status_for_done_ratio? |
|
582 | 585 | Setting.issue_done_ratio == 'issue_status' |
|
583 | 586 | end |
|
584 | 587 | |
|
585 | 588 | def self.use_field_for_done_ratio? |
|
586 | 589 | Setting.issue_done_ratio == 'issue_field' |
|
587 | 590 | end |
|
588 | 591 | |
|
589 | 592 | def validate_issue |
|
590 | 593 | if due_date && start_date && (start_date_changed? || due_date_changed?) && due_date < start_date |
|
591 | 594 | errors.add :due_date, :greater_than_start_date |
|
592 | 595 | end |
|
593 | 596 | |
|
594 | 597 | if start_date && start_date_changed? && soonest_start && start_date < soonest_start |
|
595 | 598 | errors.add :start_date, :earlier_than_minimum_start_date, :date => format_date(soonest_start) |
|
596 | 599 | end |
|
597 | 600 | |
|
598 | 601 | if fixed_version |
|
599 | 602 | if !assignable_versions.include?(fixed_version) |
|
600 | 603 | errors.add :fixed_version_id, :inclusion |
|
601 | 604 | elsif reopening? && fixed_version.closed? |
|
602 | 605 | errors.add :base, I18n.t(:error_can_not_reopen_issue_on_closed_version) |
|
603 | 606 | end |
|
604 | 607 | end |
|
605 | 608 | |
|
606 | 609 | # Checks that the issue can not be added/moved to a disabled tracker |
|
607 | 610 | if project && (tracker_id_changed? || project_id_changed?) |
|
608 | 611 | unless project.trackers.include?(tracker) |
|
609 | 612 | errors.add :tracker_id, :inclusion |
|
610 | 613 | end |
|
611 | 614 | end |
|
612 | 615 | |
|
613 | 616 | # Checks parent issue assignment |
|
614 | 617 | if @invalid_parent_issue_id.present? |
|
615 | 618 | errors.add :parent_issue_id, :invalid |
|
616 | 619 | elsif @parent_issue |
|
617 | 620 | if !valid_parent_project?(@parent_issue) |
|
618 | 621 | errors.add :parent_issue_id, :invalid |
|
619 | 622 | elsif (@parent_issue != parent) && (all_dependent_issues.include?(@parent_issue) || @parent_issue.all_dependent_issues.include?(self)) |
|
620 | 623 | errors.add :parent_issue_id, :invalid |
|
621 | 624 | elsif !new_record? |
|
622 | 625 | # moving an existing issue |
|
623 | 626 | if @parent_issue.root_id != root_id |
|
624 | 627 | # we can always move to another tree |
|
625 | 628 | elsif move_possible?(@parent_issue) |
|
626 | 629 | # move accepted inside tree |
|
627 | 630 | else |
|
628 | 631 | errors.add :parent_issue_id, :invalid |
|
629 | 632 | end |
|
630 | 633 | end |
|
631 | 634 | end |
|
632 | 635 | end |
|
633 | 636 | |
|
634 | 637 | # Validates the issue against additional workflow requirements |
|
635 | 638 | def validate_required_fields |
|
636 | 639 | user = new_record? ? author : current_journal.try(:user) |
|
637 | 640 | |
|
638 | 641 | required_attribute_names(user).each do |attribute| |
|
639 | 642 | if attribute =~ /^\d+$/ |
|
640 | 643 | attribute = attribute.to_i |
|
641 | 644 | v = custom_field_values.detect {|v| v.custom_field_id == attribute } |
|
642 | 645 | if v && v.value.blank? |
|
643 | 646 | errors.add :base, v.custom_field.name + ' ' + l('activerecord.errors.messages.blank') |
|
644 | 647 | end |
|
645 | 648 | else |
|
646 | 649 | if respond_to?(attribute) && send(attribute).blank? && !disabled_core_fields.include?(attribute) |
|
647 | 650 | errors.add attribute, :blank |
|
648 | 651 | end |
|
649 | 652 | end |
|
650 | 653 | end |
|
651 | 654 | end |
|
652 | 655 | |
|
653 | 656 | # Set the done_ratio using the status if that setting is set. This will keep the done_ratios |
|
654 | 657 | # even if the user turns off the setting later |
|
655 | 658 | def update_done_ratio_from_issue_status |
|
656 | 659 | if Issue.use_status_for_done_ratio? && status && status.default_done_ratio |
|
657 | 660 | self.done_ratio = status.default_done_ratio |
|
658 | 661 | end |
|
659 | 662 | end |
|
660 | 663 | |
|
661 | 664 | def init_journal(user, notes = "") |
|
662 | 665 | @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes) |
|
663 | 666 | end |
|
664 | 667 | |
|
665 | 668 | # Returns the current journal or nil if it's not initialized |
|
666 | 669 | def current_journal |
|
667 | 670 | @current_journal |
|
668 | 671 | end |
|
669 | 672 | |
|
670 | 673 | # Returns the names of attributes that are journalized when updating the issue |
|
671 | 674 | def journalized_attribute_names |
|
672 | 675 | Issue.column_names - %w(id root_id lft rgt lock_version created_on updated_on closed_on) |
|
673 | 676 | end |
|
674 | 677 | |
|
675 | 678 | # Returns the id of the last journal or nil |
|
676 | 679 | def last_journal_id |
|
677 | 680 | if new_record? |
|
678 | 681 | nil |
|
679 | 682 | else |
|
680 | 683 | journals.maximum(:id) |
|
681 | 684 | end |
|
682 | 685 | end |
|
683 | 686 | |
|
684 | 687 | # Returns a scope for journals that have an id greater than journal_id |
|
685 | 688 | def journals_after(journal_id) |
|
686 | 689 | scope = journals.reorder("#{Journal.table_name}.id ASC") |
|
687 | 690 | if journal_id.present? |
|
688 | 691 | scope = scope.where("#{Journal.table_name}.id > ?", journal_id.to_i) |
|
689 | 692 | end |
|
690 | 693 | scope |
|
691 | 694 | end |
|
692 | 695 | |
|
693 | 696 | # Returns the initial status of the issue |
|
694 | 697 | # Returns nil for a new issue |
|
695 | 698 | def status_was |
|
696 | 699 | if status_id_changed? |
|
697 | 700 | if status_id_was.to_i > 0 |
|
698 | 701 | @status_was ||= IssueStatus.find_by_id(status_id_was) |
|
699 | 702 | end |
|
700 | 703 | else |
|
701 | 704 | @status_was ||= status |
|
702 | 705 | end |
|
703 | 706 | end |
|
704 | 707 | |
|
705 | 708 | # Return true if the issue is closed, otherwise false |
|
706 | 709 | def closed? |
|
707 | 710 | status.present? && status.is_closed? |
|
708 | 711 | end |
|
709 | 712 | |
|
710 | 713 | # Returns true if the issue was closed when loaded |
|
711 | 714 | def was_closed? |
|
712 | 715 | status_was.present? && status_was.is_closed? |
|
713 | 716 | end |
|
714 | 717 | |
|
715 | 718 | # Return true if the issue is being reopened |
|
716 | 719 | def reopening? |
|
717 | 720 | if new_record? |
|
718 | 721 | false |
|
719 | 722 | else |
|
720 | 723 | status_id_changed? && !closed? && was_closed? |
|
721 | 724 | end |
|
722 | 725 | end |
|
723 | 726 | alias :reopened? :reopening? |
|
724 | 727 | |
|
725 | 728 | # Return true if the issue is being closed |
|
726 | 729 | def closing? |
|
727 | 730 | if new_record? |
|
728 | 731 | closed? |
|
729 | 732 | else |
|
730 | 733 | status_id_changed? && closed? && !was_closed? |
|
731 | 734 | end |
|
732 | 735 | end |
|
733 | 736 | |
|
734 | 737 | # Returns true if the issue is overdue |
|
735 | 738 | def overdue? |
|
736 | 739 | due_date.present? && (due_date < Date.today) && !closed? |
|
737 | 740 | end |
|
738 | 741 | |
|
739 | 742 | # Is the amount of work done less than it should for the due date |
|
740 | 743 | def behind_schedule? |
|
741 | 744 | return false if start_date.nil? || due_date.nil? |
|
742 | 745 | done_date = start_date + ((due_date - start_date + 1) * done_ratio / 100).floor |
|
743 | 746 | return done_date <= Date.today |
|
744 | 747 | end |
|
745 | 748 | |
|
746 | 749 | # Does this issue have children? |
|
747 | 750 | def children? |
|
748 | 751 | !leaf? |
|
749 | 752 | end |
|
750 | 753 | |
|
751 | 754 | # Users the issue can be assigned to |
|
752 | 755 | def assignable_users |
|
753 | 756 | users = project.assignable_users.to_a |
|
754 | 757 | users << author if author |
|
755 | 758 | users << assigned_to if assigned_to |
|
756 | 759 | users.uniq.sort |
|
757 | 760 | end |
|
758 | 761 | |
|
759 | 762 | # Versions that the issue can be assigned to |
|
760 | 763 | def assignable_versions |
|
761 | 764 | return @assignable_versions if @assignable_versions |
|
762 | 765 | |
|
763 | 766 | versions = project.shared_versions.open.to_a |
|
764 | 767 | if fixed_version |
|
765 | 768 | if fixed_version_id_changed? |
|
766 | 769 | # nothing to do |
|
767 | 770 | elsif project_id_changed? |
|
768 | 771 | if project.shared_versions.include?(fixed_version) |
|
769 | 772 | versions << fixed_version |
|
770 | 773 | end |
|
771 | 774 | else |
|
772 | 775 | versions << fixed_version |
|
773 | 776 | end |
|
774 | 777 | end |
|
775 | 778 | @assignable_versions = versions.uniq.sort |
|
776 | 779 | end |
|
777 | 780 | |
|
778 | 781 | # Returns true if this issue is blocked by another issue that is still open |
|
779 | 782 | def blocked? |
|
780 | 783 | !relations_to.detect {|ir| ir.relation_type == 'blocks' && !ir.issue_from.closed?}.nil? |
|
781 | 784 | end |
|
782 | 785 | |
|
783 | 786 | # Returns the default status of the issue based on its tracker |
|
784 | 787 | # Returns nil if tracker is nil |
|
785 | 788 | def default_status |
|
786 | 789 | tracker.try(:default_status) |
|
787 | 790 | end |
|
788 | 791 | |
|
789 | 792 | # Returns an array of statuses that user is able to apply |
|
790 | 793 | def new_statuses_allowed_to(user=User.current, include_default=false) |
|
791 | 794 | if new_record? && @copied_from |
|
792 | 795 | [default_status, @copied_from.status].compact.uniq.sort |
|
793 | 796 | else |
|
794 | 797 | initial_status = nil |
|
795 | 798 | if new_record? |
|
796 | 799 | initial_status = default_status |
|
797 | 800 | elsif tracker_id_changed? |
|
798 | 801 | if Tracker.where(:id => tracker_id_was, :default_status_id => status_id_was).any? |
|
799 | 802 | initial_status = default_status |
|
800 | 803 | elsif tracker.issue_status_ids.include?(status_id_was) |
|
801 | 804 | initial_status = IssueStatus.find_by_id(status_id_was) |
|
802 | 805 | else |
|
803 | 806 | initial_status = default_status |
|
804 | 807 | end |
|
805 | 808 | else |
|
806 | 809 | initial_status = status_was |
|
807 | 810 | end |
|
808 | 811 | |
|
809 | 812 | initial_assigned_to_id = assigned_to_id_changed? ? assigned_to_id_was : assigned_to_id |
|
810 | 813 | assignee_transitions_allowed = initial_assigned_to_id.present? && |
|
811 | 814 | (user.id == initial_assigned_to_id || user.group_ids.include?(initial_assigned_to_id)) |
|
812 | 815 | |
|
813 | 816 | statuses = [] |
|
814 | 817 | if initial_status |
|
815 | 818 | statuses += initial_status.find_new_statuses_allowed_to( |
|
816 | 819 | user.admin ? Role.all.to_a : user.roles_for_project(project), |
|
817 | 820 | tracker, |
|
818 | 821 | author == user, |
|
819 | 822 | assignee_transitions_allowed |
|
820 | 823 | ) |
|
821 | 824 | end |
|
822 | 825 | statuses << initial_status unless statuses.empty? |
|
823 | 826 | statuses << default_status if include_default |
|
824 | 827 | statuses = statuses.compact.uniq.sort |
|
825 | 828 | if blocked? |
|
826 | 829 | statuses.reject!(&:is_closed?) |
|
827 | 830 | end |
|
828 | 831 | statuses |
|
829 | 832 | end |
|
830 | 833 | end |
|
831 | 834 | |
|
832 | 835 | # Returns the previous assignee if changed |
|
833 | 836 | def assigned_to_was |
|
834 | 837 | # assigned_to_id_was is reset before after_save callbacks |
|
835 | 838 | user_id = @previous_assigned_to_id || assigned_to_id_was |
|
836 | 839 | if user_id && user_id != assigned_to_id |
|
837 | 840 | @assigned_to_was ||= User.find_by_id(user_id) |
|
838 | 841 | end |
|
839 | 842 | end |
|
840 | 843 | |
|
841 | 844 | # Returns the users that should be notified |
|
842 | 845 | def notified_users |
|
843 | 846 | notified = [] |
|
844 | 847 | # Author and assignee are always notified unless they have been |
|
845 | 848 | # locked or don't want to be notified |
|
846 | 849 | notified << author if author |
|
847 | 850 | if assigned_to |
|
848 | 851 | notified += (assigned_to.is_a?(Group) ? assigned_to.users : [assigned_to]) |
|
849 | 852 | end |
|
850 | 853 | if assigned_to_was |
|
851 | 854 | notified += (assigned_to_was.is_a?(Group) ? assigned_to_was.users : [assigned_to_was]) |
|
852 | 855 | end |
|
853 | 856 | notified = notified.select {|u| u.active? && u.notify_about?(self)} |
|
854 | 857 | |
|
855 | 858 | notified += project.notified_users |
|
856 | 859 | notified.uniq! |
|
857 | 860 | # Remove users that can not view the issue |
|
858 | 861 | notified.reject! {|user| !visible?(user)} |
|
859 | 862 | notified |
|
860 | 863 | end |
|
861 | 864 | |
|
862 | 865 | # Returns the email addresses that should be notified |
|
863 | 866 | def recipients |
|
864 | 867 | notified_users.collect(&:mail) |
|
865 | 868 | end |
|
866 | 869 | |
|
867 | 870 | def each_notification(users, &block) |
|
868 | 871 | if users.any? |
|
869 | 872 | if custom_field_values.detect {|value| !value.custom_field.visible?} |
|
870 | 873 | users_by_custom_field_visibility = users.group_by do |user| |
|
871 | 874 | visible_custom_field_values(user).map(&:custom_field_id).sort |
|
872 | 875 | end |
|
873 | 876 | users_by_custom_field_visibility.values.each do |users| |
|
874 | 877 | yield(users) |
|
875 | 878 | end |
|
876 | 879 | else |
|
877 | 880 | yield(users) |
|
878 | 881 | end |
|
879 | 882 | end |
|
880 | 883 | end |
|
881 | 884 | |
|
882 | 885 | # Returns the number of hours spent on this issue |
|
883 | 886 | def spent_hours |
|
884 | 887 | @spent_hours ||= time_entries.sum(:hours) || 0 |
|
885 | 888 | end |
|
886 | 889 | |
|
887 | 890 | # Returns the total number of hours spent on this issue and its descendants |
|
888 | 891 | # |
|
889 | 892 | # Example: |
|
890 | 893 | # spent_hours => 0.0 |
|
891 | 894 | # spent_hours => 50.2 |
|
892 | 895 | def total_spent_hours |
|
893 | 896 | @total_spent_hours ||= |
|
894 | 897 | self_and_descendants. |
|
895 | 898 | joins("LEFT JOIN #{TimeEntry.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"). |
|
896 | 899 | sum("#{TimeEntry.table_name}.hours").to_f || 0.0 |
|
897 | 900 | end |
|
898 | 901 | |
|
899 | 902 | def relations |
|
900 | 903 | @relations ||= IssueRelation::Relations.new(self, (relations_from + relations_to).sort) |
|
901 | 904 | end |
|
902 | 905 | |
|
903 | 906 | # Preloads relations for a collection of issues |
|
904 | 907 | def self.load_relations(issues) |
|
905 | 908 | if issues.any? |
|
906 | 909 | relations = IssueRelation.where("issue_from_id IN (:ids) OR issue_to_id IN (:ids)", :ids => issues.map(&:id)).all |
|
907 | 910 | issues.each do |issue| |
|
908 | 911 | issue.instance_variable_set "@relations", relations.select {|r| r.issue_from_id == issue.id || r.issue_to_id == issue.id} |
|
909 | 912 | end |
|
910 | 913 | end |
|
911 | 914 | end |
|
912 | 915 | |
|
913 | 916 | # Preloads visible spent time for a collection of issues |
|
914 | 917 | def self.load_visible_spent_hours(issues, user=User.current) |
|
915 | 918 | if issues.any? |
|
916 | 919 | hours_by_issue_id = TimeEntry.visible(user).group(:issue_id).sum(:hours) |
|
917 | 920 | issues.each do |issue| |
|
918 | 921 | issue.instance_variable_set "@spent_hours", (hours_by_issue_id[issue.id] || 0) |
|
919 | 922 | end |
|
920 | 923 | end |
|
921 | 924 | end |
|
922 | 925 | |
|
923 | 926 | # Preloads visible relations for a collection of issues |
|
924 | 927 | def self.load_visible_relations(issues, user=User.current) |
|
925 | 928 | if issues.any? |
|
926 | 929 | issue_ids = issues.map(&:id) |
|
927 | 930 | # Relations with issue_from in given issues and visible issue_to |
|
928 | 931 | relations_from = IssueRelation.joins(:issue_to => :project). |
|
929 | 932 | where(visible_condition(user)).where(:issue_from_id => issue_ids).to_a |
|
930 | 933 | # Relations with issue_to in given issues and visible issue_from |
|
931 | 934 | relations_to = IssueRelation.joins(:issue_from => :project). |
|
932 | 935 | where(visible_condition(user)). |
|
933 | 936 | where(:issue_to_id => issue_ids).to_a |
|
934 | 937 | issues.each do |issue| |
|
935 | 938 | relations = |
|
936 | 939 | relations_from.select {|relation| relation.issue_from_id == issue.id} + |
|
937 | 940 | relations_to.select {|relation| relation.issue_to_id == issue.id} |
|
938 | 941 | |
|
939 | 942 | issue.instance_variable_set "@relations", IssueRelation::Relations.new(issue, relations.sort) |
|
940 | 943 | end |
|
941 | 944 | end |
|
942 | 945 | end |
|
943 | 946 | |
|
944 | 947 | # Finds an issue relation given its id. |
|
945 | 948 | def find_relation(relation_id) |
|
946 | 949 | IssueRelation.where("issue_to_id = ? OR issue_from_id = ?", id, id).find(relation_id) |
|
947 | 950 | end |
|
948 | 951 | |
|
949 | 952 | # Returns all the other issues that depend on the issue |
|
950 | 953 | # The algorithm is a modified breadth first search (bfs) |
|
951 | 954 | def all_dependent_issues(except=[]) |
|
952 | 955 | # The found dependencies |
|
953 | 956 | dependencies = [] |
|
954 | 957 | |
|
955 | 958 | # The visited flag for every node (issue) used by the breadth first search |
|
956 | 959 | eNOT_DISCOVERED = 0 # The issue is "new" to the algorithm, it has not seen it before. |
|
957 | 960 | |
|
958 | 961 | ePROCESS_ALL = 1 # The issue is added to the queue. Process both children and relations of |
|
959 | 962 | # the issue when it is processed. |
|
960 | 963 | |
|
961 | 964 | ePROCESS_RELATIONS_ONLY = 2 # The issue was added to the queue and will be output as dependent issue, |
|
962 | 965 | # but its children will not be added to the queue when it is processed. |
|
963 | 966 | |
|
964 | 967 | eRELATIONS_PROCESSED = 3 # The related issues, the parent issue and the issue itself have been added to |
|
965 | 968 | # the queue, but its children have not been added. |
|
966 | 969 | |
|
967 | 970 | ePROCESS_CHILDREN_ONLY = 4 # The relations and the parent of the issue have been added to the queue, but |
|
968 | 971 | # the children still need to be processed. |
|
969 | 972 | |
|
970 | 973 | eALL_PROCESSED = 5 # The issue and all its children, its parent and its related issues have been |
|
971 | 974 | # added as dependent issues. It needs no further processing. |
|
972 | 975 | |
|
973 | 976 | issue_status = Hash.new(eNOT_DISCOVERED) |
|
974 | 977 | |
|
975 | 978 | # The queue |
|
976 | 979 | queue = [] |
|
977 | 980 | |
|
978 | 981 | # Initialize the bfs, add start node (self) to the queue |
|
979 | 982 | queue << self |
|
980 | 983 | issue_status[self] = ePROCESS_ALL |
|
981 | 984 | |
|
982 | 985 | while (!queue.empty?) do |
|
983 | 986 | current_issue = queue.shift |
|
984 | 987 | current_issue_status = issue_status[current_issue] |
|
985 | 988 | dependencies << current_issue |
|
986 | 989 | |
|
987 | 990 | # Add parent to queue, if not already in it. |
|
988 | 991 | parent = current_issue.parent |
|
989 | 992 | parent_status = issue_status[parent] |
|
990 | 993 | |
|
991 | 994 | if parent && (parent_status == eNOT_DISCOVERED) && !except.include?(parent) |
|
992 | 995 | queue << parent |
|
993 | 996 | issue_status[parent] = ePROCESS_RELATIONS_ONLY |
|
994 | 997 | end |
|
995 | 998 | |
|
996 | 999 | # Add children to queue, but only if they are not already in it and |
|
997 | 1000 | # the children of the current node need to be processed. |
|
998 | 1001 | if (current_issue_status == ePROCESS_CHILDREN_ONLY || current_issue_status == ePROCESS_ALL) |
|
999 | 1002 | current_issue.children.each do |child| |
|
1000 | 1003 | next if except.include?(child) |
|
1001 | 1004 | |
|
1002 | 1005 | if (issue_status[child] == eNOT_DISCOVERED) |
|
1003 | 1006 | queue << child |
|
1004 | 1007 | issue_status[child] = ePROCESS_ALL |
|
1005 | 1008 | elsif (issue_status[child] == eRELATIONS_PROCESSED) |
|
1006 | 1009 | queue << child |
|
1007 | 1010 | issue_status[child] = ePROCESS_CHILDREN_ONLY |
|
1008 | 1011 | elsif (issue_status[child] == ePROCESS_RELATIONS_ONLY) |
|
1009 | 1012 | queue << child |
|
1010 | 1013 | issue_status[child] = ePROCESS_ALL |
|
1011 | 1014 | end |
|
1012 | 1015 | end |
|
1013 | 1016 | end |
|
1014 | 1017 | |
|
1015 | 1018 | # Add related issues to the queue, if they are not already in it. |
|
1016 | 1019 | current_issue.relations_from.map(&:issue_to).each do |related_issue| |
|
1017 | 1020 | next if except.include?(related_issue) |
|
1018 | 1021 | |
|
1019 | 1022 | if (issue_status[related_issue] == eNOT_DISCOVERED) |
|
1020 | 1023 | queue << related_issue |
|
1021 | 1024 | issue_status[related_issue] = ePROCESS_ALL |
|
1022 | 1025 | elsif (issue_status[related_issue] == eRELATIONS_PROCESSED) |
|
1023 | 1026 | queue << related_issue |
|
1024 | 1027 | issue_status[related_issue] = ePROCESS_CHILDREN_ONLY |
|
1025 | 1028 | elsif (issue_status[related_issue] == ePROCESS_RELATIONS_ONLY) |
|
1026 | 1029 | queue << related_issue |
|
1027 | 1030 | issue_status[related_issue] = ePROCESS_ALL |
|
1028 | 1031 | end |
|
1029 | 1032 | end |
|
1030 | 1033 | |
|
1031 | 1034 | # Set new status for current issue |
|
1032 | 1035 | if (current_issue_status == ePROCESS_ALL) || (current_issue_status == ePROCESS_CHILDREN_ONLY) |
|
1033 | 1036 | issue_status[current_issue] = eALL_PROCESSED |
|
1034 | 1037 | elsif (current_issue_status == ePROCESS_RELATIONS_ONLY) |
|
1035 | 1038 | issue_status[current_issue] = eRELATIONS_PROCESSED |
|
1036 | 1039 | end |
|
1037 | 1040 | end # while |
|
1038 | 1041 | |
|
1039 | 1042 | # Remove the issues from the "except" parameter from the result array |
|
1040 | 1043 | dependencies -= except |
|
1041 | 1044 | dependencies.delete(self) |
|
1042 | 1045 | |
|
1043 | 1046 | dependencies |
|
1044 | 1047 | end |
|
1045 | 1048 | |
|
1046 | 1049 | # Returns an array of issues that duplicate this one |
|
1047 | 1050 | def duplicates |
|
1048 | 1051 | relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from} |
|
1049 | 1052 | end |
|
1050 | 1053 | |
|
1051 | 1054 | # Returns the due date or the target due date if any |
|
1052 | 1055 | # Used on gantt chart |
|
1053 | 1056 | def due_before |
|
1054 | 1057 | due_date || (fixed_version ? fixed_version.effective_date : nil) |
|
1055 | 1058 | end |
|
1056 | 1059 | |
|
1057 | 1060 | # Returns the time scheduled for this issue. |
|
1058 | 1061 | # |
|
1059 | 1062 | # Example: |
|
1060 | 1063 | # Start Date: 2/26/09, End Date: 3/04/09 |
|
1061 | 1064 | # duration => 6 |
|
1062 | 1065 | def duration |
|
1063 | 1066 | (start_date && due_date) ? due_date - start_date : 0 |
|
1064 | 1067 | end |
|
1065 | 1068 | |
|
1066 | 1069 | # Returns the duration in working days |
|
1067 | 1070 | def working_duration |
|
1068 | 1071 | (start_date && due_date) ? working_days(start_date, due_date) : 0 |
|
1069 | 1072 | end |
|
1070 | 1073 | |
|
1071 | 1074 | def soonest_start(reload=false) |
|
1072 | 1075 | @soonest_start = nil if reload |
|
1073 | 1076 | @soonest_start ||= ( |
|
1074 | 1077 | relations_to(reload).collect{|relation| relation.successor_soonest_start} + |
|
1075 | 1078 | [(@parent_issue || parent).try(:soonest_start)] |
|
1076 | 1079 | ).compact.max |
|
1077 | 1080 | end |
|
1078 | 1081 | |
|
1079 | 1082 | # Sets start_date on the given date or the next working day |
|
1080 | 1083 | # and changes due_date to keep the same working duration. |
|
1081 | 1084 | def reschedule_on(date) |
|
1082 | 1085 | wd = working_duration |
|
1083 | 1086 | date = next_working_date(date) |
|
1084 | 1087 | self.start_date = date |
|
1085 | 1088 | self.due_date = add_working_days(date, wd) |
|
1086 | 1089 | end |
|
1087 | 1090 | |
|
1088 | 1091 | # Reschedules the issue on the given date or the next working day and saves the record. |
|
1089 | 1092 | # If the issue is a parent task, this is done by rescheduling its subtasks. |
|
1090 | 1093 | def reschedule_on!(date) |
|
1091 | 1094 | return if date.nil? |
|
1092 | 1095 | if leaf? |
|
1093 | 1096 | if start_date.nil? || start_date != date |
|
1094 | 1097 | if start_date && start_date > date |
|
1095 | 1098 | # Issue can not be moved earlier than its soonest start date |
|
1096 | 1099 | date = [soonest_start(true), date].compact.max |
|
1097 | 1100 | end |
|
1098 | 1101 | reschedule_on(date) |
|
1099 | 1102 | begin |
|
1100 | 1103 | save |
|
1101 | 1104 | rescue ActiveRecord::StaleObjectError |
|
1102 | 1105 | reload |
|
1103 | 1106 | reschedule_on(date) |
|
1104 | 1107 | save |
|
1105 | 1108 | end |
|
1106 | 1109 | end |
|
1107 | 1110 | else |
|
1108 | 1111 | leaves.each do |leaf| |
|
1109 | 1112 | if leaf.start_date |
|
1110 | 1113 | # Only move subtask if it starts at the same date as the parent |
|
1111 | 1114 | # or if it starts before the given date |
|
1112 | 1115 | if start_date == leaf.start_date || date > leaf.start_date |
|
1113 | 1116 | leaf.reschedule_on!(date) |
|
1114 | 1117 | end |
|
1115 | 1118 | else |
|
1116 | 1119 | leaf.reschedule_on!(date) |
|
1117 | 1120 | end |
|
1118 | 1121 | end |
|
1119 | 1122 | end |
|
1120 | 1123 | end |
|
1121 | 1124 | |
|
1122 | 1125 | def <=>(issue) |
|
1123 | 1126 | if issue.nil? |
|
1124 | 1127 | -1 |
|
1125 | 1128 | elsif root_id != issue.root_id |
|
1126 | 1129 | (root_id || 0) <=> (issue.root_id || 0) |
|
1127 | 1130 | else |
|
1128 | 1131 | (lft || 0) <=> (issue.lft || 0) |
|
1129 | 1132 | end |
|
1130 | 1133 | end |
|
1131 | 1134 | |
|
1132 | 1135 | def to_s |
|
1133 | 1136 | "#{tracker} ##{id}: #{subject}" |
|
1134 | 1137 | end |
|
1135 | 1138 | |
|
1136 | 1139 | # Returns a string of css classes that apply to the issue |
|
1137 | 1140 | def css_classes(user=User.current) |
|
1138 | 1141 | s = "issue tracker-#{tracker_id} status-#{status_id} #{priority.try(:css_classes)}" |
|
1139 | 1142 | s << ' closed' if closed? |
|
1140 | 1143 | s << ' overdue' if overdue? |
|
1141 | 1144 | s << ' child' if child? |
|
1142 | 1145 | s << ' parent' unless leaf? |
|
1143 | 1146 | s << ' private' if is_private? |
|
1144 | 1147 | if user.logged? |
|
1145 | 1148 | s << ' created-by-me' if author_id == user.id |
|
1146 | 1149 | s << ' assigned-to-me' if assigned_to_id == user.id |
|
1147 | 1150 | s << ' assigned-to-my-group' if user.groups.any? {|g| g.id == assigned_to_id} |
|
1148 | 1151 | end |
|
1149 | 1152 | s |
|
1150 | 1153 | end |
|
1151 | 1154 | |
|
1152 | 1155 | # Unassigns issues from +version+ if it's no longer shared with issue's project |
|
1153 | 1156 | def self.update_versions_from_sharing_change(version) |
|
1154 | 1157 | # Update issues assigned to the version |
|
1155 | 1158 | update_versions(["#{Issue.table_name}.fixed_version_id = ?", version.id]) |
|
1156 | 1159 | end |
|
1157 | 1160 | |
|
1158 | 1161 | # Unassigns issues from versions that are no longer shared |
|
1159 | 1162 | # after +project+ was moved |
|
1160 | 1163 | def self.update_versions_from_hierarchy_change(project) |
|
1161 | 1164 | moved_project_ids = project.self_and_descendants.reload.collect(&:id) |
|
1162 | 1165 | # Update issues of the moved projects and issues assigned to a version of a moved project |
|
1163 | 1166 | Issue.update_versions( |
|
1164 | 1167 | ["#{Version.table_name}.project_id IN (?) OR #{Issue.table_name}.project_id IN (?)", |
|
1165 | 1168 | moved_project_ids, moved_project_ids] |
|
1166 | 1169 | ) |
|
1167 | 1170 | end |
|
1168 | 1171 | |
|
1169 | 1172 | def parent_issue_id=(arg) |
|
1170 | 1173 | s = arg.to_s.strip.presence |
|
1171 | 1174 | if s && (m = s.match(%r{\A#?(\d+)\z})) && (@parent_issue = Issue.find_by_id(m[1])) |
|
1172 | 1175 | @invalid_parent_issue_id = nil |
|
1173 | 1176 | elsif s.blank? |
|
1174 | 1177 | @parent_issue = nil |
|
1175 | 1178 | @invalid_parent_issue_id = nil |
|
1176 | 1179 | else |
|
1177 | 1180 | @parent_issue = nil |
|
1178 | 1181 | @invalid_parent_issue_id = arg |
|
1179 | 1182 | end |
|
1180 | 1183 | end |
|
1181 | 1184 | |
|
1182 | 1185 | def parent_issue_id |
|
1183 | 1186 | if @invalid_parent_issue_id |
|
1184 | 1187 | @invalid_parent_issue_id |
|
1185 | 1188 | elsif instance_variable_defined? :@parent_issue |
|
1186 | 1189 | @parent_issue.nil? ? nil : @parent_issue.id |
|
1187 | 1190 | else |
|
1188 | 1191 | parent_id |
|
1189 | 1192 | end |
|
1190 | 1193 | end |
|
1191 | 1194 | |
|
1192 | 1195 | # Returns true if issue's project is a valid |
|
1193 | 1196 | # parent issue project |
|
1194 | 1197 | def valid_parent_project?(issue=parent) |
|
1195 | 1198 | return true if issue.nil? || issue.project_id == project_id |
|
1196 | 1199 | |
|
1197 | 1200 | case Setting.cross_project_subtasks |
|
1198 | 1201 | when 'system' |
|
1199 | 1202 | true |
|
1200 | 1203 | when 'tree' |
|
1201 | 1204 | issue.project.root == project.root |
|
1202 | 1205 | when 'hierarchy' |
|
1203 | 1206 | issue.project.is_or_is_ancestor_of?(project) || issue.project.is_descendant_of?(project) |
|
1204 | 1207 | when 'descendants' |
|
1205 | 1208 | issue.project.is_or_is_ancestor_of?(project) |
|
1206 | 1209 | else |
|
1207 | 1210 | false |
|
1208 | 1211 | end |
|
1209 | 1212 | end |
|
1210 | 1213 | |
|
1211 | 1214 | # Returns an issue scope based on project and scope |
|
1212 | 1215 | def self.cross_project_scope(project, scope=nil) |
|
1213 | 1216 | if project.nil? |
|
1214 | 1217 | return Issue |
|
1215 | 1218 | end |
|
1216 | 1219 | case scope |
|
1217 | 1220 | when 'all', 'system' |
|
1218 | 1221 | Issue |
|
1219 | 1222 | when 'tree' |
|
1220 | 1223 | Issue.joins(:project).where("(#{Project.table_name}.lft >= :lft AND #{Project.table_name}.rgt <= :rgt)", |
|
1221 | 1224 | :lft => project.root.lft, :rgt => project.root.rgt) |
|
1222 | 1225 | when 'hierarchy' |
|
1223 | 1226 | Issue.joins(:project).where("(#{Project.table_name}.lft >= :lft AND #{Project.table_name}.rgt <= :rgt) OR (#{Project.table_name}.lft < :lft AND #{Project.table_name}.rgt > :rgt)", |
|
1224 | 1227 | :lft => project.lft, :rgt => project.rgt) |
|
1225 | 1228 | when 'descendants' |
|
1226 | 1229 | Issue.joins(:project).where("(#{Project.table_name}.lft >= :lft AND #{Project.table_name}.rgt <= :rgt)", |
|
1227 | 1230 | :lft => project.lft, :rgt => project.rgt) |
|
1228 | 1231 | else |
|
1229 | 1232 | Issue.where(:project_id => project.id) |
|
1230 | 1233 | end |
|
1231 | 1234 | end |
|
1232 | 1235 | |
|
1233 | 1236 | def self.by_tracker(project) |
|
1234 | 1237 | count_and_group_by(:project => project, :association => :tracker) |
|
1235 | 1238 | end |
|
1236 | 1239 | |
|
1237 | 1240 | def self.by_version(project) |
|
1238 | 1241 | count_and_group_by(:project => project, :association => :fixed_version) |
|
1239 | 1242 | end |
|
1240 | 1243 | |
|
1241 | 1244 | def self.by_priority(project) |
|
1242 | 1245 | count_and_group_by(:project => project, :association => :priority) |
|
1243 | 1246 | end |
|
1244 | 1247 | |
|
1245 | 1248 | def self.by_category(project) |
|
1246 | 1249 | count_and_group_by(:project => project, :association => :category) |
|
1247 | 1250 | end |
|
1248 | 1251 | |
|
1249 | 1252 | def self.by_assigned_to(project) |
|
1250 | 1253 | count_and_group_by(:project => project, :association => :assigned_to) |
|
1251 | 1254 | end |
|
1252 | 1255 | |
|
1253 | 1256 | def self.by_author(project) |
|
1254 | 1257 | count_and_group_by(:project => project, :association => :author) |
|
1255 | 1258 | end |
|
1256 | 1259 | |
|
1257 | 1260 | def self.by_subproject(project) |
|
1258 | 1261 | r = count_and_group_by(:project => project, :with_subprojects => true, :association => :project) |
|
1259 | 1262 | r.reject {|r| r["project_id"] == project.id.to_s} |
|
1260 | 1263 | end |
|
1261 | 1264 | |
|
1262 | 1265 | # Query generator for selecting groups of issue counts for a project |
|
1263 | 1266 | # based on specific criteria |
|
1264 | 1267 | # |
|
1265 | 1268 | # Options |
|
1266 | 1269 | # * project - Project to search in. |
|
1267 | 1270 | # * with_subprojects - Includes subprojects issues if set to true. |
|
1268 | 1271 | # * association - Symbol. Association for grouping. |
|
1269 | 1272 | def self.count_and_group_by(options) |
|
1270 | 1273 | assoc = reflect_on_association(options[:association]) |
|
1271 | 1274 | select_field = assoc.foreign_key |
|
1272 | 1275 | |
|
1273 | 1276 | Issue. |
|
1274 | 1277 | visible(User.current, :project => options[:project], :with_subprojects => options[:with_subprojects]). |
|
1275 | 1278 | joins(:status, assoc.name). |
|
1276 | 1279 | group(:status_id, :is_closed, select_field). |
|
1277 | 1280 | count. |
|
1278 | 1281 | map do |columns, total| |
|
1279 | 1282 | status_id, is_closed, field_value = columns |
|
1280 | 1283 | is_closed = ['t', 'true', '1'].include?(is_closed.to_s) |
|
1281 | 1284 | { |
|
1282 | 1285 | "status_id" => status_id.to_s, |
|
1283 | 1286 | "closed" => is_closed, |
|
1284 | 1287 | select_field => field_value.to_s, |
|
1285 | 1288 | "total" => total.to_s |
|
1286 | 1289 | } |
|
1287 | 1290 | end |
|
1288 | 1291 | end |
|
1289 | 1292 | |
|
1290 | 1293 | # Returns a scope of projects that user can assign the issue to |
|
1291 | 1294 | def allowed_target_projects(user=User.current) |
|
1292 | 1295 | if new_record? |
|
1293 | 1296 | Project.where(Project.allowed_to_condition(user, :add_issues)) |
|
1294 | 1297 | else |
|
1295 | 1298 | self.class.allowed_target_projects_on_move(user) |
|
1296 | 1299 | end |
|
1297 | 1300 | end |
|
1298 | 1301 | |
|
1299 | 1302 | # Returns a scope of projects that user can move issues to |
|
1300 | 1303 | def self.allowed_target_projects_on_move(user=User.current) |
|
1301 | 1304 | Project.where(Project.allowed_to_condition(user, :move_issues)) |
|
1302 | 1305 | end |
|
1303 | 1306 | |
|
1304 | 1307 | private |
|
1305 | 1308 | |
|
1306 | 1309 | def after_project_change |
|
1307 | 1310 | # Update project_id on related time entries |
|
1308 | 1311 | TimeEntry.where({:issue_id => id}).update_all(["project_id = ?", project_id]) |
|
1309 | 1312 | |
|
1310 | 1313 | # Delete issue relations |
|
1311 | 1314 | unless Setting.cross_project_issue_relations? |
|
1312 | 1315 | relations_from.clear |
|
1313 | 1316 | relations_to.clear |
|
1314 | 1317 | end |
|
1315 | 1318 | |
|
1316 | 1319 | # Move subtasks that were in the same project |
|
1317 | 1320 | children.each do |child| |
|
1318 | 1321 | next unless child.project_id == project_id_was |
|
1319 | 1322 | # Change project and keep project |
|
1320 | 1323 | child.send :project=, project, true |
|
1321 | 1324 | unless child.save |
|
1322 | 1325 | raise ActiveRecord::Rollback |
|
1323 | 1326 | end |
|
1324 | 1327 | end |
|
1325 | 1328 | end |
|
1326 | 1329 | |
|
1327 | 1330 | # Callback for after the creation of an issue by copy |
|
1328 | 1331 | # * adds a "copied to" relation with the copied issue |
|
1329 | 1332 | # * copies subtasks from the copied issue |
|
1330 | 1333 | def after_create_from_copy |
|
1331 | 1334 | return unless copy? && !@after_create_from_copy_handled |
|
1332 | 1335 | |
|
1333 | 1336 | if (@copied_from.project_id == project_id || Setting.cross_project_issue_relations?) && @copy_options[:link] != false |
|
1334 | 1337 | if @current_journal |
|
1335 | 1338 | @copied_from.init_journal(@current_journal.user) |
|
1336 | 1339 | end |
|
1337 | 1340 | relation = IssueRelation.new(:issue_from => @copied_from, :issue_to => self, :relation_type => IssueRelation::TYPE_COPIED_TO) |
|
1338 | 1341 | unless relation.save |
|
1339 | 1342 | logger.error "Could not create relation while copying ##{@copied_from.id} to ##{id} due to validation errors: #{relation.errors.full_messages.join(', ')}" if logger |
|
1340 | 1343 | end |
|
1341 | 1344 | end |
|
1342 | 1345 | |
|
1343 | 1346 | unless @copied_from.leaf? || @copy_options[:subtasks] == false |
|
1344 | 1347 | copy_options = (@copy_options || {}).merge(:subtasks => false) |
|
1345 | 1348 | copied_issue_ids = {@copied_from.id => self.id} |
|
1346 | 1349 | @copied_from.reload.descendants.reorder("#{Issue.table_name}.lft").each do |child| |
|
1347 | 1350 | # Do not copy self when copying an issue as a descendant of the copied issue |
|
1348 | 1351 | next if child == self |
|
1349 | 1352 | # Do not copy subtasks of issues that were not copied |
|
1350 | 1353 | next unless copied_issue_ids[child.parent_id] |
|
1351 | 1354 | # Do not copy subtasks that are not visible to avoid potential disclosure of private data |
|
1352 | 1355 | unless child.visible? |
|
1353 | 1356 | logger.error "Subtask ##{child.id} was not copied during ##{@copied_from.id} copy because it is not visible to the current user" if logger |
|
1354 | 1357 | next |
|
1355 | 1358 | end |
|
1356 | 1359 | copy = Issue.new.copy_from(child, copy_options) |
|
1357 | 1360 | if @current_journal |
|
1358 | 1361 | copy.init_journal(@current_journal.user) |
|
1359 | 1362 | end |
|
1360 | 1363 | copy.author = author |
|
1361 | 1364 | copy.project = project |
|
1362 | 1365 | copy.parent_issue_id = copied_issue_ids[child.parent_id] |
|
1363 | 1366 | unless copy.save |
|
1364 | 1367 | logger.error "Could not copy subtask ##{child.id} while copying ##{@copied_from.id} to ##{id} due to validation errors: #{copy.errors.full_messages.join(', ')}" if logger |
|
1365 | 1368 | next |
|
1366 | 1369 | end |
|
1367 | 1370 | copied_issue_ids[child.id] = copy.id |
|
1368 | 1371 | end |
|
1369 | 1372 | end |
|
1370 | 1373 | @after_create_from_copy_handled = true |
|
1371 | 1374 | end |
|
1372 | 1375 | |
|
1373 | 1376 | def update_nested_set_attributes |
|
1374 | 1377 | if root_id.nil? |
|
1375 | 1378 | # issue was just created |
|
1376 | 1379 | self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id) |
|
1377 | 1380 | Issue.where(["id = ?", id]).update_all(["root_id = ?", root_id]) |
|
1378 | 1381 | if @parent_issue |
|
1379 | 1382 | move_to_child_of(@parent_issue) |
|
1380 | 1383 | end |
|
1381 | 1384 | elsif parent_issue_id != parent_id |
|
1382 | 1385 | update_nested_set_attributes_on_parent_change |
|
1383 | 1386 | end |
|
1384 | 1387 | remove_instance_variable(:@parent_issue) if instance_variable_defined?(:@parent_issue) |
|
1385 | 1388 | end |
|
1386 | 1389 | |
|
1387 | 1390 | # Updates the nested set for when an existing issue is moved |
|
1388 | 1391 | def update_nested_set_attributes_on_parent_change |
|
1389 | 1392 | former_parent_id = parent_id |
|
1390 | 1393 | # moving an existing issue |
|
1391 | 1394 | if @parent_issue && @parent_issue.root_id == root_id |
|
1392 | 1395 | # inside the same tree |
|
1393 | 1396 | move_to_child_of(@parent_issue) |
|
1394 | 1397 | else |
|
1395 | 1398 | # to another tree |
|
1396 | 1399 | unless root? |
|
1397 | 1400 | move_to_right_of(root) |
|
1398 | 1401 | end |
|
1399 | 1402 | old_root_id = root_id |
|
1400 | 1403 | in_tenacious_transaction do |
|
1401 | 1404 | @parent_issue.reload_nested_set if @parent_issue |
|
1402 | 1405 | self.reload_nested_set |
|
1403 | 1406 | self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id) |
|
1404 | 1407 | cond = ["root_id = ? AND lft >= ? AND rgt <= ? ", old_root_id, lft, rgt] |
|
1405 | 1408 | self.class.base_class.select('id').lock(true).where(cond) |
|
1406 | 1409 | offset = rdm_right_most_bound + 1 - lft |
|
1407 | 1410 | Issue.where(cond). |
|
1408 | 1411 | update_all(["root_id = ?, lft = lft + ?, rgt = rgt + ?", root_id, offset, offset]) |
|
1409 | 1412 | end |
|
1410 | 1413 | if @parent_issue |
|
1411 | 1414 | move_to_child_of(@parent_issue) |
|
1412 | 1415 | end |
|
1413 | 1416 | end |
|
1414 | 1417 | # delete invalid relations of all descendants |
|
1415 | 1418 | self_and_descendants.each do |issue| |
|
1416 | 1419 | issue.relations.each do |relation| |
|
1417 | 1420 | relation.destroy unless relation.valid? |
|
1418 | 1421 | end |
|
1419 | 1422 | end |
|
1420 | 1423 | # update former parent |
|
1421 | 1424 | recalculate_attributes_for(former_parent_id) if former_parent_id |
|
1422 | 1425 | end |
|
1423 | 1426 | |
|
1424 | 1427 | def rdm_right_most_bound |
|
1425 | 1428 | right_most_node = |
|
1426 | 1429 | self.class.base_class.unscoped. |
|
1427 | 1430 | order("#{quoted_right_column_full_name} desc").limit(1).lock(true).first |
|
1428 | 1431 | right_most_node ? (right_most_node[right_column_name] || 0 ) : 0 |
|
1429 | 1432 | end |
|
1430 | 1433 | private :rdm_right_most_bound |
|
1431 | 1434 | |
|
1432 | 1435 | def update_parent_attributes |
|
1433 | 1436 | recalculate_attributes_for(parent_id) if parent_id |
|
1434 | 1437 | end |
|
1435 | 1438 | |
|
1436 | 1439 | def recalculate_attributes_for(issue_id) |
|
1437 | 1440 | if issue_id && p = Issue.find_by_id(issue_id) |
|
1438 | 1441 | # priority = highest priority of children |
|
1439 | 1442 | if priority_position = p.children.joins(:priority).maximum("#{IssuePriority.table_name}.position") |
|
1440 | 1443 | p.priority = IssuePriority.find_by_position(priority_position) |
|
1441 | 1444 | end |
|
1442 | 1445 | |
|
1443 | 1446 | # start/due dates = lowest/highest dates of children |
|
1444 | 1447 | p.start_date = p.children.minimum(:start_date) |
|
1445 | 1448 | p.due_date = p.children.maximum(:due_date) |
|
1446 | 1449 | if p.start_date && p.due_date && p.due_date < p.start_date |
|
1447 | 1450 | p.start_date, p.due_date = p.due_date, p.start_date |
|
1448 | 1451 | end |
|
1449 | 1452 | |
|
1450 | 1453 | # done ratio = weighted average ratio of leaves |
|
1451 | 1454 | unless Issue.use_status_for_done_ratio? && p.status && p.status.default_done_ratio |
|
1452 | 1455 | leaves_count = p.leaves.count |
|
1453 | 1456 | if leaves_count > 0 |
|
1454 | 1457 | average = p.leaves.where("estimated_hours > 0").average(:estimated_hours).to_f |
|
1455 | 1458 | if average == 0 |
|
1456 | 1459 | average = 1 |
|
1457 | 1460 | end |
|
1458 | 1461 | done = p.leaves.joins(:status). |
|
1459 | 1462 | sum("COALESCE(CASE WHEN estimated_hours > 0 THEN estimated_hours ELSE NULL END, #{average}) " + |
|
1460 | 1463 | "* (CASE WHEN is_closed = #{self.class.connection.quoted_true} THEN 100 ELSE COALESCE(done_ratio, 0) END)").to_f |
|
1461 | 1464 | progress = done / (average * leaves_count) |
|
1462 | 1465 | p.done_ratio = progress.round |
|
1463 | 1466 | end |
|
1464 | 1467 | end |
|
1465 | 1468 | |
|
1466 | 1469 | # estimate = sum of leaves estimates |
|
1467 | 1470 | p.estimated_hours = p.leaves.sum(:estimated_hours).to_f |
|
1468 | 1471 | p.estimated_hours = nil if p.estimated_hours == 0.0 |
|
1469 | 1472 | |
|
1470 | 1473 | # ancestors will be recursively updated |
|
1471 | 1474 | p.save(:validate => false) |
|
1472 | 1475 | end |
|
1473 | 1476 | end |
|
1474 | 1477 | |
|
1475 | 1478 | # Update issues so their versions are not pointing to a |
|
1476 | 1479 | # fixed_version that is not shared with the issue's project |
|
1477 | 1480 | def self.update_versions(conditions=nil) |
|
1478 | 1481 | # Only need to update issues with a fixed_version from |
|
1479 | 1482 | # a different project and that is not systemwide shared |
|
1480 | 1483 | Issue.joins(:project, :fixed_version). |
|
1481 | 1484 | where("#{Issue.table_name}.fixed_version_id IS NOT NULL" + |
|
1482 | 1485 | " AND #{Issue.table_name}.project_id <> #{Version.table_name}.project_id" + |
|
1483 | 1486 | " AND #{Version.table_name}.sharing <> 'system'"). |
|
1484 | 1487 | where(conditions).each do |issue| |
|
1485 | 1488 | next if issue.project.nil? || issue.fixed_version.nil? |
|
1486 | 1489 | unless issue.project.shared_versions.include?(issue.fixed_version) |
|
1487 | 1490 | issue.init_journal(User.current) |
|
1488 | 1491 | issue.fixed_version = nil |
|
1489 | 1492 | issue.save |
|
1490 | 1493 | end |
|
1491 | 1494 | end |
|
1492 | 1495 | end |
|
1493 | 1496 | |
|
1494 | 1497 | # Callback on file attachment |
|
1495 | 1498 | def attachment_added(attachment) |
|
1496 | 1499 | if current_journal && !attachment.new_record? |
|
1497 | 1500 | current_journal.journalize_attachment(attachment, :added) |
|
1498 | 1501 | end |
|
1499 | 1502 | end |
|
1500 | 1503 | |
|
1501 | 1504 | # Callback on attachment deletion |
|
1502 | 1505 | def attachment_removed(attachment) |
|
1503 | 1506 | if current_journal && !attachment.new_record? |
|
1504 | 1507 | current_journal.journalize_attachment(attachment, :removed) |
|
1505 | 1508 | current_journal.save |
|
1506 | 1509 | end |
|
1507 | 1510 | end |
|
1508 | 1511 | |
|
1509 | 1512 | # Called after a relation is added |
|
1510 | 1513 | def relation_added(relation) |
|
1511 | 1514 | if current_journal |
|
1512 | 1515 | current_journal.journalize_relation(relation, :added) |
|
1513 | 1516 | current_journal.save |
|
1514 | 1517 | end |
|
1515 | 1518 | end |
|
1516 | 1519 | |
|
1517 | 1520 | # Called after a relation is removed |
|
1518 | 1521 | def relation_removed(relation) |
|
1519 | 1522 | if current_journal |
|
1520 | 1523 | current_journal.journalize_relation(relation, :removed) |
|
1521 | 1524 | current_journal.save |
|
1522 | 1525 | end |
|
1523 | 1526 | end |
|
1524 | 1527 | |
|
1525 | 1528 | # Default assignment based on category |
|
1526 | 1529 | def default_assign |
|
1527 | 1530 | if assigned_to.nil? && category && category.assigned_to |
|
1528 | 1531 | self.assigned_to = category.assigned_to |
|
1529 | 1532 | end |
|
1530 | 1533 | end |
|
1531 | 1534 | |
|
1532 | 1535 | # Updates start/due dates of following issues |
|
1533 | 1536 | def reschedule_following_issues |
|
1534 | 1537 | if start_date_changed? || due_date_changed? |
|
1535 | 1538 | relations_from.each do |relation| |
|
1536 | 1539 | relation.set_issue_to_dates |
|
1537 | 1540 | end |
|
1538 | 1541 | end |
|
1539 | 1542 | end |
|
1540 | 1543 | |
|
1541 | 1544 | # Closes duplicates if the issue is being closed |
|
1542 | 1545 | def close_duplicates |
|
1543 | 1546 | if closing? |
|
1544 | 1547 | duplicates.each do |duplicate| |
|
1545 | 1548 | # Reload is needed in case the duplicate was updated by a previous duplicate |
|
1546 | 1549 | duplicate.reload |
|
1547 | 1550 | # Don't re-close it if it's already closed |
|
1548 | 1551 | next if duplicate.closed? |
|
1549 | 1552 | # Same user and notes |
|
1550 | 1553 | if @current_journal |
|
1551 | 1554 | duplicate.init_journal(@current_journal.user, @current_journal.notes) |
|
1552 | 1555 | end |
|
1553 | 1556 | duplicate.update_attribute :status, self.status |
|
1554 | 1557 | end |
|
1555 | 1558 | end |
|
1556 | 1559 | end |
|
1557 | 1560 | |
|
1558 | 1561 | # Make sure updated_on is updated when adding a note and set updated_on now |
|
1559 | 1562 | # so we can set closed_on with the same value on closing |
|
1560 | 1563 | def force_updated_on_change |
|
1561 | 1564 | if @current_journal || changed? |
|
1562 | 1565 | self.updated_on = current_time_from_proper_timezone |
|
1563 | 1566 | if new_record? |
|
1564 | 1567 | self.created_on = updated_on |
|
1565 | 1568 | end |
|
1566 | 1569 | end |
|
1567 | 1570 | end |
|
1568 | 1571 | |
|
1569 | 1572 | # Callback for setting closed_on when the issue is closed. |
|
1570 | 1573 | # The closed_on attribute stores the time of the last closing |
|
1571 | 1574 | # and is preserved when the issue is reopened. |
|
1572 | 1575 | def update_closed_on |
|
1573 | 1576 | if closing? |
|
1574 | 1577 | self.closed_on = updated_on |
|
1575 | 1578 | end |
|
1576 | 1579 | end |
|
1577 | 1580 | |
|
1578 | 1581 | # Saves the changes in a Journal |
|
1579 | 1582 | # Called after_save |
|
1580 | 1583 | def create_journal |
|
1581 | 1584 | if current_journal |
|
1582 | 1585 | current_journal.save |
|
1583 | 1586 | end |
|
1584 | 1587 | end |
|
1585 | 1588 | |
|
1586 | 1589 | def send_notification |
|
1587 | 1590 | if Setting.notified_events.include?('issue_added') |
|
1588 | 1591 | Mailer.deliver_issue_add(self) |
|
1589 | 1592 | end |
|
1590 | 1593 | end |
|
1591 | 1594 | |
|
1592 | 1595 | # Stores the previous assignee so we can still have access |
|
1593 | 1596 | # to it during after_save callbacks (assigned_to_id_was is reset) |
|
1594 | 1597 | def set_assigned_to_was |
|
1595 | 1598 | @previous_assigned_to_id = assigned_to_id_was |
|
1596 | 1599 | end |
|
1597 | 1600 | |
|
1598 | 1601 | # Clears the previous assignee at the end of after_save callbacks |
|
1599 | 1602 | def clear_assigned_to_was |
|
1600 | 1603 | @assigned_to_was = nil |
|
1601 | 1604 | @previous_assigned_to_id = nil |
|
1602 | 1605 | end |
|
1603 | 1606 | end |
@@ -1,1077 +1,1076 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2014 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class Project < ActiveRecord::Base |
|
19 | 19 | include Redmine::SafeAttributes |
|
20 | 20 | |
|
21 | 21 | # Project statuses |
|
22 | 22 | STATUS_ACTIVE = 1 |
|
23 | 23 | STATUS_CLOSED = 5 |
|
24 | 24 | STATUS_ARCHIVED = 9 |
|
25 | 25 | |
|
26 | 26 | # Maximum length for project identifiers |
|
27 | 27 | IDENTIFIER_MAX_LENGTH = 100 |
|
28 | 28 | |
|
29 | 29 | # Specific overridden Activities |
|
30 | 30 | has_many :time_entry_activities |
|
31 | 31 | has_many :members, |
|
32 | 32 | lambda { joins(:principal, :roles). |
|
33 | 33 | where("#{Principal.table_name}.type='User' AND #{Principal.table_name}.status=#{Principal::STATUS_ACTIVE}") } |
|
34 | 34 | has_many :memberships, :class_name => 'Member' |
|
35 | 35 | has_many :member_principals, |
|
36 | 36 | lambda { joins(:principal). |
|
37 | 37 | where("#{Principal.table_name}.status=#{Principal::STATUS_ACTIVE}")}, |
|
38 | 38 | :class_name => 'Member' |
|
39 | 39 | has_many :enabled_modules, :dependent => :delete_all |
|
40 | 40 | has_and_belongs_to_many :trackers, lambda {order(:position)} |
|
41 | 41 | has_many :issues, :dependent => :destroy |
|
42 | 42 | has_many :issue_changes, :through => :issues, :source => :journals |
|
43 | 43 | has_many :versions, lambda {order("#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC")}, :dependent => :destroy |
|
44 | 44 | has_many :time_entries, :dependent => :destroy |
|
45 | 45 | has_many :queries, :class_name => 'IssueQuery', :dependent => :delete_all |
|
46 | 46 | has_many :documents, :dependent => :destroy |
|
47 | 47 | has_many :news, lambda {includes(:author)}, :dependent => :destroy |
|
48 | 48 | has_many :issue_categories, lambda {order("#{IssueCategory.table_name}.name")}, :dependent => :delete_all |
|
49 | 49 | has_many :boards, lambda {order("position ASC")}, :dependent => :destroy |
|
50 | 50 | has_one :repository, lambda {where(["is_default = ?", true])} |
|
51 | 51 | has_many :repositories, :dependent => :destroy |
|
52 | 52 | has_many :changesets, :through => :repository |
|
53 | 53 | has_one :wiki, :dependent => :destroy |
|
54 | 54 | # Custom field for the project issues |
|
55 | 55 | has_and_belongs_to_many :issue_custom_fields, |
|
56 | 56 | lambda {order("#{CustomField.table_name}.position")}, |
|
57 | 57 | :class_name => 'IssueCustomField', |
|
58 | 58 | :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}", |
|
59 | 59 | :association_foreign_key => 'custom_field_id' |
|
60 | 60 | |
|
61 | 61 | acts_as_nested_set :dependent => :destroy |
|
62 | 62 | acts_as_attachable :view_permission => :view_files, |
|
63 | 63 | :edit_permission => :manage_files, |
|
64 | 64 | :delete_permission => :manage_files |
|
65 | 65 | |
|
66 | 66 | acts_as_customizable |
|
67 | 67 | acts_as_searchable :columns => ['name', 'identifier', 'description'], :project_key => 'id', :permission => nil |
|
68 | 68 | acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"}, |
|
69 | 69 | :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o}}, |
|
70 | 70 | :author => nil |
|
71 | 71 | |
|
72 | 72 | attr_protected :status |
|
73 | 73 | |
|
74 | 74 | validates_presence_of :name, :identifier |
|
75 | validates_uniqueness_of :identifier | |
|
76 | validates_associated :repository, :wiki | |
|
75 | validates_uniqueness_of :identifier, :if => Proc.new {|p| p.identifier_changed?} | |
|
77 | 76 | validates_length_of :name, :maximum => 255 |
|
78 | 77 | validates_length_of :homepage, :maximum => 255 |
|
79 | 78 | validates_length_of :identifier, :in => 1..IDENTIFIER_MAX_LENGTH |
|
80 | 79 | # downcase letters, digits, dashes but not digits only |
|
81 | 80 | validates_format_of :identifier, :with => /\A(?!\d+$)[a-z0-9\-_]*\z/, :if => Proc.new { |p| p.identifier_changed? } |
|
82 | 81 | # reserved words |
|
83 | 82 | validates_exclusion_of :identifier, :in => %w( new ) |
|
84 | 83 | |
|
85 | 84 | after_save :update_position_under_parent, :if => Proc.new {|project| project.name_changed?} |
|
86 | 85 | after_save :update_inherited_members, :if => Proc.new {|project| project.inherit_members_changed?} |
|
87 | 86 | before_destroy :delete_all_members |
|
88 | 87 | |
|
89 | 88 | scope :has_module, lambda {|mod| |
|
90 | 89 | where("#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s) |
|
91 | 90 | } |
|
92 | 91 | scope :active, lambda { where(:status => STATUS_ACTIVE) } |
|
93 | 92 | scope :status, lambda {|arg| where(arg.blank? ? nil : {:status => arg.to_i}) } |
|
94 | 93 | scope :all_public, lambda { where(:is_public => true) } |
|
95 | 94 | scope :visible, lambda {|*args| where(Project.visible_condition(args.shift || User.current, *args)) } |
|
96 | 95 | scope :allowed_to, lambda {|*args| |
|
97 | 96 | user = User.current |
|
98 | 97 | permission = nil |
|
99 | 98 | if args.first.is_a?(Symbol) |
|
100 | 99 | permission = args.shift |
|
101 | 100 | else |
|
102 | 101 | user = args.shift |
|
103 | 102 | permission = args.shift |
|
104 | 103 | end |
|
105 | 104 | where(Project.allowed_to_condition(user, permission, *args)) |
|
106 | 105 | } |
|
107 | 106 | scope :like, lambda {|arg| |
|
108 | 107 | if arg.blank? |
|
109 | 108 | where(nil) |
|
110 | 109 | else |
|
111 | 110 | pattern = "%#{arg.to_s.strip.downcase}%" |
|
112 | 111 | where("LOWER(identifier) LIKE :p OR LOWER(name) LIKE :p", :p => pattern) |
|
113 | 112 | end |
|
114 | 113 | } |
|
115 | 114 | scope :sorted, lambda {order(:lft)} |
|
116 | 115 | |
|
117 | 116 | def initialize(attributes=nil, *args) |
|
118 | 117 | super |
|
119 | 118 | |
|
120 | 119 | initialized = (attributes || {}).stringify_keys |
|
121 | 120 | if !initialized.key?('identifier') && Setting.sequential_project_identifiers? |
|
122 | 121 | self.identifier = Project.next_identifier |
|
123 | 122 | end |
|
124 | 123 | if !initialized.key?('is_public') |
|
125 | 124 | self.is_public = Setting.default_projects_public? |
|
126 | 125 | end |
|
127 | 126 | if !initialized.key?('enabled_module_names') |
|
128 | 127 | self.enabled_module_names = Setting.default_projects_modules |
|
129 | 128 | end |
|
130 | 129 | if !initialized.key?('trackers') && !initialized.key?('tracker_ids') |
|
131 | 130 | default = Setting.default_projects_tracker_ids |
|
132 | 131 | if default.is_a?(Array) |
|
133 | 132 | self.trackers = Tracker.where(:id => default.map(&:to_i)).sorted.to_a |
|
134 | 133 | else |
|
135 | 134 | self.trackers = Tracker.sorted.to_a |
|
136 | 135 | end |
|
137 | 136 | end |
|
138 | 137 | end |
|
139 | 138 | |
|
140 | 139 | def identifier=(identifier) |
|
141 | 140 | super unless identifier_frozen? |
|
142 | 141 | end |
|
143 | 142 | |
|
144 | 143 | def identifier_frozen? |
|
145 | 144 | errors[:identifier].blank? && !(new_record? || identifier.blank?) |
|
146 | 145 | end |
|
147 | 146 | |
|
148 | 147 | # returns latest created projects |
|
149 | 148 | # non public projects will be returned only if user is a member of those |
|
150 | 149 | def self.latest(user=nil, count=5) |
|
151 | 150 | visible(user).limit(count).order("created_on DESC").to_a |
|
152 | 151 | end |
|
153 | 152 | |
|
154 | 153 | # Returns true if the project is visible to +user+ or to the current user. |
|
155 | 154 | def visible?(user=User.current) |
|
156 | 155 | user.allowed_to?(:view_project, self) |
|
157 | 156 | end |
|
158 | 157 | |
|
159 | 158 | # Returns a SQL conditions string used to find all projects visible by the specified user. |
|
160 | 159 | # |
|
161 | 160 | # Examples: |
|
162 | 161 | # Project.visible_condition(admin) => "projects.status = 1" |
|
163 | 162 | # Project.visible_condition(normal_user) => "((projects.status = 1) AND (projects.is_public = 1 OR projects.id IN (1,3,4)))" |
|
164 | 163 | # Project.visible_condition(anonymous) => "((projects.status = 1) AND (projects.is_public = 1))" |
|
165 | 164 | def self.visible_condition(user, options={}) |
|
166 | 165 | allowed_to_condition(user, :view_project, options) |
|
167 | 166 | end |
|
168 | 167 | |
|
169 | 168 | # Returns a SQL conditions string used to find all projects for which +user+ has the given +permission+ |
|
170 | 169 | # |
|
171 | 170 | # Valid options: |
|
172 | 171 | # * :project => limit the condition to project |
|
173 | 172 | # * :with_subprojects => limit the condition to project and its subprojects |
|
174 | 173 | # * :member => limit the condition to the user projects |
|
175 | 174 | def self.allowed_to_condition(user, permission, options={}) |
|
176 | 175 | perm = Redmine::AccessControl.permission(permission) |
|
177 | 176 | base_statement = (perm && perm.read? ? "#{Project.table_name}.status <> #{Project::STATUS_ARCHIVED}" : "#{Project.table_name}.status = #{Project::STATUS_ACTIVE}") |
|
178 | 177 | if perm && perm.project_module |
|
179 | 178 | # If the permission belongs to a project module, make sure the module is enabled |
|
180 | 179 | base_statement << " AND #{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}')" |
|
181 | 180 | end |
|
182 | 181 | if project = options[:project] |
|
183 | 182 | project_statement = project.project_condition(options[:with_subprojects]) |
|
184 | 183 | base_statement = "(#{project_statement}) AND (#{base_statement})" |
|
185 | 184 | end |
|
186 | 185 | |
|
187 | 186 | if user.admin? |
|
188 | 187 | base_statement |
|
189 | 188 | else |
|
190 | 189 | statement_by_role = {} |
|
191 | 190 | unless options[:member] |
|
192 | 191 | role = user.builtin_role |
|
193 | 192 | if role.allowed_to?(permission) |
|
194 | 193 | statement_by_role[role] = "#{Project.table_name}.is_public = #{connection.quoted_true}" |
|
195 | 194 | end |
|
196 | 195 | end |
|
197 | 196 | user.projects_by_role.each do |role, projects| |
|
198 | 197 | if role.allowed_to?(permission) && projects.any? |
|
199 | 198 | statement_by_role[role] = "#{Project.table_name}.id IN (#{projects.collect(&:id).join(',')})" |
|
200 | 199 | end |
|
201 | 200 | end |
|
202 | 201 | if statement_by_role.empty? |
|
203 | 202 | "1=0" |
|
204 | 203 | else |
|
205 | 204 | if block_given? |
|
206 | 205 | statement_by_role.each do |role, statement| |
|
207 | 206 | if s = yield(role, user) |
|
208 | 207 | statement_by_role[role] = "(#{statement} AND (#{s}))" |
|
209 | 208 | end |
|
210 | 209 | end |
|
211 | 210 | end |
|
212 | 211 | "((#{base_statement}) AND (#{statement_by_role.values.join(' OR ')}))" |
|
213 | 212 | end |
|
214 | 213 | end |
|
215 | 214 | end |
|
216 | 215 | |
|
217 | 216 | def override_roles(role) |
|
218 | 217 | group_class = role.anonymous? ? GroupAnonymous : GroupNonMember |
|
219 | 218 | member = member_principals.where("#{Principal.table_name}.type = ?", group_class.name).first |
|
220 | 219 | member ? member.roles.to_a : [role] |
|
221 | 220 | end |
|
222 | 221 | |
|
223 | 222 | def principals |
|
224 | 223 | @principals ||= Principal.active.joins(:members).where("#{Member.table_name}.project_id = ?", id).uniq |
|
225 | 224 | end |
|
226 | 225 | |
|
227 | 226 | def users |
|
228 | 227 | @users ||= User.active.joins(:members).where("#{Member.table_name}.project_id = ?", id).uniq |
|
229 | 228 | end |
|
230 | 229 | |
|
231 | 230 | # Returns the Systemwide and project specific activities |
|
232 | 231 | def activities(include_inactive=false) |
|
233 | 232 | if include_inactive |
|
234 | 233 | return all_activities |
|
235 | 234 | else |
|
236 | 235 | return active_activities |
|
237 | 236 | end |
|
238 | 237 | end |
|
239 | 238 | |
|
240 | 239 | # Will create a new Project specific Activity or update an existing one |
|
241 | 240 | # |
|
242 | 241 | # This will raise a ActiveRecord::Rollback if the TimeEntryActivity |
|
243 | 242 | # does not successfully save. |
|
244 | 243 | def update_or_create_time_entry_activity(id, activity_hash) |
|
245 | 244 | if activity_hash.respond_to?(:has_key?) && activity_hash.has_key?('parent_id') |
|
246 | 245 | self.create_time_entry_activity_if_needed(activity_hash) |
|
247 | 246 | else |
|
248 | 247 | activity = project.time_entry_activities.find_by_id(id.to_i) |
|
249 | 248 | activity.update_attributes(activity_hash) if activity |
|
250 | 249 | end |
|
251 | 250 | end |
|
252 | 251 | |
|
253 | 252 | # Create a new TimeEntryActivity if it overrides a system TimeEntryActivity |
|
254 | 253 | # |
|
255 | 254 | # This will raise a ActiveRecord::Rollback if the TimeEntryActivity |
|
256 | 255 | # does not successfully save. |
|
257 | 256 | def create_time_entry_activity_if_needed(activity) |
|
258 | 257 | if activity['parent_id'] |
|
259 | 258 | parent_activity = TimeEntryActivity.find(activity['parent_id']) |
|
260 | 259 | activity['name'] = parent_activity.name |
|
261 | 260 | activity['position'] = parent_activity.position |
|
262 | 261 | if Enumeration.overriding_change?(activity, parent_activity) |
|
263 | 262 | project_activity = self.time_entry_activities.create(activity) |
|
264 | 263 | if project_activity.new_record? |
|
265 | 264 | raise ActiveRecord::Rollback, "Overriding TimeEntryActivity was not successfully saved" |
|
266 | 265 | else |
|
267 | 266 | self.time_entries. |
|
268 | 267 | where(["activity_id = ?", parent_activity.id]). |
|
269 | 268 | update_all("activity_id = #{project_activity.id}") |
|
270 | 269 | end |
|
271 | 270 | end |
|
272 | 271 | end |
|
273 | 272 | end |
|
274 | 273 | |
|
275 | 274 | # Returns a :conditions SQL string that can be used to find the issues associated with this project. |
|
276 | 275 | # |
|
277 | 276 | # Examples: |
|
278 | 277 | # project.project_condition(true) => "(projects.id = 1 OR (projects.lft > 1 AND projects.rgt < 10))" |
|
279 | 278 | # project.project_condition(false) => "projects.id = 1" |
|
280 | 279 | def project_condition(with_subprojects) |
|
281 | 280 | cond = "#{Project.table_name}.id = #{id}" |
|
282 | 281 | cond = "(#{cond} OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt}))" if with_subprojects |
|
283 | 282 | cond |
|
284 | 283 | end |
|
285 | 284 | |
|
286 | 285 | def self.find(*args) |
|
287 | 286 | if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/) |
|
288 | 287 | project = find_by_identifier(*args) |
|
289 | 288 | raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil? |
|
290 | 289 | project |
|
291 | 290 | else |
|
292 | 291 | super |
|
293 | 292 | end |
|
294 | 293 | end |
|
295 | 294 | |
|
296 | 295 | def self.find_by_param(*args) |
|
297 | 296 | self.find(*args) |
|
298 | 297 | end |
|
299 | 298 | |
|
300 | 299 | alias :base_reload :reload |
|
301 | 300 | def reload(*args) |
|
302 | 301 | @principals = nil |
|
303 | 302 | @users = nil |
|
304 | 303 | @shared_versions = nil |
|
305 | 304 | @rolled_up_versions = nil |
|
306 | 305 | @rolled_up_trackers = nil |
|
307 | 306 | @all_issue_custom_fields = nil |
|
308 | 307 | @all_time_entry_custom_fields = nil |
|
309 | 308 | @to_param = nil |
|
310 | 309 | @allowed_parents = nil |
|
311 | 310 | @allowed_permissions = nil |
|
312 | 311 | @actions_allowed = nil |
|
313 | 312 | @start_date = nil |
|
314 | 313 | @due_date = nil |
|
315 | 314 | @override_members = nil |
|
316 | 315 | @assignable_users = nil |
|
317 | 316 | base_reload(*args) |
|
318 | 317 | end |
|
319 | 318 | |
|
320 | 319 | def to_param |
|
321 | 320 | # id is used for projects with a numeric identifier (compatibility) |
|
322 | 321 | @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id.to_s : identifier) |
|
323 | 322 | end |
|
324 | 323 | |
|
325 | 324 | def active? |
|
326 | 325 | self.status == STATUS_ACTIVE |
|
327 | 326 | end |
|
328 | 327 | |
|
329 | 328 | def archived? |
|
330 | 329 | self.status == STATUS_ARCHIVED |
|
331 | 330 | end |
|
332 | 331 | |
|
333 | 332 | # Archives the project and its descendants |
|
334 | 333 | def archive |
|
335 | 334 | # Check that there is no issue of a non descendant project that is assigned |
|
336 | 335 | # to one of the project or descendant versions |
|
337 | 336 | version_ids = self_and_descendants.joins(:versions).pluck("#{Version.table_name}.id") |
|
338 | 337 | |
|
339 | 338 | if version_ids.any? && |
|
340 | 339 | Issue. |
|
341 | 340 | includes(:project). |
|
342 | 341 | where("#{Project.table_name}.lft < ? OR #{Project.table_name}.rgt > ?", lft, rgt). |
|
343 | 342 | where(:fixed_version_id => version_ids). |
|
344 | 343 | exists? |
|
345 | 344 | return false |
|
346 | 345 | end |
|
347 | 346 | Project.transaction do |
|
348 | 347 | archive! |
|
349 | 348 | end |
|
350 | 349 | true |
|
351 | 350 | end |
|
352 | 351 | |
|
353 | 352 | # Unarchives the project |
|
354 | 353 | # All its ancestors must be active |
|
355 | 354 | def unarchive |
|
356 | 355 | return false if ancestors.detect {|a| !a.active?} |
|
357 | 356 | update_attribute :status, STATUS_ACTIVE |
|
358 | 357 | end |
|
359 | 358 | |
|
360 | 359 | def close |
|
361 | 360 | self_and_descendants.status(STATUS_ACTIVE).update_all :status => STATUS_CLOSED |
|
362 | 361 | end |
|
363 | 362 | |
|
364 | 363 | def reopen |
|
365 | 364 | self_and_descendants.status(STATUS_CLOSED).update_all :status => STATUS_ACTIVE |
|
366 | 365 | end |
|
367 | 366 | |
|
368 | 367 | # Returns an array of projects the project can be moved to |
|
369 | 368 | # by the current user |
|
370 | 369 | def allowed_parents |
|
371 | 370 | return @allowed_parents if @allowed_parents |
|
372 | 371 | @allowed_parents = Project.allowed_to(User.current, :add_subprojects).to_a |
|
373 | 372 | @allowed_parents = @allowed_parents - self_and_descendants |
|
374 | 373 | if User.current.allowed_to?(:add_project, nil, :global => true) || (!new_record? && parent.nil?) |
|
375 | 374 | @allowed_parents << nil |
|
376 | 375 | end |
|
377 | 376 | unless parent.nil? || @allowed_parents.empty? || @allowed_parents.include?(parent) |
|
378 | 377 | @allowed_parents << parent |
|
379 | 378 | end |
|
380 | 379 | @allowed_parents |
|
381 | 380 | end |
|
382 | 381 | |
|
383 | 382 | # Sets the parent of the project with authorization check |
|
384 | 383 | def set_allowed_parent!(p) |
|
385 | 384 | unless p.nil? || p.is_a?(Project) |
|
386 | 385 | if p.to_s.blank? |
|
387 | 386 | p = nil |
|
388 | 387 | else |
|
389 | 388 | p = Project.find_by_id(p) |
|
390 | 389 | return false unless p |
|
391 | 390 | end |
|
392 | 391 | end |
|
393 | 392 | if p.nil? |
|
394 | 393 | if !new_record? && allowed_parents.empty? |
|
395 | 394 | return false |
|
396 | 395 | end |
|
397 | 396 | elsif !allowed_parents.include?(p) |
|
398 | 397 | return false |
|
399 | 398 | end |
|
400 | 399 | set_parent!(p) |
|
401 | 400 | end |
|
402 | 401 | |
|
403 | 402 | # Sets the parent of the project |
|
404 | 403 | # Argument can be either a Project, a String, a Fixnum or nil |
|
405 | 404 | def set_parent!(p) |
|
406 | 405 | unless p.nil? || p.is_a?(Project) |
|
407 | 406 | if p.to_s.blank? |
|
408 | 407 | p = nil |
|
409 | 408 | else |
|
410 | 409 | p = Project.find_by_id(p) |
|
411 | 410 | return false unless p |
|
412 | 411 | end |
|
413 | 412 | end |
|
414 | 413 | if p == parent && !p.nil? |
|
415 | 414 | # Nothing to do |
|
416 | 415 | true |
|
417 | 416 | elsif p.nil? || (p.active? && move_possible?(p)) |
|
418 | 417 | set_or_update_position_under(p) |
|
419 | 418 | Issue.update_versions_from_hierarchy_change(self) |
|
420 | 419 | true |
|
421 | 420 | else |
|
422 | 421 | # Can not move to the given target |
|
423 | 422 | false |
|
424 | 423 | end |
|
425 | 424 | end |
|
426 | 425 | |
|
427 | 426 | # Recalculates all lft and rgt values based on project names |
|
428 | 427 | # Unlike Project.rebuild!, these values are recalculated even if the tree "looks" valid |
|
429 | 428 | # Used in BuildProjectsTree migration |
|
430 | 429 | def self.rebuild_tree! |
|
431 | 430 | transaction do |
|
432 | 431 | update_all "lft = NULL, rgt = NULL" |
|
433 | 432 | rebuild!(false) |
|
434 | 433 | all.each { |p| p.set_or_update_position_under(p.parent) } |
|
435 | 434 | end |
|
436 | 435 | end |
|
437 | 436 | |
|
438 | 437 | # Returns an array of the trackers used by the project and its active sub projects |
|
439 | 438 | def rolled_up_trackers |
|
440 | 439 | @rolled_up_trackers ||= |
|
441 | 440 | Tracker. |
|
442 | 441 | joins(:projects). |
|
443 | 442 | joins("JOIN #{EnabledModule.table_name} ON #{EnabledModule.table_name}.project_id = #{Project.table_name}.id AND #{EnabledModule.table_name}.name = 'issue_tracking'"). |
|
444 | 443 | where("#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status <> ?", lft, rgt, STATUS_ARCHIVED). |
|
445 | 444 | uniq. |
|
446 | 445 | sorted. |
|
447 | 446 | to_a |
|
448 | 447 | end |
|
449 | 448 | |
|
450 | 449 | # Closes open and locked project versions that are completed |
|
451 | 450 | def close_completed_versions |
|
452 | 451 | Version.transaction do |
|
453 | 452 | versions.where(:status => %w(open locked)).each do |version| |
|
454 | 453 | if version.completed? |
|
455 | 454 | version.update_attribute(:status, 'closed') |
|
456 | 455 | end |
|
457 | 456 | end |
|
458 | 457 | end |
|
459 | 458 | end |
|
460 | 459 | |
|
461 | 460 | # Returns a scope of the Versions on subprojects |
|
462 | 461 | def rolled_up_versions |
|
463 | 462 | @rolled_up_versions ||= |
|
464 | 463 | Version. |
|
465 | 464 | joins(:project). |
|
466 | 465 | where("#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status <> ?", lft, rgt, STATUS_ARCHIVED) |
|
467 | 466 | end |
|
468 | 467 | |
|
469 | 468 | # Returns a scope of the Versions used by the project |
|
470 | 469 | def shared_versions |
|
471 | 470 | if new_record? |
|
472 | 471 | Version. |
|
473 | 472 | joins(:project). |
|
474 | 473 | preload(:project). |
|
475 | 474 | where("#{Project.table_name}.status <> ? AND #{Version.table_name}.sharing = 'system'", STATUS_ARCHIVED) |
|
476 | 475 | else |
|
477 | 476 | @shared_versions ||= begin |
|
478 | 477 | r = root? ? self : root |
|
479 | 478 | Version. |
|
480 | 479 | joins(:project). |
|
481 | 480 | preload(:project). |
|
482 | 481 | where("#{Project.table_name}.id = #{id}" + |
|
483 | 482 | " OR (#{Project.table_name}.status <> #{Project::STATUS_ARCHIVED} AND (" + |
|
484 | 483 | " #{Version.table_name}.sharing = 'system'" + |
|
485 | 484 | " OR (#{Project.table_name}.lft >= #{r.lft} AND #{Project.table_name}.rgt <= #{r.rgt} AND #{Version.table_name}.sharing = 'tree')" + |
|
486 | 485 | " OR (#{Project.table_name}.lft < #{lft} AND #{Project.table_name}.rgt > #{rgt} AND #{Version.table_name}.sharing IN ('hierarchy', 'descendants'))" + |
|
487 | 486 | " OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt} AND #{Version.table_name}.sharing = 'hierarchy')" + |
|
488 | 487 | "))") |
|
489 | 488 | end |
|
490 | 489 | end |
|
491 | 490 | end |
|
492 | 491 | |
|
493 | 492 | # Returns a hash of project users grouped by role |
|
494 | 493 | def users_by_role |
|
495 | 494 | members.includes(:user, :roles).inject({}) do |h, m| |
|
496 | 495 | m.roles.each do |r| |
|
497 | 496 | h[r] ||= [] |
|
498 | 497 | h[r] << m.user |
|
499 | 498 | end |
|
500 | 499 | h |
|
501 | 500 | end |
|
502 | 501 | end |
|
503 | 502 | |
|
504 | 503 | # Adds user as a project member with the default role |
|
505 | 504 | # Used for when a non-admin user creates a project |
|
506 | 505 | def add_default_member(user) |
|
507 | 506 | role = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first |
|
508 | 507 | member = Member.new(:project => self, :principal => user, :roles => [role]) |
|
509 | 508 | self.members << member |
|
510 | 509 | member |
|
511 | 510 | end |
|
512 | 511 | |
|
513 | 512 | # Deletes all project's members |
|
514 | 513 | def delete_all_members |
|
515 | 514 | me, mr = Member.table_name, MemberRole.table_name |
|
516 | 515 | self.class.connection.delete("DELETE FROM #{mr} WHERE #{mr}.member_id IN (SELECT #{me}.id FROM #{me} WHERE #{me}.project_id = #{id})") |
|
517 | 516 | Member.delete_all(['project_id = ?', id]) |
|
518 | 517 | end |
|
519 | 518 | |
|
520 | 519 | # Return a Principal scope of users/groups issues can be assigned to |
|
521 | 520 | def assignable_users |
|
522 | 521 | types = ['User'] |
|
523 | 522 | types << 'Group' if Setting.issue_group_assignment? |
|
524 | 523 | |
|
525 | 524 | @assignable_users ||= Principal. |
|
526 | 525 | active. |
|
527 | 526 | joins(:members => :roles). |
|
528 | 527 | where(:type => types, :members => {:project_id => id}, :roles => {:assignable => true}). |
|
529 | 528 | uniq. |
|
530 | 529 | sorted |
|
531 | 530 | end |
|
532 | 531 | |
|
533 | 532 | # Returns the mail addresses of users that should be always notified on project events |
|
534 | 533 | def recipients |
|
535 | 534 | notified_users.collect {|user| user.mail} |
|
536 | 535 | end |
|
537 | 536 | |
|
538 | 537 | # Returns the users that should be notified on project events |
|
539 | 538 | def notified_users |
|
540 | 539 | # TODO: User part should be extracted to User#notify_about? |
|
541 | 540 | members.select {|m| m.principal.present? && (m.mail_notification? || m.principal.mail_notification == 'all')}.collect {|m| m.principal} |
|
542 | 541 | end |
|
543 | 542 | |
|
544 | 543 | # Returns a scope of all custom fields enabled for project issues |
|
545 | 544 | # (explicitly associated custom fields and custom fields enabled for all projects) |
|
546 | 545 | def all_issue_custom_fields |
|
547 | 546 | @all_issue_custom_fields ||= IssueCustomField. |
|
548 | 547 | sorted. |
|
549 | 548 | where("is_for_all = ? OR id IN (SELECT DISTINCT cfp.custom_field_id" + |
|
550 | 549 | " FROM #{table_name_prefix}custom_fields_projects#{table_name_suffix} cfp" + |
|
551 | 550 | " WHERE cfp.project_id = ?)", true, id) |
|
552 | 551 | end |
|
553 | 552 | |
|
554 | 553 | def project |
|
555 | 554 | self |
|
556 | 555 | end |
|
557 | 556 | |
|
558 | 557 | def <=>(project) |
|
559 | 558 | name.downcase <=> project.name.downcase |
|
560 | 559 | end |
|
561 | 560 | |
|
562 | 561 | def to_s |
|
563 | 562 | name |
|
564 | 563 | end |
|
565 | 564 | |
|
566 | 565 | # Returns a short description of the projects (first lines) |
|
567 | 566 | def short_description(length = 255) |
|
568 | 567 | description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description |
|
569 | 568 | end |
|
570 | 569 | |
|
571 | 570 | def css_classes |
|
572 | 571 | s = 'project' |
|
573 | 572 | s << ' root' if root? |
|
574 | 573 | s << ' child' if child? |
|
575 | 574 | s << (leaf? ? ' leaf' : ' parent') |
|
576 | 575 | unless active? |
|
577 | 576 | if archived? |
|
578 | 577 | s << ' archived' |
|
579 | 578 | else |
|
580 | 579 | s << ' closed' |
|
581 | 580 | end |
|
582 | 581 | end |
|
583 | 582 | s |
|
584 | 583 | end |
|
585 | 584 | |
|
586 | 585 | # The earliest start date of a project, based on it's issues and versions |
|
587 | 586 | def start_date |
|
588 | 587 | @start_date ||= [ |
|
589 | 588 | issues.minimum('start_date'), |
|
590 | 589 | shared_versions.minimum('effective_date'), |
|
591 | 590 | Issue.fixed_version(shared_versions).minimum('start_date') |
|
592 | 591 | ].compact.min |
|
593 | 592 | end |
|
594 | 593 | |
|
595 | 594 | # The latest due date of an issue or version |
|
596 | 595 | def due_date |
|
597 | 596 | @due_date ||= [ |
|
598 | 597 | issues.maximum('due_date'), |
|
599 | 598 | shared_versions.maximum('effective_date'), |
|
600 | 599 | Issue.fixed_version(shared_versions).maximum('due_date') |
|
601 | 600 | ].compact.max |
|
602 | 601 | end |
|
603 | 602 | |
|
604 | 603 | def overdue? |
|
605 | 604 | active? && !due_date.nil? && (due_date < Date.today) |
|
606 | 605 | end |
|
607 | 606 | |
|
608 | 607 | # Returns the percent completed for this project, based on the |
|
609 | 608 | # progress on it's versions. |
|
610 | 609 | def completed_percent(options={:include_subprojects => false}) |
|
611 | 610 | if options.delete(:include_subprojects) |
|
612 | 611 | total = self_and_descendants.collect(&:completed_percent).sum |
|
613 | 612 | |
|
614 | 613 | total / self_and_descendants.count |
|
615 | 614 | else |
|
616 | 615 | if versions.count > 0 |
|
617 | 616 | total = versions.collect(&:completed_percent).sum |
|
618 | 617 | |
|
619 | 618 | total / versions.count |
|
620 | 619 | else |
|
621 | 620 | 100 |
|
622 | 621 | end |
|
623 | 622 | end |
|
624 | 623 | end |
|
625 | 624 | |
|
626 | 625 | # Return true if this project allows to do the specified action. |
|
627 | 626 | # action can be: |
|
628 | 627 | # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit') |
|
629 | 628 | # * a permission Symbol (eg. :edit_project) |
|
630 | 629 | def allows_to?(action) |
|
631 | 630 | if archived? |
|
632 | 631 | # No action allowed on archived projects |
|
633 | 632 | return false |
|
634 | 633 | end |
|
635 | 634 | unless active? || Redmine::AccessControl.read_action?(action) |
|
636 | 635 | # No write action allowed on closed projects |
|
637 | 636 | return false |
|
638 | 637 | end |
|
639 | 638 | # No action allowed on disabled modules |
|
640 | 639 | if action.is_a? Hash |
|
641 | 640 | allowed_actions.include? "#{action[:controller]}/#{action[:action]}" |
|
642 | 641 | else |
|
643 | 642 | allowed_permissions.include? action |
|
644 | 643 | end |
|
645 | 644 | end |
|
646 | 645 | |
|
647 | 646 | # Return the enabled module with the given name |
|
648 | 647 | # or nil if the module is not enabled for the project |
|
649 | 648 | def enabled_module(name) |
|
650 | 649 | name = name.to_s |
|
651 | 650 | enabled_modules.detect {|m| m.name == name} |
|
652 | 651 | end |
|
653 | 652 | |
|
654 | 653 | # Return true if the module with the given name is enabled |
|
655 | 654 | def module_enabled?(name) |
|
656 | 655 | enabled_module(name).present? |
|
657 | 656 | end |
|
658 | 657 | |
|
659 | 658 | def enabled_module_names=(module_names) |
|
660 | 659 | if module_names && module_names.is_a?(Array) |
|
661 | 660 | module_names = module_names.collect(&:to_s).reject(&:blank?) |
|
662 | 661 | self.enabled_modules = module_names.collect {|name| enabled_modules.detect {|mod| mod.name == name} || EnabledModule.new(:name => name)} |
|
663 | 662 | else |
|
664 | 663 | enabled_modules.clear |
|
665 | 664 | end |
|
666 | 665 | end |
|
667 | 666 | |
|
668 | 667 | # Returns an array of the enabled modules names |
|
669 | 668 | def enabled_module_names |
|
670 | 669 | enabled_modules.collect(&:name) |
|
671 | 670 | end |
|
672 | 671 | |
|
673 | 672 | # Enable a specific module |
|
674 | 673 | # |
|
675 | 674 | # Examples: |
|
676 | 675 | # project.enable_module!(:issue_tracking) |
|
677 | 676 | # project.enable_module!("issue_tracking") |
|
678 | 677 | def enable_module!(name) |
|
679 | 678 | enabled_modules << EnabledModule.new(:name => name.to_s) unless module_enabled?(name) |
|
680 | 679 | end |
|
681 | 680 | |
|
682 | 681 | # Disable a module if it exists |
|
683 | 682 | # |
|
684 | 683 | # Examples: |
|
685 | 684 | # project.disable_module!(:issue_tracking) |
|
686 | 685 | # project.disable_module!("issue_tracking") |
|
687 | 686 | # project.disable_module!(project.enabled_modules.first) |
|
688 | 687 | def disable_module!(target) |
|
689 | 688 | target = enabled_modules.detect{|mod| target.to_s == mod.name} unless enabled_modules.include?(target) |
|
690 | 689 | target.destroy unless target.blank? |
|
691 | 690 | end |
|
692 | 691 | |
|
693 | 692 | safe_attributes 'name', |
|
694 | 693 | 'description', |
|
695 | 694 | 'homepage', |
|
696 | 695 | 'is_public', |
|
697 | 696 | 'identifier', |
|
698 | 697 | 'custom_field_values', |
|
699 | 698 | 'custom_fields', |
|
700 | 699 | 'tracker_ids', |
|
701 | 700 | 'issue_custom_field_ids' |
|
702 | 701 | |
|
703 | 702 | safe_attributes 'enabled_module_names', |
|
704 | 703 | :if => lambda {|project, user| project.new_record? || user.allowed_to?(:select_project_modules, project) } |
|
705 | 704 | |
|
706 | 705 | safe_attributes 'inherit_members', |
|
707 | 706 | :if => lambda {|project, user| project.parent.nil? || project.parent.visible?(user)} |
|
708 | 707 | |
|
709 | 708 | # Returns an array of projects that are in this project's hierarchy |
|
710 | 709 | # |
|
711 | 710 | # Example: parents, children, siblings |
|
712 | 711 | def hierarchy |
|
713 | 712 | parents = project.self_and_ancestors || [] |
|
714 | 713 | descendants = project.descendants || [] |
|
715 | 714 | project_hierarchy = parents | descendants # Set union |
|
716 | 715 | end |
|
717 | 716 | |
|
718 | 717 | # Returns an auto-generated project identifier based on the last identifier used |
|
719 | 718 | def self.next_identifier |
|
720 | 719 | p = Project.order('id DESC').first |
|
721 | 720 | p.nil? ? nil : p.identifier.to_s.succ |
|
722 | 721 | end |
|
723 | 722 | |
|
724 | 723 | # Copies and saves the Project instance based on the +project+. |
|
725 | 724 | # Duplicates the source project's: |
|
726 | 725 | # * Wiki |
|
727 | 726 | # * Versions |
|
728 | 727 | # * Categories |
|
729 | 728 | # * Issues |
|
730 | 729 | # * Members |
|
731 | 730 | # * Queries |
|
732 | 731 | # |
|
733 | 732 | # Accepts an +options+ argument to specify what to copy |
|
734 | 733 | # |
|
735 | 734 | # Examples: |
|
736 | 735 | # project.copy(1) # => copies everything |
|
737 | 736 | # project.copy(1, :only => 'members') # => copies members only |
|
738 | 737 | # project.copy(1, :only => ['members', 'versions']) # => copies members and versions |
|
739 | 738 | def copy(project, options={}) |
|
740 | 739 | project = project.is_a?(Project) ? project : Project.find(project) |
|
741 | 740 | |
|
742 | 741 | to_be_copied = %w(wiki versions issue_categories issues members queries boards) |
|
743 | 742 | to_be_copied = to_be_copied & Array.wrap(options[:only]) unless options[:only].nil? |
|
744 | 743 | |
|
745 | 744 | Project.transaction do |
|
746 | 745 | if save |
|
747 | 746 | reload |
|
748 | 747 | to_be_copied.each do |name| |
|
749 | 748 | send "copy_#{name}", project |
|
750 | 749 | end |
|
751 | 750 | Redmine::Hook.call_hook(:model_project_copy_before_save, :source_project => project, :destination_project => self) |
|
752 | 751 | save |
|
753 | 752 | end |
|
754 | 753 | end |
|
755 | 754 | true |
|
756 | 755 | end |
|
757 | 756 | |
|
758 | 757 | # Returns a new unsaved Project instance with attributes copied from +project+ |
|
759 | 758 | def self.copy_from(project) |
|
760 | 759 | project = project.is_a?(Project) ? project : Project.find(project) |
|
761 | 760 | # clear unique attributes |
|
762 | 761 | attributes = project.attributes.dup.except('id', 'name', 'identifier', 'status', 'parent_id', 'lft', 'rgt') |
|
763 | 762 | copy = Project.new(attributes) |
|
764 | 763 | copy.enabled_modules = project.enabled_modules |
|
765 | 764 | copy.trackers = project.trackers |
|
766 | 765 | copy.custom_values = project.custom_values.collect {|v| v.clone} |
|
767 | 766 | copy.issue_custom_fields = project.issue_custom_fields |
|
768 | 767 | copy |
|
769 | 768 | end |
|
770 | 769 | |
|
771 | 770 | # Yields the given block for each project with its level in the tree |
|
772 | 771 | def self.project_tree(projects, &block) |
|
773 | 772 | ancestors = [] |
|
774 | 773 | projects.sort_by(&:lft).each do |project| |
|
775 | 774 | while (ancestors.any? && !project.is_descendant_of?(ancestors.last)) |
|
776 | 775 | ancestors.pop |
|
777 | 776 | end |
|
778 | 777 | yield project, ancestors.size |
|
779 | 778 | ancestors << project |
|
780 | 779 | end |
|
781 | 780 | end |
|
782 | 781 | |
|
783 | 782 | private |
|
784 | 783 | |
|
785 | 784 | def after_parent_changed(parent_was) |
|
786 | 785 | remove_inherited_member_roles |
|
787 | 786 | add_inherited_member_roles |
|
788 | 787 | end |
|
789 | 788 | |
|
790 | 789 | def update_inherited_members |
|
791 | 790 | if parent |
|
792 | 791 | if inherit_members? && !inherit_members_was |
|
793 | 792 | remove_inherited_member_roles |
|
794 | 793 | add_inherited_member_roles |
|
795 | 794 | elsif !inherit_members? && inherit_members_was |
|
796 | 795 | remove_inherited_member_roles |
|
797 | 796 | end |
|
798 | 797 | end |
|
799 | 798 | end |
|
800 | 799 | |
|
801 | 800 | def remove_inherited_member_roles |
|
802 | 801 | member_roles = memberships.map(&:member_roles).flatten |
|
803 | 802 | member_role_ids = member_roles.map(&:id) |
|
804 | 803 | member_roles.each do |member_role| |
|
805 | 804 | if member_role.inherited_from && !member_role_ids.include?(member_role.inherited_from) |
|
806 | 805 | member_role.destroy |
|
807 | 806 | end |
|
808 | 807 | end |
|
809 | 808 | end |
|
810 | 809 | |
|
811 | 810 | def add_inherited_member_roles |
|
812 | 811 | if inherit_members? && parent |
|
813 | 812 | parent.memberships.each do |parent_member| |
|
814 | 813 | member = Member.find_or_new(self.id, parent_member.user_id) |
|
815 | 814 | parent_member.member_roles.each do |parent_member_role| |
|
816 | 815 | member.member_roles << MemberRole.new(:role => parent_member_role.role, :inherited_from => parent_member_role.id) |
|
817 | 816 | end |
|
818 | 817 | member.save! |
|
819 | 818 | end |
|
820 | 819 | end |
|
821 | 820 | end |
|
822 | 821 | |
|
823 | 822 | # Copies wiki from +project+ |
|
824 | 823 | def copy_wiki(project) |
|
825 | 824 | # Check that the source project has a wiki first |
|
826 | 825 | unless project.wiki.nil? |
|
827 | 826 | wiki = self.wiki || Wiki.new |
|
828 | 827 | wiki.attributes = project.wiki.attributes.dup.except("id", "project_id") |
|
829 | 828 | wiki_pages_map = {} |
|
830 | 829 | project.wiki.pages.each do |page| |
|
831 | 830 | # Skip pages without content |
|
832 | 831 | next if page.content.nil? |
|
833 | 832 | new_wiki_content = WikiContent.new(page.content.attributes.dup.except("id", "page_id", "updated_on")) |
|
834 | 833 | new_wiki_page = WikiPage.new(page.attributes.dup.except("id", "wiki_id", "created_on", "parent_id")) |
|
835 | 834 | new_wiki_page.content = new_wiki_content |
|
836 | 835 | wiki.pages << new_wiki_page |
|
837 | 836 | wiki_pages_map[page.id] = new_wiki_page |
|
838 | 837 | end |
|
839 | 838 | |
|
840 | 839 | self.wiki = wiki |
|
841 | 840 | wiki.save |
|
842 | 841 | # Reproduce page hierarchy |
|
843 | 842 | project.wiki.pages.each do |page| |
|
844 | 843 | if page.parent_id && wiki_pages_map[page.id] |
|
845 | 844 | wiki_pages_map[page.id].parent = wiki_pages_map[page.parent_id] |
|
846 | 845 | wiki_pages_map[page.id].save |
|
847 | 846 | end |
|
848 | 847 | end |
|
849 | 848 | end |
|
850 | 849 | end |
|
851 | 850 | |
|
852 | 851 | # Copies versions from +project+ |
|
853 | 852 | def copy_versions(project) |
|
854 | 853 | project.versions.each do |version| |
|
855 | 854 | new_version = Version.new |
|
856 | 855 | new_version.attributes = version.attributes.dup.except("id", "project_id", "created_on", "updated_on") |
|
857 | 856 | self.versions << new_version |
|
858 | 857 | end |
|
859 | 858 | end |
|
860 | 859 | |
|
861 | 860 | # Copies issue categories from +project+ |
|
862 | 861 | def copy_issue_categories(project) |
|
863 | 862 | project.issue_categories.each do |issue_category| |
|
864 | 863 | new_issue_category = IssueCategory.new |
|
865 | 864 | new_issue_category.attributes = issue_category.attributes.dup.except("id", "project_id") |
|
866 | 865 | self.issue_categories << new_issue_category |
|
867 | 866 | end |
|
868 | 867 | end |
|
869 | 868 | |
|
870 | 869 | # Copies issues from +project+ |
|
871 | 870 | def copy_issues(project) |
|
872 | 871 | # Stores the source issue id as a key and the copied issues as the |
|
873 | 872 | # value. Used to map the two together for issue relations. |
|
874 | 873 | issues_map = {} |
|
875 | 874 | |
|
876 | 875 | # Store status and reopen locked/closed versions |
|
877 | 876 | version_statuses = versions.reject(&:open?).map {|version| [version, version.status]} |
|
878 | 877 | version_statuses.each do |version, status| |
|
879 | 878 | version.update_attribute :status, 'open' |
|
880 | 879 | end |
|
881 | 880 | |
|
882 | 881 | # Get issues sorted by root_id, lft so that parent issues |
|
883 | 882 | # get copied before their children |
|
884 | 883 | project.issues.reorder('root_id, lft').each do |issue| |
|
885 | 884 | new_issue = Issue.new |
|
886 | 885 | new_issue.copy_from(issue, :subtasks => false, :link => false) |
|
887 | 886 | new_issue.project = self |
|
888 | 887 | # Changing project resets the custom field values |
|
889 | 888 | # TODO: handle this in Issue#project= |
|
890 | 889 | new_issue.custom_field_values = issue.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h} |
|
891 | 890 | # Reassign fixed_versions by name, since names are unique per project |
|
892 | 891 | if issue.fixed_version && issue.fixed_version.project == project |
|
893 | 892 | new_issue.fixed_version = self.versions.detect {|v| v.name == issue.fixed_version.name} |
|
894 | 893 | end |
|
895 | 894 | # Reassign the category by name, since names are unique per project |
|
896 | 895 | if issue.category |
|
897 | 896 | new_issue.category = self.issue_categories.detect {|c| c.name == issue.category.name} |
|
898 | 897 | end |
|
899 | 898 | # Parent issue |
|
900 | 899 | if issue.parent_id |
|
901 | 900 | if copied_parent = issues_map[issue.parent_id] |
|
902 | 901 | new_issue.parent_issue_id = copied_parent.id |
|
903 | 902 | end |
|
904 | 903 | end |
|
905 | 904 | |
|
906 | 905 | self.issues << new_issue |
|
907 | 906 | if new_issue.new_record? |
|
908 | 907 | logger.info "Project#copy_issues: issue ##{issue.id} could not be copied: #{new_issue.errors.full_messages}" if logger && logger.info |
|
909 | 908 | else |
|
910 | 909 | issues_map[issue.id] = new_issue unless new_issue.new_record? |
|
911 | 910 | end |
|
912 | 911 | end |
|
913 | 912 | |
|
914 | 913 | # Restore locked/closed version statuses |
|
915 | 914 | version_statuses.each do |version, status| |
|
916 | 915 | version.update_attribute :status, status |
|
917 | 916 | end |
|
918 | 917 | |
|
919 | 918 | # Relations after in case issues related each other |
|
920 | 919 | project.issues.each do |issue| |
|
921 | 920 | new_issue = issues_map[issue.id] |
|
922 | 921 | unless new_issue |
|
923 | 922 | # Issue was not copied |
|
924 | 923 | next |
|
925 | 924 | end |
|
926 | 925 | |
|
927 | 926 | # Relations |
|
928 | 927 | issue.relations_from.each do |source_relation| |
|
929 | 928 | new_issue_relation = IssueRelation.new |
|
930 | 929 | new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id") |
|
931 | 930 | new_issue_relation.issue_to = issues_map[source_relation.issue_to_id] |
|
932 | 931 | if new_issue_relation.issue_to.nil? && Setting.cross_project_issue_relations? |
|
933 | 932 | new_issue_relation.issue_to = source_relation.issue_to |
|
934 | 933 | end |
|
935 | 934 | new_issue.relations_from << new_issue_relation |
|
936 | 935 | end |
|
937 | 936 | |
|
938 | 937 | issue.relations_to.each do |source_relation| |
|
939 | 938 | new_issue_relation = IssueRelation.new |
|
940 | 939 | new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id") |
|
941 | 940 | new_issue_relation.issue_from = issues_map[source_relation.issue_from_id] |
|
942 | 941 | if new_issue_relation.issue_from.nil? && Setting.cross_project_issue_relations? |
|
943 | 942 | new_issue_relation.issue_from = source_relation.issue_from |
|
944 | 943 | end |
|
945 | 944 | new_issue.relations_to << new_issue_relation |
|
946 | 945 | end |
|
947 | 946 | end |
|
948 | 947 | end |
|
949 | 948 | |
|
950 | 949 | # Copies members from +project+ |
|
951 | 950 | def copy_members(project) |
|
952 | 951 | # Copy users first, then groups to handle members with inherited and given roles |
|
953 | 952 | members_to_copy = [] |
|
954 | 953 | members_to_copy += project.memberships.select {|m| m.principal.is_a?(User)} |
|
955 | 954 | members_to_copy += project.memberships.select {|m| !m.principal.is_a?(User)} |
|
956 | 955 | |
|
957 | 956 | members_to_copy.each do |member| |
|
958 | 957 | new_member = Member.new |
|
959 | 958 | new_member.attributes = member.attributes.dup.except("id", "project_id", "created_on") |
|
960 | 959 | # only copy non inherited roles |
|
961 | 960 | # inherited roles will be added when copying the group membership |
|
962 | 961 | role_ids = member.member_roles.reject(&:inherited?).collect(&:role_id) |
|
963 | 962 | next if role_ids.empty? |
|
964 | 963 | new_member.role_ids = role_ids |
|
965 | 964 | new_member.project = self |
|
966 | 965 | self.members << new_member |
|
967 | 966 | end |
|
968 | 967 | end |
|
969 | 968 | |
|
970 | 969 | # Copies queries from +project+ |
|
971 | 970 | def copy_queries(project) |
|
972 | 971 | project.queries.each do |query| |
|
973 | 972 | new_query = IssueQuery.new |
|
974 | 973 | new_query.attributes = query.attributes.dup.except("id", "project_id", "sort_criteria", "user_id", "type") |
|
975 | 974 | new_query.sort_criteria = query.sort_criteria if query.sort_criteria |
|
976 | 975 | new_query.project = self |
|
977 | 976 | new_query.user_id = query.user_id |
|
978 | 977 | new_query.role_ids = query.role_ids if query.visibility == IssueQuery::VISIBILITY_ROLES |
|
979 | 978 | self.queries << new_query |
|
980 | 979 | end |
|
981 | 980 | end |
|
982 | 981 | |
|
983 | 982 | # Copies boards from +project+ |
|
984 | 983 | def copy_boards(project) |
|
985 | 984 | project.boards.each do |board| |
|
986 | 985 | new_board = Board.new |
|
987 | 986 | new_board.attributes = board.attributes.dup.except("id", "project_id", "topics_count", "messages_count", "last_message_id") |
|
988 | 987 | new_board.project = self |
|
989 | 988 | self.boards << new_board |
|
990 | 989 | end |
|
991 | 990 | end |
|
992 | 991 | |
|
993 | 992 | def allowed_permissions |
|
994 | 993 | @allowed_permissions ||= begin |
|
995 | 994 | module_names = enabled_modules.loaded? ? enabled_modules.map(&:name) : enabled_modules.pluck(:name) |
|
996 | 995 | Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name} |
|
997 | 996 | end |
|
998 | 997 | end |
|
999 | 998 | |
|
1000 | 999 | def allowed_actions |
|
1001 | 1000 | @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten |
|
1002 | 1001 | end |
|
1003 | 1002 | |
|
1004 | 1003 | # Returns all the active Systemwide and project specific activities |
|
1005 | 1004 | def active_activities |
|
1006 | 1005 | overridden_activity_ids = self.time_entry_activities.collect(&:parent_id) |
|
1007 | 1006 | |
|
1008 | 1007 | if overridden_activity_ids.empty? |
|
1009 | 1008 | return TimeEntryActivity.shared.active |
|
1010 | 1009 | else |
|
1011 | 1010 | return system_activities_and_project_overrides |
|
1012 | 1011 | end |
|
1013 | 1012 | end |
|
1014 | 1013 | |
|
1015 | 1014 | # Returns all the Systemwide and project specific activities |
|
1016 | 1015 | # (inactive and active) |
|
1017 | 1016 | def all_activities |
|
1018 | 1017 | overridden_activity_ids = self.time_entry_activities.collect(&:parent_id) |
|
1019 | 1018 | |
|
1020 | 1019 | if overridden_activity_ids.empty? |
|
1021 | 1020 | return TimeEntryActivity.shared |
|
1022 | 1021 | else |
|
1023 | 1022 | return system_activities_and_project_overrides(true) |
|
1024 | 1023 | end |
|
1025 | 1024 | end |
|
1026 | 1025 | |
|
1027 | 1026 | # Returns the systemwide active activities merged with the project specific overrides |
|
1028 | 1027 | def system_activities_and_project_overrides(include_inactive=false) |
|
1029 | 1028 | t = TimeEntryActivity.table_name |
|
1030 | 1029 | scope = TimeEntryActivity.where( |
|
1031 | 1030 | "(#{t}.project_id IS NULL AND #{t}.id NOT IN (?)) OR (#{t}.project_id = ?)", |
|
1032 | 1031 | time_entry_activities.map(&:parent_id), id |
|
1033 | 1032 | ) |
|
1034 | 1033 | unless include_inactive |
|
1035 | 1034 | scope = scope.active |
|
1036 | 1035 | end |
|
1037 | 1036 | scope |
|
1038 | 1037 | end |
|
1039 | 1038 | |
|
1040 | 1039 | # Archives subprojects recursively |
|
1041 | 1040 | def archive! |
|
1042 | 1041 | children.each do |subproject| |
|
1043 | 1042 | subproject.send :archive! |
|
1044 | 1043 | end |
|
1045 | 1044 | update_attribute :status, STATUS_ARCHIVED |
|
1046 | 1045 | end |
|
1047 | 1046 | |
|
1048 | 1047 | def update_position_under_parent |
|
1049 | 1048 | set_or_update_position_under(parent) |
|
1050 | 1049 | end |
|
1051 | 1050 | |
|
1052 | 1051 | public |
|
1053 | 1052 | |
|
1054 | 1053 | # Inserts/moves the project so that target's children or root projects stay alphabetically sorted |
|
1055 | 1054 | def set_or_update_position_under(target_parent) |
|
1056 | 1055 | parent_was = parent |
|
1057 | 1056 | sibs = (target_parent.nil? ? self.class.roots : target_parent.children) |
|
1058 | 1057 | to_be_inserted_before = sibs.sort_by {|c| c.name.to_s.downcase}.detect {|c| c.name.to_s.downcase > name.to_s.downcase } |
|
1059 | 1058 | |
|
1060 | 1059 | if to_be_inserted_before |
|
1061 | 1060 | move_to_left_of(to_be_inserted_before) |
|
1062 | 1061 | elsif target_parent.nil? |
|
1063 | 1062 | if sibs.empty? |
|
1064 | 1063 | # move_to_root adds the project in first (ie. left) position |
|
1065 | 1064 | move_to_root |
|
1066 | 1065 | else |
|
1067 | 1066 | move_to_right_of(sibs.last) unless self == sibs.last |
|
1068 | 1067 | end |
|
1069 | 1068 | else |
|
1070 | 1069 | # move_to_child_of adds the project in last (ie.right) position |
|
1071 | 1070 | move_to_child_of(target_parent) |
|
1072 | 1071 | end |
|
1073 | 1072 | if parent_was != target_parent |
|
1074 | 1073 | after_parent_changed(parent_was) |
|
1075 | 1074 | end |
|
1076 | 1075 | end |
|
1077 | 1076 | end |
General Comments 0
You need to be logged in to leave comments.
Login now