annotate app/models/issue.rb @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 0a574315af3e 622f24f53b42
children
rev   line source
Chris@245 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@441 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@441 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 class Issue < ActiveRecord::Base
Chris@117 19 include Redmine::SafeAttributes
Chris@1115 20 include Redmine::Utils::DateCalculation
Chris@441 21
Chris@0 22 belongs_to :project
Chris@0 23 belongs_to :tracker
Chris@0 24 belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id'
Chris@0 25 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
Chris@909 26 belongs_to :assigned_to, :class_name => 'Principal', :foreign_key => 'assigned_to_id'
Chris@0 27 belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id'
Chris@0 28 belongs_to :priority, :class_name => 'IssuePriority', :foreign_key => 'priority_id'
Chris@0 29 belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id'
Chris@0 30
Chris@0 31 has_many :journals, :as => :journalized, :dependent => :destroy
Chris@1115 32 has_many :visible_journals,
Chris@1115 33 :class_name => 'Journal',
Chris@1115 34 :as => :journalized,
Chris@1115 35 :conditions => Proc.new {
Chris@1115 36 ["(#{Journal.table_name}.private_notes = ? OR (#{Project.allowed_to_condition(User.current, :view_private_notes)}))", false]
Chris@1115 37 },
Chris@1115 38 :readonly => true
Chris@1115 39
Chris@0 40 has_many :time_entries, :dependent => :delete_all
Chris@0 41 has_and_belongs_to_many :changesets, :order => "#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC"
Chris@441 42
Chris@0 43 has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all
Chris@0 44 has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all
Chris@441 45
Chris@210 46 acts_as_nested_set :scope => 'root_id', :dependent => :destroy
Chris@909 47 acts_as_attachable :after_add => :attachment_added, :after_remove => :attachment_removed
Chris@0 48 acts_as_customizable
Chris@0 49 acts_as_watchable
Chris@0 50 acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"],
Chris@1115 51 :include => [:project, :visible_journals],
Chris@0 52 # sort by id so that limited eager loading doesn't break with postgresql
Chris@0 53 :order_column => "#{table_name}.id"
Chris@0 54 acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id} (#{o.status}): #{o.subject}"},
Chris@0 55 :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}},
Chris@0 56 :type => Proc.new {|o| 'issue' + (o.closed? ? ' closed' : '') }
Chris@441 57
Chris@0 58 acts_as_activity_provider :find_options => {:include => [:project, :author, :tracker]},
Chris@0 59 :author_key => :author_id
Chris@0 60
Chris@0 61 DONE_RATIO_OPTIONS = %w(issue_field issue_status)
Chris@0 62
Chris@0 63 attr_reader :current_journal
Chris@1115 64 delegate :notes, :notes=, :private_notes, :private_notes=, :to => :current_journal, :allow_nil => true
Chris@0 65
Chris@0 66 validates_presence_of :subject, :priority, :project, :tracker, :author, :status
Chris@0 67
Chris@0 68 validates_length_of :subject, :maximum => 255
Chris@0 69 validates_inclusion_of :done_ratio, :in => 0..100
Chris@1295 70 validates :estimated_hours, :numericality => {:greater_than_or_equal_to => 0, :allow_nil => true, :message => :invalid}
Chris@1295 71 validates :start_date, :date => true
Chris@1295 72 validates :due_date, :date => true
Chris@1115 73 validate :validate_issue, :validate_required_fields
Chris@0 74
Chris@1295 75 scope :visible, lambda {|*args|
Chris@1295 76 includes(:project).where(Issue.visible_condition(args.shift || User.current, *args))
Chris@1295 77 }
Chris@441 78
Chris@1115 79 scope :open, lambda {|*args|
Chris@1115 80 is_closed = args.size > 0 ? !args.first : false
Chris@1295 81 includes(:status).where("#{IssueStatus.table_name}.is_closed = ?", is_closed)
chris@22 82 }
chris@22 83
Chris@1295 84 scope :recently_updated, lambda { order("#{Issue.table_name}.updated_on DESC") }
Chris@1295 85 scope :on_active_project, lambda {
Chris@1295 86 includes(:status, :project, :tracker).where("#{Project.table_name}.status = ?", Project::STATUS_ACTIVE)
Chris@1295 87 }
Chris@1295 88 scope :fixed_version, lambda {|versions|
Chris@1295 89 ids = [versions].flatten.compact.map {|v| v.is_a?(Version) ? v.id : v}
Chris@1295 90 ids.any? ? where(:fixed_version_id => ids) : where('1=0')
Chris@1295 91 }
Chris@0 92
Chris@0 93 before_create :default_assign
Chris@1295 94 before_save :close_duplicates, :update_done_ratio_from_issue_status, :force_updated_on_change, :update_closed_on
Chris@1115 95 after_save {|issue| issue.send :after_project_change if !issue.id_changed? && issue.project_id_changed?}
chris@37 96 after_save :reschedule_following_issues, :update_nested_set_attributes, :update_parent_attributes, :create_journal
Chris@1115 97 # Should be after_create but would be called before previous after_save callbacks
Chris@1115 98 after_save :after_create_from_copy
Chris@0 99 after_destroy :update_parent_attributes
Chris@441 100
Chris@441 101 # Returns a SQL conditions string used to find all issues visible by the specified user
Chris@441 102 def self.visible_condition(user, options={})
Chris@441 103 Project.allowed_to_condition(user, :view_issues, options) do |role, user|
Chris@1115 104 if user.logged?
Chris@1115 105 case role.issues_visibility
Chris@1115 106 when 'all'
Chris@1115 107 nil
Chris@1115 108 when 'default'
Chris@1115 109 user_ids = [user.id] + user.groups.map(&:id)
Chris@1115 110 "(#{table_name}.is_private = #{connection.quoted_false} OR #{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))"
Chris@1115 111 when 'own'
Chris@1115 112 user_ids = [user.id] + user.groups.map(&:id)
Chris@1115 113 "(#{table_name}.author_id = #{user.id} OR #{table_name}.assigned_to_id IN (#{user_ids.join(',')}))"
Chris@1115 114 else
Chris@1115 115 '1=0'
Chris@1115 116 end
Chris@441 117 else
Chris@1115 118 "(#{table_name}.is_private = #{connection.quoted_false})"
Chris@441 119 end
Chris@441 120 end
Chris@441 121 end
Chris@441 122
Chris@0 123 # Returns true if usr or current user is allowed to view the issue
Chris@0 124 def visible?(usr=nil)
Chris@441 125 (usr || User.current).allowed_to?(:view_issues, self.project) do |role, user|
Chris@1115 126 if user.logged?
Chris@1115 127 case role.issues_visibility
Chris@1115 128 when 'all'
Chris@1115 129 true
Chris@1115 130 when 'default'
Chris@1115 131 !self.is_private? || (self.author == user || user.is_or_belongs_to?(assigned_to))
Chris@1115 132 when 'own'
Chris@1115 133 self.author == user || user.is_or_belongs_to?(assigned_to)
Chris@1115 134 else
Chris@1115 135 false
Chris@1115 136 end
Chris@441 137 else
Chris@1115 138 !self.is_private?
Chris@441 139 end
Chris@441 140 end
Chris@0 141 end
Chris@441 142
Chris@1295 143 # Returns true if user or current user is allowed to edit or add a note to the issue
Chris@1295 144 def editable?(user=User.current)
Chris@1295 145 user.allowed_to?(:edit_issues, project) || user.allowed_to?(:add_issue_notes, project)
Chris@1295 146 end
Chris@1295 147
Chris@1115 148 def initialize(attributes=nil, *args)
Chris@1115 149 super
Chris@0 150 if new_record?
Chris@0 151 # set default values for new records only
Chris@0 152 self.status ||= IssueStatus.default
Chris@0 153 self.priority ||= IssuePriority.default
Chris@1115 154 self.watcher_user_ids = []
Chris@0 155 end
Chris@0 156 end
Chris@441 157
Chris@1295 158 def create_or_update
Chris@1295 159 super
Chris@1295 160 ensure
Chris@1295 161 @status_was = nil
Chris@1295 162 end
Chris@1295 163 private :create_or_update
Chris@1295 164
Chris@1115 165 # AR#Persistence#destroy would raise and RecordNotFound exception
Chris@1115 166 # if the issue was already deleted or updated (non matching lock_version).
Chris@1115 167 # This is a problem when bulk deleting issues or deleting a project
Chris@1115 168 # (because an issue may already be deleted if its parent was deleted
Chris@1115 169 # first).
Chris@1115 170 # The issue is reloaded by the nested_set before being deleted so
Chris@1115 171 # the lock_version condition should not be an issue but we handle it.
Chris@1115 172 def destroy
Chris@1115 173 super
Chris@1115 174 rescue ActiveRecord::RecordNotFound
Chris@1115 175 # Stale or already deleted
Chris@1115 176 begin
Chris@1115 177 reload
Chris@1115 178 rescue ActiveRecord::RecordNotFound
Chris@1115 179 # The issue was actually already deleted
Chris@1115 180 @destroyed = true
Chris@1115 181 return freeze
Chris@1115 182 end
Chris@1115 183 # The issue was stale, retry to destroy
Chris@1115 184 super
Chris@1115 185 end
Chris@1115 186
Chris@1295 187 alias :base_reload :reload
Chris@1115 188 def reload(*args)
Chris@1115 189 @workflow_rule_by_attribute = nil
Chris@1115 190 @assignable_versions = nil
Chris@1295 191 @relations = nil
Chris@1295 192 base_reload(*args)
Chris@1115 193 end
Chris@1115 194
Chris@0 195 # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
Chris@0 196 def available_custom_fields
Chris@441 197 (project && tracker) ? (project.all_issue_custom_fields & tracker.custom_fields.all) : []
Chris@0 198 end
Chris@441 199
Chris@1115 200 # Copies attributes from another issue, arg can be an id or an Issue
Chris@1115 201 def copy_from(arg, options={})
Chris@0 202 issue = arg.is_a?(Issue) ? arg : Issue.visible.find(arg)
Chris@0 203 self.attributes = issue.attributes.dup.except("id", "root_id", "parent_id", "lft", "rgt", "created_on", "updated_on")
Chris@0 204 self.custom_field_values = issue.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
Chris@0 205 self.status = issue.status
Chris@1115 206 self.author = User.current
Chris@1115 207 unless options[:attachments] == false
Chris@1115 208 self.attachments = issue.attachments.map do |attachement|
Chris@1115 209 attachement.copy(:container => self)
Chris@1115 210 end
Chris@1115 211 end
Chris@1115 212 @copied_from = issue
Chris@1115 213 @copy_options = options
Chris@0 214 self
Chris@0 215 end
Chris@441 216
Chris@1115 217 # Returns an unsaved copy of the issue
Chris@1115 218 def copy(attributes=nil, copy_options={})
Chris@1115 219 copy = self.class.new.copy_from(self, copy_options)
Chris@1115 220 copy.attributes = attributes if attributes
Chris@1115 221 copy
Chris@1115 222 end
Chris@1115 223
Chris@1115 224 # Returns true if the issue is a copy
Chris@1115 225 def copy?
Chris@1115 226 @copied_from.present?
Chris@1115 227 end
Chris@1115 228
Chris@0 229 # Moves/copies an issue to a new project and tracker
Chris@0 230 # Returns the moved/copied issue on success, false on failure
Chris@1115 231 def move_to_project(new_project, new_tracker=nil, options={})
Chris@1115 232 ActiveSupport::Deprecation.warn "Issue#move_to_project is deprecated, use #project= instead."
Chris@441 233
Chris@1115 234 if options[:copy]
Chris@1115 235 issue = self.copy
Chris@1115 236 else
Chris@1115 237 issue = self
Chris@1115 238 end
Chris@441 239
Chris@1115 240 issue.init_journal(User.current, options[:notes])
Chris@1115 241
Chris@1115 242 # Preserve previous behaviour
Chris@1115 243 # #move_to_project doesn't change tracker automatically
Chris@1115 244 issue.send :project=, new_project, true
Chris@0 245 if new_tracker
Chris@0 246 issue.tracker = new_tracker
Chris@0 247 end
Chris@0 248 # Allow bulk setting of attributes on the issue
Chris@0 249 if options[:attributes]
Chris@0 250 issue.attributes = options[:attributes]
Chris@0 251 end
Chris@441 252
Chris@1115 253 issue.save ? issue : false
Chris@0 254 end
Chris@0 255
Chris@0 256 def status_id=(sid)
Chris@0 257 self.status = nil
Chris@1115 258 result = write_attribute(:status_id, sid)
Chris@1115 259 @workflow_rule_by_attribute = nil
Chris@1115 260 result
Chris@0 261 end
Chris@441 262
Chris@0 263 def priority_id=(pid)
Chris@0 264 self.priority = nil
Chris@0 265 write_attribute(:priority_id, pid)
Chris@0 266 end
Chris@0 267
Chris@1115 268 def category_id=(cid)
Chris@1115 269 self.category = nil
Chris@1115 270 write_attribute(:category_id, cid)
Chris@1115 271 end
Chris@1115 272
Chris@1115 273 def fixed_version_id=(vid)
Chris@1115 274 self.fixed_version = nil
Chris@1115 275 write_attribute(:fixed_version_id, vid)
Chris@1115 276 end
Chris@1115 277
Chris@0 278 def tracker_id=(tid)
Chris@0 279 self.tracker = nil
Chris@0 280 result = write_attribute(:tracker_id, tid)
Chris@0 281 @custom_field_values = nil
Chris@1115 282 @workflow_rule_by_attribute = nil
Chris@0 283 result
Chris@0 284 end
Chris@909 285
Chris@1115 286 def project_id=(project_id)
Chris@1115 287 if project_id.to_s != self.project_id.to_s
Chris@1115 288 self.project = (project_id.present? ? Project.find_by_id(project_id) : nil)
Chris@1115 289 end
Chris@1115 290 end
Chris@1115 291
Chris@1115 292 def project=(project, keep_tracker=false)
Chris@1115 293 project_was = self.project
Chris@1115 294 write_attribute(:project_id, project ? project.id : nil)
Chris@1115 295 association_instance_set('project', project)
Chris@1115 296 if project_was && project && project_was != project
Chris@1115 297 @assignable_versions = nil
Chris@1115 298
Chris@1115 299 unless keep_tracker || project.trackers.include?(tracker)
Chris@1115 300 self.tracker = project.trackers.first
Chris@1115 301 end
Chris@1115 302 # Reassign to the category with same name if any
Chris@1115 303 if category
Chris@1115 304 self.category = project.issue_categories.find_by_name(category.name)
Chris@1115 305 end
Chris@1115 306 # Keep the fixed_version if it's still valid in the new_project
Chris@1115 307 if fixed_version && fixed_version.project != project && !project.shared_versions.include?(fixed_version)
Chris@1115 308 self.fixed_version = nil
Chris@1115 309 end
Chris@1115 310 # Clear the parent task if it's no longer valid
Chris@1115 311 unless valid_parent_project?
Chris@1115 312 self.parent_issue_id = nil
Chris@1115 313 end
Chris@1115 314 @custom_field_values = nil
Chris@1115 315 end
Chris@1115 316 end
Chris@1115 317
Chris@507 318 def description=(arg)
Chris@507 319 if arg.is_a?(String)
Chris@507 320 arg = arg.gsub(/(\r\n|\n|\r)/, "\r\n")
Chris@507 321 end
Chris@507 322 write_attribute(:description, arg)
Chris@507 323 end
Chris@441 324
Chris@1115 325 # Overrides assign_attributes so that project and tracker get assigned first
Chris@1115 326 def assign_attributes_with_project_and_tracker_first(new_attributes, *args)
Chris@0 327 return if new_attributes.nil?
Chris@1115 328 attrs = new_attributes.dup
Chris@1115 329 attrs.stringify_keys!
Chris@1115 330
Chris@1115 331 %w(project project_id tracker tracker_id).each do |attr|
Chris@1115 332 if attrs.has_key?(attr)
Chris@1115 333 send "#{attr}=", attrs.delete(attr)
Chris@1115 334 end
Chris@0 335 end
Chris@1115 336 send :assign_attributes_without_project_and_tracker_first, attrs, *args
Chris@0 337 end
Chris@0 338 # Do not redefine alias chain on reload (see #4838)
Chris@1115 339 alias_method_chain(:assign_attributes, :project_and_tracker_first) unless method_defined?(:assign_attributes_without_project_and_tracker_first)
Chris@441 340
Chris@0 341 def estimated_hours=(h)
Chris@0 342 write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h)
Chris@0 343 end
Chris@441 344
Chris@1115 345 safe_attributes 'project_id',
Chris@1115 346 :if => lambda {|issue, user|
Chris@1115 347 if issue.new_record?
Chris@1115 348 issue.copy?
Chris@1115 349 elsif user.allowed_to?(:move_issues, issue.project)
Chris@1115 350 projects = Issue.allowed_target_projects_on_move(user)
Chris@1115 351 projects.include?(issue.project) && projects.size > 1
Chris@1115 352 end
Chris@1115 353 }
Chris@1115 354
Chris@117 355 safe_attributes 'tracker_id',
Chris@117 356 'status_id',
Chris@117 357 'category_id',
Chris@117 358 'assigned_to_id',
Chris@117 359 'priority_id',
Chris@117 360 'fixed_version_id',
Chris@117 361 'subject',
Chris@117 362 'description',
Chris@117 363 'start_date',
Chris@117 364 'due_date',
Chris@117 365 'done_ratio',
Chris@117 366 'estimated_hours',
Chris@117 367 'custom_field_values',
Chris@117 368 'custom_fields',
Chris@117 369 'lock_version',
Chris@1115 370 'notes',
Chris@117 371 :if => lambda {|issue, user| issue.new_record? || user.allowed_to?(:edit_issues, issue.project) }
Chris@441 372
Chris@117 373 safe_attributes 'status_id',
Chris@117 374 'assigned_to_id',
Chris@117 375 'fixed_version_id',
Chris@117 376 'done_ratio',
Chris@1115 377 'lock_version',
Chris@1115 378 'notes',
Chris@117 379 :if => lambda {|issue, user| issue.new_statuses_allowed_to(user).any? }
chris@37 380
Chris@1115 381 safe_attributes 'notes',
Chris@1115 382 :if => lambda {|issue, user| user.allowed_to?(:add_issue_notes, issue.project)}
Chris@1115 383
Chris@1115 384 safe_attributes 'private_notes',
Chris@1115 385 :if => lambda {|issue, user| !issue.new_record? && user.allowed_to?(:set_notes_private, issue.project)}
Chris@1115 386
Chris@1115 387 safe_attributes 'watcher_user_ids',
Chris@1115 388 :if => lambda {|issue, user| issue.new_record? && user.allowed_to?(:add_issue_watchers, issue.project)}
Chris@1115 389
Chris@441 390 safe_attributes 'is_private',
Chris@441 391 :if => lambda {|issue, user|
Chris@441 392 user.allowed_to?(:set_issues_private, issue.project) ||
Chris@441 393 (issue.author == user && user.allowed_to?(:set_own_issues_private, issue.project))
Chris@441 394 }
Chris@441 395
Chris@1115 396 safe_attributes 'parent_issue_id',
Chris@1115 397 :if => lambda {|issue, user| (issue.new_record? || user.allowed_to?(:edit_issues, issue.project)) &&
Chris@1115 398 user.allowed_to?(:manage_subtasks, issue.project)}
Chris@1115 399
Chris@1115 400 def safe_attribute_names(user=nil)
Chris@1115 401 names = super
Chris@1115 402 names -= disabled_core_fields
Chris@1115 403 names -= read_only_attribute_names(user)
Chris@1115 404 names
Chris@1115 405 end
Chris@1115 406
Chris@0 407 # Safely sets attributes
Chris@0 408 # Should be called from controllers instead of #attributes=
Chris@0 409 # attr_accessible is too rough because we still want things like
Chris@0 410 # Issue.new(:project => foo) to work
Chris@0 411 def safe_attributes=(attrs, user=User.current)
chris@37 412 return unless attrs.is_a?(Hash)
Chris@441 413
Chris@1115 414 attrs = attrs.dup
Chris@441 415
Chris@1115 416 # Project and Tracker must be set before since new_statuses_allowed_to depends on it.
Chris@1115 417 if (p = attrs.delete('project_id')) && safe_attribute?('project_id')
Chris@1115 418 if allowed_target_projects(user).collect(&:id).include?(p.to_i)
Chris@1115 419 self.project_id = p
Chris@1115 420 end
Chris@1115 421 end
Chris@1115 422
Chris@1115 423 if (t = attrs.delete('tracker_id')) && safe_attribute?('tracker_id')
chris@37 424 self.tracker_id = t
chris@37 425 end
Chris@441 426
Chris@1115 427 if (s = attrs.delete('status_id')) && safe_attribute?('status_id')
Chris@1115 428 if new_statuses_allowed_to(user).collect(&:id).include?(s.to_i)
Chris@1115 429 self.status_id = s
Chris@0 430 end
Chris@0 431 end
Chris@441 432
Chris@1115 433 attrs = delete_unsafe_attributes(attrs, user)
Chris@1115 434 return if attrs.empty?
Chris@1115 435
Chris@0 436 unless leaf?
Chris@0 437 attrs.reject! {|k,v| %w(priority_id done_ratio start_date due_date estimated_hours).include?(k)}
Chris@0 438 end
Chris@441 439
Chris@1115 440 if attrs['parent_issue_id'].present?
Chris@1115 441 s = attrs['parent_issue_id'].to_s
Chris@1294 442 unless (m = s.match(%r{\A#?(\d+)\z})) && (m[1] == parent_id.to_s || Issue.visible(user).exists?(m[1]))
Chris@1115 443 @invalid_parent_issue_id = attrs.delete('parent_issue_id')
Chris@0 444 end
Chris@0 445 end
Chris@441 446
Chris@1115 447 if attrs['custom_field_values'].present?
Chris@1115 448 attrs['custom_field_values'] = attrs['custom_field_values'].reject {|k, v| read_only_attribute_names(user).include? k.to_s}
Chris@1115 449 end
Chris@1115 450
Chris@1115 451 if attrs['custom_fields'].present?
Chris@1115 452 attrs['custom_fields'] = attrs['custom_fields'].reject {|c| read_only_attribute_names(user).include? c['id'].to_s}
Chris@1115 453 end
Chris@1115 454
Chris@1115 455 # mass-assignment security bypass
Chris@1115 456 assign_attributes attrs, :without_protection => true
Chris@0 457 end
Chris@441 458
Chris@1115 459 def disabled_core_fields
Chris@1115 460 tracker ? tracker.disabled_core_fields : []
Chris@1115 461 end
Chris@1115 462
Chris@1115 463 # Returns the custom_field_values that can be edited by the given user
Chris@1115 464 def editable_custom_field_values(user=nil)
Chris@1115 465 custom_field_values.reject do |value|
Chris@1115 466 read_only_attribute_names(user).include?(value.custom_field_id.to_s)
Chris@1115 467 end
Chris@1115 468 end
Chris@1115 469
Chris@1115 470 # Returns the names of attributes that are read-only for user or the current user
Chris@1115 471 # For users with multiple roles, the read-only fields are the intersection of
Chris@1115 472 # read-only fields of each role
Chris@1115 473 # The result is an array of strings where sustom fields are represented with their ids
Chris@1115 474 #
Chris@1115 475 # Examples:
Chris@1115 476 # issue.read_only_attribute_names # => ['due_date', '2']
Chris@1115 477 # issue.read_only_attribute_names(user) # => []
Chris@1115 478 def read_only_attribute_names(user=nil)
Chris@1115 479 workflow_rule_by_attribute(user).reject {|attr, rule| rule != 'readonly'}.keys
Chris@1115 480 end
Chris@1115 481
Chris@1115 482 # Returns the names of required attributes for user or the current user
Chris@1115 483 # For users with multiple roles, the required fields are the intersection of
Chris@1115 484 # required fields of each role
Chris@1115 485 # The result is an array of strings where sustom fields are represented with their ids
Chris@1115 486 #
Chris@1115 487 # Examples:
Chris@1115 488 # issue.required_attribute_names # => ['due_date', '2']
Chris@1115 489 # issue.required_attribute_names(user) # => []
Chris@1115 490 def required_attribute_names(user=nil)
Chris@1115 491 workflow_rule_by_attribute(user).reject {|attr, rule| rule != 'required'}.keys
Chris@1115 492 end
Chris@1115 493
Chris@1115 494 # Returns true if the attribute is required for user
Chris@1115 495 def required_attribute?(name, user=nil)
Chris@1115 496 required_attribute_names(user).include?(name.to_s)
Chris@1115 497 end
Chris@1115 498
Chris@1115 499 # Returns a hash of the workflow rule by attribute for the given user
Chris@1115 500 #
Chris@1115 501 # Examples:
Chris@1115 502 # issue.workflow_rule_by_attribute # => {'due_date' => 'required', 'start_date' => 'readonly'}
Chris@1115 503 def workflow_rule_by_attribute(user=nil)
Chris@1115 504 return @workflow_rule_by_attribute if @workflow_rule_by_attribute && user.nil?
Chris@1115 505
Chris@1115 506 user_real = user || User.current
Chris@1115 507 roles = user_real.admin ? Role.all : user_real.roles_for_project(project)
Chris@1115 508 return {} if roles.empty?
Chris@1115 509
Chris@1115 510 result = {}
Chris@1115 511 workflow_permissions = WorkflowPermission.where(:tracker_id => tracker_id, :old_status_id => status_id, :role_id => roles.map(&:id)).all
Chris@1115 512 if workflow_permissions.any?
Chris@1115 513 workflow_rules = workflow_permissions.inject({}) do |h, wp|
Chris@1115 514 h[wp.field_name] ||= []
Chris@1115 515 h[wp.field_name] << wp.rule
Chris@1115 516 h
Chris@1115 517 end
Chris@1115 518 workflow_rules.each do |attr, rules|
Chris@1115 519 next if rules.size < roles.size
Chris@1115 520 uniq_rules = rules.uniq
Chris@1115 521 if uniq_rules.size == 1
Chris@1115 522 result[attr] = uniq_rules.first
Chris@1115 523 else
Chris@1115 524 result[attr] = 'required'
Chris@1115 525 end
Chris@1115 526 end
Chris@1115 527 end
Chris@1115 528 @workflow_rule_by_attribute = result if user.nil?
Chris@1115 529 result
Chris@1115 530 end
Chris@1115 531 private :workflow_rule_by_attribute
Chris@1115 532
Chris@0 533 def done_ratio
chris@37 534 if Issue.use_status_for_done_ratio? && status && status.default_done_ratio
Chris@0 535 status.default_done_ratio
Chris@0 536 else
Chris@0 537 read_attribute(:done_ratio)
Chris@0 538 end
Chris@0 539 end
Chris@0 540
Chris@0 541 def self.use_status_for_done_ratio?
Chris@0 542 Setting.issue_done_ratio == 'issue_status'
Chris@0 543 end
Chris@0 544
Chris@0 545 def self.use_field_for_done_ratio?
Chris@0 546 Setting.issue_done_ratio == 'issue_field'
Chris@0 547 end
Chris@441 548
Chris@909 549 def validate_issue
Chris@1115 550 if due_date && start_date && due_date < start_date
Chris@0 551 errors.add :due_date, :greater_than_start_date
Chris@0 552 end
Chris@441 553
Chris@0 554 if start_date && soonest_start && start_date < soonest_start
Chris@0 555 errors.add :start_date, :invalid
Chris@0 556 end
Chris@441 557
Chris@0 558 if fixed_version
Chris@0 559 if !assignable_versions.include?(fixed_version)
Chris@0 560 errors.add :fixed_version_id, :inclusion
Chris@0 561 elsif reopened? && fixed_version.closed?
Chris@909 562 errors.add :base, I18n.t(:error_can_not_reopen_issue_on_closed_version)
Chris@0 563 end
Chris@0 564 end
Chris@441 565
Chris@0 566 # Checks that the issue can not be added/moved to a disabled tracker
Chris@0 567 if project && (tracker_id_changed? || project_id_changed?)
Chris@0 568 unless project.trackers.include?(tracker)
Chris@0 569 errors.add :tracker_id, :inclusion
Chris@0 570 end
Chris@0 571 end
Chris@441 572
Chris@0 573 # Checks parent issue assignment
Chris@1115 574 if @invalid_parent_issue_id.present?
Chris@1115 575 errors.add :parent_issue_id, :invalid
Chris@1115 576 elsif @parent_issue
Chris@1115 577 if !valid_parent_project?(@parent_issue)
Chris@1115 578 errors.add :parent_issue_id, :invalid
Chris@1295 579 elsif (@parent_issue != parent) && (all_dependent_issues.include?(@parent_issue) || @parent_issue.all_dependent_issues.include?(self))
Chris@1295 580 errors.add :parent_issue_id, :invalid
Chris@0 581 elsif !new_record?
Chris@0 582 # moving an existing issue
Chris@0 583 if @parent_issue.root_id != root_id
Chris@0 584 # we can always move to another tree
Chris@0 585 elsif move_possible?(@parent_issue)
Chris@0 586 # move accepted inside tree
Chris@0 587 else
Chris@1115 588 errors.add :parent_issue_id, :invalid
Chris@1115 589 end
Chris@1115 590 end
Chris@1115 591 end
Chris@1115 592 end
Chris@1115 593
Chris@1115 594 # Validates the issue against additional workflow requirements
Chris@1115 595 def validate_required_fields
Chris@1115 596 user = new_record? ? author : current_journal.try(:user)
Chris@1115 597
Chris@1115 598 required_attribute_names(user).each do |attribute|
Chris@1115 599 if attribute =~ /^\d+$/
Chris@1115 600 attribute = attribute.to_i
Chris@1115 601 v = custom_field_values.detect {|v| v.custom_field_id == attribute }
Chris@1115 602 if v && v.value.blank?
Chris@1115 603 errors.add :base, v.custom_field.name + ' ' + l('activerecord.errors.messages.blank')
Chris@1115 604 end
Chris@1115 605 else
Chris@1115 606 if respond_to?(attribute) && send(attribute).blank?
Chris@1115 607 errors.add attribute, :blank
Chris@0 608 end
Chris@0 609 end
Chris@0 610 end
Chris@0 611 end
Chris@441 612
Chris@0 613 # Set the done_ratio using the status if that setting is set. This will keep the done_ratios
Chris@0 614 # even if the user turns off the setting later
Chris@0 615 def update_done_ratio_from_issue_status
chris@37 616 if Issue.use_status_for_done_ratio? && status && status.default_done_ratio
Chris@0 617 self.done_ratio = status.default_done_ratio
Chris@0 618 end
Chris@0 619 end
Chris@441 620
Chris@0 621 def init_journal(user, notes = "")
Chris@0 622 @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
Chris@1115 623 if new_record?
Chris@1115 624 @current_journal.notify = false
Chris@1115 625 else
Chris@1115 626 @attributes_before_change = attributes.dup
Chris@1115 627 @custom_values_before_change = {}
Chris@1115 628 self.custom_field_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
Chris@1115 629 end
Chris@0 630 @current_journal
Chris@0 631 end
Chris@441 632
Chris@1115 633 # Returns the id of the last journal or nil
Chris@1115 634 def last_journal_id
Chris@1115 635 if new_record?
Chris@1115 636 nil
Chris@1115 637 else
Chris@1115 638 journals.maximum(:id)
Chris@1115 639 end
Chris@1115 640 end
Chris@1115 641
Chris@1115 642 # Returns a scope for journals that have an id greater than journal_id
Chris@1115 643 def journals_after(journal_id)
Chris@1115 644 scope = journals.reorder("#{Journal.table_name}.id ASC")
Chris@1115 645 if journal_id.present?
Chris@1115 646 scope = scope.where("#{Journal.table_name}.id > ?", journal_id.to_i)
Chris@1115 647 end
Chris@1115 648 scope
Chris@1115 649 end
Chris@1115 650
Chris@1295 651 # Returns the initial status of the issue
Chris@1295 652 # Returns nil for a new issue
Chris@1295 653 def status_was
Chris@1295 654 if status_id_was && status_id_was.to_i > 0
Chris@1295 655 @status_was ||= IssueStatus.find_by_id(status_id_was)
Chris@1295 656 end
Chris@1295 657 end
Chris@1295 658
Chris@0 659 # Return true if the issue is closed, otherwise false
Chris@0 660 def closed?
Chris@0 661 self.status.is_closed?
Chris@0 662 end
Chris@441 663
Chris@0 664 # Return true if the issue is being reopened
Chris@0 665 def reopened?
Chris@0 666 if !new_record? && status_id_changed?
Chris@0 667 status_was = IssueStatus.find_by_id(status_id_was)
Chris@0 668 status_new = IssueStatus.find_by_id(status_id)
Chris@0 669 if status_was && status_new && status_was.is_closed? && !status_new.is_closed?
Chris@0 670 return true
Chris@0 671 end
Chris@0 672 end
Chris@0 673 false
Chris@0 674 end
Chris@0 675
Chris@0 676 # Return true if the issue is being closed
Chris@0 677 def closing?
Chris@0 678 if !new_record? && status_id_changed?
Chris@1295 679 if status_was && status && !status_was.is_closed? && status.is_closed?
Chris@0 680 return true
Chris@0 681 end
Chris@0 682 end
Chris@0 683 false
Chris@0 684 end
Chris@441 685
Chris@0 686 # Returns true if the issue is overdue
Chris@0 687 def overdue?
Chris@0 688 !due_date.nil? && (due_date < Date.today) && !status.is_closed?
Chris@0 689 end
chris@22 690
chris@22 691 # Is the amount of work done less than it should for the due date
chris@22 692 def behind_schedule?
chris@22 693 return false if start_date.nil? || due_date.nil?
chris@22 694 done_date = start_date + ((due_date - start_date+1)* done_ratio/100).floor
chris@22 695 return done_date <= Date.today
chris@22 696 end
chris@22 697
chris@22 698 # Does this issue have children?
chris@22 699 def children?
chris@22 700 !leaf?
chris@22 701 end
Chris@441 702
Chris@0 703 # Users the issue can be assigned to
Chris@0 704 def assignable_users
chris@37 705 users = project.assignable_users
chris@37 706 users << author if author
Chris@909 707 users << assigned_to if assigned_to
chris@37 708 users.uniq.sort
Chris@0 709 end
Chris@441 710
Chris@0 711 # Versions that the issue can be assigned to
Chris@0 712 def assignable_versions
Chris@1115 713 return @assignable_versions if @assignable_versions
Chris@1115 714
Chris@1115 715 versions = project.shared_versions.open.all
Chris@1115 716 if fixed_version
Chris@1115 717 if fixed_version_id_changed?
Chris@1115 718 # nothing to do
Chris@1115 719 elsif project_id_changed?
Chris@1115 720 if project.shared_versions.include?(fixed_version)
Chris@1115 721 versions << fixed_version
Chris@1115 722 end
Chris@1115 723 else
Chris@1115 724 versions << fixed_version
Chris@1115 725 end
Chris@1115 726 end
Chris@1115 727 @assignable_versions = versions.uniq.sort
Chris@0 728 end
Chris@441 729
Chris@0 730 # Returns true if this issue is blocked by another issue that is still open
Chris@0 731 def blocked?
Chris@0 732 !relations_to.detect {|ir| ir.relation_type == 'blocks' && !ir.issue_from.closed?}.nil?
Chris@0 733 end
Chris@441 734
Chris@1115 735 # Returns an array of statuses that user is able to apply
Chris@1115 736 def new_statuses_allowed_to(user=User.current, include_default=false)
Chris@1115 737 if new_record? && @copied_from
Chris@1115 738 [IssueStatus.default, @copied_from.status].compact.uniq.sort
Chris@1115 739 else
Chris@1115 740 initial_status = nil
Chris@1115 741 if new_record?
Chris@1115 742 initial_status = IssueStatus.default
Chris@1115 743 elsif status_id_was
Chris@1115 744 initial_status = IssueStatus.find_by_id(status_id_was)
Chris@1115 745 end
Chris@1115 746 initial_status ||= status
Chris@1115 747
Chris@1115 748 statuses = initial_status.find_new_statuses_allowed_to(
Chris@1115 749 user.admin ? Role.all : user.roles_for_project(project),
Chris@1115 750 tracker,
Chris@1115 751 author == user,
Chris@1115 752 assigned_to_id_changed? ? assigned_to_id_was == user.id : assigned_to_id == user.id
Chris@1115 753 )
Chris@1115 754 statuses << initial_status unless statuses.empty?
Chris@1115 755 statuses << IssueStatus.default if include_default
Chris@1115 756 statuses = statuses.compact.uniq.sort
Chris@1115 757 blocked? ? statuses.reject {|s| s.is_closed?} : statuses
Chris@1115 758 end
Chris@0 759 end
Chris@441 760
Chris@1115 761 def assigned_to_was
Chris@1115 762 if assigned_to_id_changed? && assigned_to_id_was.present?
Chris@1115 763 @assigned_to_was ||= User.find_by_id(assigned_to_id_was)
Chris@1115 764 end
Chris@1115 765 end
Chris@1115 766
Chris@1115 767 # Returns the users that should be notified
Chris@1115 768 def notified_users
Chris@1115 769 notified = []
chris@37 770 # Author and assignee are always notified unless they have been
chris@37 771 # locked or don't want to be notified
Chris@1115 772 notified << author if author
Chris@909 773 if assigned_to
Chris@1115 774 notified += (assigned_to.is_a?(Group) ? assigned_to.users : [assigned_to])
Chris@909 775 end
Chris@1115 776 if assigned_to_was
Chris@1115 777 notified += (assigned_to_was.is_a?(Group) ? assigned_to_was.users : [assigned_to_was])
Chris@1115 778 end
Chris@1115 779 notified = notified.select {|u| u.active? && u.notify_about?(self)}
Chris@1115 780
Chris@1115 781 notified += project.notified_users
Chris@0 782 notified.uniq!
Chris@0 783 # Remove users that can not view the issue
Chris@0 784 notified.reject! {|user| !visible?(user)}
Chris@1115 785 notified
Chris@1115 786 end
Chris@1115 787
Chris@1115 788 # Returns the email addresses that should be notified
Chris@1115 789 def recipients
Chris@1115 790 notified_users.collect(&:mail)
Chris@1115 791 end
Chris@1115 792
Chris@1115 793 # Returns the number of hours spent on this issue
Chris@1115 794 def spent_hours
Chris@1115 795 @spent_hours ||= time_entries.sum(:hours) || 0
Chris@0 796 end
Chris@441 797
Chris@0 798 # Returns the total number of hours spent on this issue and its descendants
Chris@0 799 #
Chris@0 800 # Example:
Chris@0 801 # spent_hours => 0.0
Chris@0 802 # spent_hours => 50.2
Chris@1115 803 def total_spent_hours
Chris@1115 804 @total_spent_hours ||= self_and_descendants.sum("#{TimeEntry.table_name}.hours",
Chris@1115 805 :joins => "LEFT JOIN #{TimeEntry.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id").to_f || 0.0
Chris@0 806 end
Chris@441 807
Chris@0 808 def relations
Chris@1295 809 @relations ||= IssueRelation::Relations.new(self, (relations_from + relations_to).sort)
Chris@909 810 end
Chris@909 811
Chris@909 812 # Preloads relations for a collection of issues
Chris@909 813 def self.load_relations(issues)
Chris@909 814 if issues.any?
Chris@909 815 relations = IssueRelation.all(:conditions => ["issue_from_id IN (:ids) OR issue_to_id IN (:ids)", {:ids => issues.map(&:id)}])
Chris@909 816 issues.each do |issue|
Chris@909 817 issue.instance_variable_set "@relations", relations.select {|r| r.issue_from_id == issue.id || r.issue_to_id == issue.id}
Chris@909 818 end
Chris@909 819 end
Chris@909 820 end
Chris@909 821
Chris@1115 822 # Preloads visible spent time for a collection of issues
Chris@1115 823 def self.load_visible_spent_hours(issues, user=User.current)
Chris@1115 824 if issues.any?
Chris@1115 825 hours_by_issue_id = TimeEntry.visible(user).sum(:hours, :group => :issue_id)
Chris@1115 826 issues.each do |issue|
Chris@1115 827 issue.instance_variable_set "@spent_hours", (hours_by_issue_id[issue.id] || 0)
Chris@1115 828 end
Chris@1115 829 end
Chris@1115 830 end
Chris@1115 831
Chris@1115 832 # Preloads visible relations for a collection of issues
Chris@1115 833 def self.load_visible_relations(issues, user=User.current)
Chris@1115 834 if issues.any?
Chris@1115 835 issue_ids = issues.map(&:id)
Chris@1115 836 # Relations with issue_from in given issues and visible issue_to
Chris@1115 837 relations_from = IssueRelation.includes(:issue_to => [:status, :project]).where(visible_condition(user)).where(:issue_from_id => issue_ids).all
Chris@1115 838 # Relations with issue_to in given issues and visible issue_from
Chris@1115 839 relations_to = IssueRelation.includes(:issue_from => [:status, :project]).where(visible_condition(user)).where(:issue_to_id => issue_ids).all
Chris@1115 840
Chris@1115 841 issues.each do |issue|
Chris@1115 842 relations =
Chris@1115 843 relations_from.select {|relation| relation.issue_from_id == issue.id} +
Chris@1115 844 relations_to.select {|relation| relation.issue_to_id == issue.id}
Chris@1115 845
Chris@1295 846 issue.instance_variable_set "@relations", IssueRelation::Relations.new(issue, relations.sort)
Chris@1115 847 end
Chris@1115 848 end
Chris@1115 849 end
Chris@1115 850
Chris@909 851 # Finds an issue relation given its id.
Chris@909 852 def find_relation(relation_id)
Chris@909 853 IssueRelation.find(relation_id, :conditions => ["issue_to_id = ? OR issue_from_id = ?", id, id])
Chris@0 854 end
Chris@441 855
Chris@1295 856 # Returns all the other issues that depend on the issue
Chris@441 857 def all_dependent_issues(except=[])
Chris@441 858 except << self
Chris@0 859 dependencies = []
Chris@1295 860 dependencies += relations_from.map(&:issue_to)
Chris@1295 861 dependencies += children unless leaf?
Chris@1295 862 dependencies.compact!
Chris@1295 863 dependencies -= except
Chris@1295 864 dependencies += dependencies.map {|issue| issue.all_dependent_issues(except)}.flatten
Chris@1295 865 if parent
Chris@1295 866 dependencies << parent
Chris@1295 867 dependencies += parent.all_dependent_issues(except + parent.descendants)
Chris@0 868 end
Chris@0 869 dependencies
Chris@0 870 end
Chris@441 871
Chris@0 872 # Returns an array of issues that duplicate this one
Chris@0 873 def duplicates
Chris@0 874 relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from}
Chris@0 875 end
Chris@441 876
Chris@0 877 # Returns the due date or the target due date if any
Chris@0 878 # Used on gantt chart
Chris@0 879 def due_before
Chris@0 880 due_date || (fixed_version ? fixed_version.effective_date : nil)
Chris@0 881 end
Chris@441 882
Chris@0 883 # Returns the time scheduled for this issue.
Chris@441 884 #
Chris@0 885 # Example:
Chris@0 886 # Start Date: 2/26/09, End Date: 3/04/09
Chris@0 887 # duration => 6
Chris@0 888 def duration
Chris@0 889 (start_date && due_date) ? due_date - start_date : 0
Chris@0 890 end
Chris@441 891
Chris@1115 892 # Returns the duration in working days
Chris@1115 893 def working_duration
Chris@1115 894 (start_date && due_date) ? working_days(start_date, due_date) : 0
Chris@1115 895 end
Chris@1115 896
Chris@1115 897 def soonest_start(reload=false)
Chris@1115 898 @soonest_start = nil if reload
Chris@0 899 @soonest_start ||= (
Chris@1115 900 relations_to(reload).collect{|relation| relation.successor_soonest_start} +
Chris@1295 901 [(@parent_issue || parent).try(:soonest_start)]
Chris@0 902 ).compact.max
Chris@0 903 end
Chris@441 904
Chris@1115 905 # Sets start_date on the given date or the next working day
Chris@1115 906 # and changes due_date to keep the same working duration.
Chris@1115 907 def reschedule_on(date)
Chris@1115 908 wd = working_duration
Chris@1115 909 date = next_working_date(date)
Chris@1115 910 self.start_date = date
Chris@1115 911 self.due_date = add_working_days(date, wd)
Chris@1115 912 end
Chris@1115 913
Chris@1115 914 # Reschedules the issue on the given date or the next working day and saves the record.
Chris@1115 915 # If the issue is a parent task, this is done by rescheduling its subtasks.
Chris@1115 916 def reschedule_on!(date)
Chris@0 917 return if date.nil?
Chris@0 918 if leaf?
Chris@1115 919 if start_date.nil? || start_date != date
Chris@1115 920 if start_date && start_date > date
Chris@1115 921 # Issue can not be moved earlier than its soonest start date
Chris@1115 922 date = [soonest_start(true), date].compact.max
Chris@1115 923 end
Chris@1115 924 reschedule_on(date)
Chris@1115 925 begin
Chris@1115 926 save
Chris@1115 927 rescue ActiveRecord::StaleObjectError
Chris@1115 928 reload
Chris@1115 929 reschedule_on(date)
Chris@1115 930 save
Chris@1115 931 end
Chris@0 932 end
Chris@0 933 else
Chris@0 934 leaves.each do |leaf|
Chris@1115 935 if leaf.start_date
Chris@1115 936 # Only move subtask if it starts at the same date as the parent
Chris@1115 937 # or if it starts before the given date
Chris@1115 938 if start_date == leaf.start_date || date > leaf.start_date
Chris@1115 939 leaf.reschedule_on!(date)
Chris@1115 940 end
Chris@1115 941 else
Chris@1115 942 leaf.reschedule_on!(date)
Chris@1115 943 end
Chris@0 944 end
Chris@0 945 end
Chris@0 946 end
Chris@441 947
Chris@0 948 def <=>(issue)
Chris@0 949 if issue.nil?
Chris@0 950 -1
Chris@0 951 elsif root_id != issue.root_id
Chris@0 952 (root_id || 0) <=> (issue.root_id || 0)
Chris@0 953 else
Chris@0 954 (lft || 0) <=> (issue.lft || 0)
Chris@0 955 end
Chris@0 956 end
Chris@441 957
Chris@0 958 def to_s
Chris@0 959 "#{tracker} ##{id}: #{subject}"
Chris@0 960 end
Chris@441 961
Chris@0 962 # Returns a string of css classes that apply to the issue
Chris@0 963 def css_classes
Chris@1295 964 s = "issue tracker-#{tracker_id} status-#{status_id} #{priority.try(:css_classes)}"
Chris@0 965 s << ' closed' if closed?
Chris@0 966 s << ' overdue' if overdue?
Chris@441 967 s << ' child' if child?
Chris@441 968 s << ' parent' unless leaf?
Chris@441 969 s << ' private' if is_private?
Chris@0 970 s << ' created-by-me' if User.current.logged? && author_id == User.current.id
Chris@0 971 s << ' assigned-to-me' if User.current.logged? && assigned_to_id == User.current.id
Chris@0 972 s
Chris@0 973 end
Chris@0 974
Chris@1115 975 # Saves an issue and a time_entry from the parameters
Chris@0 976 def save_issue_with_child_records(params, existing_time_entry=nil)
Chris@0 977 Issue.transaction do
Chris@441 978 if params[:time_entry] && (params[:time_entry][:hours].present? || params[:time_entry][:comments].present?) && User.current.allowed_to?(:log_time, project)
Chris@0 979 @time_entry = existing_time_entry || TimeEntry.new
Chris@0 980 @time_entry.project = project
Chris@0 981 @time_entry.issue = self
Chris@0 982 @time_entry.user = User.current
Chris@909 983 @time_entry.spent_on = User.current.today
Chris@0 984 @time_entry.attributes = params[:time_entry]
Chris@0 985 self.time_entries << @time_entry
Chris@0 986 end
Chris@441 987
Chris@1115 988 # TODO: Rename hook
Chris@1115 989 Redmine::Hook.call_hook(:controller_issues_edit_before_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal})
Chris@1115 990 if save
Chris@0 991 # TODO: Rename hook
Chris@1115 992 Redmine::Hook.call_hook(:controller_issues_edit_after_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal})
Chris@1115 993 else
Chris@1115 994 raise ActiveRecord::Rollback
Chris@0 995 end
Chris@0 996 end
Chris@0 997 end
Chris@0 998
Chris@0 999 # Unassigns issues from +version+ if it's no longer shared with issue's project
Chris@0 1000 def self.update_versions_from_sharing_change(version)
Chris@0 1001 # Update issues assigned to the version
Chris@0 1002 update_versions(["#{Issue.table_name}.fixed_version_id = ?", version.id])
Chris@0 1003 end
Chris@441 1004
Chris@0 1005 # Unassigns issues from versions that are no longer shared
Chris@0 1006 # after +project+ was moved
Chris@0 1007 def self.update_versions_from_hierarchy_change(project)
Chris@0 1008 moved_project_ids = project.self_and_descendants.reload.collect(&:id)
Chris@0 1009 # Update issues of the moved projects and issues assigned to a version of a moved project
Chris@0 1010 Issue.update_versions(["#{Version.table_name}.project_id IN (?) OR #{Issue.table_name}.project_id IN (?)", moved_project_ids, moved_project_ids])
Chris@0 1011 end
Chris@0 1012
Chris@0 1013 def parent_issue_id=(arg)
Chris@1115 1014 s = arg.to_s.strip.presence
Chris@1115 1015 if s && (m = s.match(%r{\A#?(\d+)\z})) && (@parent_issue = Issue.find_by_id(m[1]))
Chris@0 1016 @parent_issue.id
Chris@0 1017 else
Chris@0 1018 @parent_issue = nil
Chris@1115 1019 @invalid_parent_issue_id = arg
Chris@0 1020 end
Chris@0 1021 end
Chris@441 1022
Chris@0 1023 def parent_issue_id
Chris@1115 1024 if @invalid_parent_issue_id
Chris@1115 1025 @invalid_parent_issue_id
Chris@1115 1026 elsif instance_variable_defined? :@parent_issue
Chris@0 1027 @parent_issue.nil? ? nil : @parent_issue.id
Chris@0 1028 else
Chris@0 1029 parent_id
Chris@0 1030 end
Chris@0 1031 end
Chris@0 1032
Chris@1295 1033 # Returns true if issue's project is a valid
Chris@1295 1034 # parent issue project
Chris@1115 1035 def valid_parent_project?(issue=parent)
Chris@1115 1036 return true if issue.nil? || issue.project_id == project_id
Chris@1115 1037
Chris@1115 1038 case Setting.cross_project_subtasks
Chris@1115 1039 when 'system'
Chris@1115 1040 true
Chris@1115 1041 when 'tree'
Chris@1115 1042 issue.project.root == project.root
Chris@1115 1043 when 'hierarchy'
Chris@1115 1044 issue.project.is_or_is_ancestor_of?(project) || issue.project.is_descendant_of?(project)
Chris@1115 1045 when 'descendants'
Chris@1115 1046 issue.project.is_or_is_ancestor_of?(project)
Chris@1115 1047 else
Chris@1115 1048 false
Chris@1115 1049 end
Chris@1115 1050 end
Chris@1115 1051
Chris@0 1052 # Extracted from the ReportsController.
Chris@0 1053 def self.by_tracker(project)
Chris@0 1054 count_and_group_by(:project => project,
Chris@0 1055 :field => 'tracker_id',
Chris@0 1056 :joins => Tracker.table_name)
Chris@0 1057 end
Chris@0 1058
Chris@0 1059 def self.by_version(project)
Chris@0 1060 count_and_group_by(:project => project,
Chris@0 1061 :field => 'fixed_version_id',
Chris@0 1062 :joins => Version.table_name)
Chris@0 1063 end
Chris@0 1064
Chris@0 1065 def self.by_priority(project)
Chris@0 1066 count_and_group_by(:project => project,
Chris@0 1067 :field => 'priority_id',
Chris@0 1068 :joins => IssuePriority.table_name)
Chris@0 1069 end
Chris@0 1070
Chris@0 1071 def self.by_category(project)
Chris@0 1072 count_and_group_by(:project => project,
Chris@0 1073 :field => 'category_id',
Chris@0 1074 :joins => IssueCategory.table_name)
Chris@0 1075 end
Chris@0 1076
Chris@0 1077 def self.by_assigned_to(project)
Chris@0 1078 count_and_group_by(:project => project,
Chris@0 1079 :field => 'assigned_to_id',
Chris@0 1080 :joins => User.table_name)
Chris@0 1081 end
Chris@0 1082
Chris@0 1083 def self.by_author(project)
Chris@0 1084 count_and_group_by(:project => project,
Chris@0 1085 :field => 'author_id',
Chris@0 1086 :joins => User.table_name)
Chris@0 1087 end
Chris@0 1088
Chris@0 1089 def self.by_subproject(project)
Chris@0 1090 ActiveRecord::Base.connection.select_all("select s.id as status_id,
Chris@0 1091 s.is_closed as closed,
Chris@441 1092 #{Issue.table_name}.project_id as project_id,
Chris@441 1093 count(#{Issue.table_name}.id) as total
Chris@0 1094 from
Chris@441 1095 #{Issue.table_name}, #{Project.table_name}, #{IssueStatus.table_name} s
Chris@0 1096 where
Chris@441 1097 #{Issue.table_name}.status_id=s.id
Chris@441 1098 and #{Issue.table_name}.project_id = #{Project.table_name}.id
Chris@441 1099 and #{visible_condition(User.current, :project => project, :with_subprojects => true)}
Chris@441 1100 and #{Issue.table_name}.project_id <> #{project.id}
Chris@441 1101 group by s.id, s.is_closed, #{Issue.table_name}.project_id") if project.descendants.active.any?
Chris@0 1102 end
Chris@0 1103 # End ReportsController extraction
Chris@441 1104
Chris@1115 1105 # Returns an array of projects that user can assign the issue to
Chris@1115 1106 def allowed_target_projects(user=User.current)
Chris@1115 1107 if new_record?
Chris@1115 1108 Project.all(:conditions => Project.allowed_to_condition(user, :add_issues))
Chris@1115 1109 else
Chris@1115 1110 self.class.allowed_target_projects_on_move(user)
Chris@0 1111 end
Chris@1115 1112 end
Chris@1115 1113
Chris@1115 1114 # Returns an array of projects that user can move issues to
Chris@1115 1115 def self.allowed_target_projects_on_move(user=User.current)
Chris@1115 1116 Project.all(:conditions => Project.allowed_to_condition(user, :move_issues))
Chris@0 1117 end
Chris@441 1118
Chris@0 1119 private
Chris@441 1120
Chris@1115 1121 def after_project_change
Chris@1115 1122 # Update project_id on related time entries
Chris@1115 1123 TimeEntry.update_all(["project_id = ?", project_id], {:issue_id => id})
Chris@1115 1124
Chris@1115 1125 # Delete issue relations
Chris@1115 1126 unless Setting.cross_project_issue_relations?
Chris@1115 1127 relations_from.clear
Chris@1115 1128 relations_to.clear
Chris@1115 1129 end
Chris@1115 1130
Chris@1115 1131 # Move subtasks that were in the same project
Chris@1115 1132 children.each do |child|
Chris@1115 1133 next unless child.project_id == project_id_was
Chris@1115 1134 # Change project and keep project
Chris@1115 1135 child.send :project=, project, true
Chris@1115 1136 unless child.save
Chris@1115 1137 raise ActiveRecord::Rollback
Chris@1115 1138 end
Chris@1115 1139 end
Chris@1115 1140 end
Chris@1115 1141
Chris@1115 1142 # Callback for after the creation of an issue by copy
Chris@1115 1143 # * adds a "copied to" relation with the copied issue
Chris@1115 1144 # * copies subtasks from the copied issue
Chris@1115 1145 def after_create_from_copy
Chris@1115 1146 return unless copy? && !@after_create_from_copy_handled
Chris@1115 1147
Chris@1115 1148 if (@copied_from.project_id == project_id || Setting.cross_project_issue_relations?) && @copy_options[:link] != false
Chris@1115 1149 relation = IssueRelation.new(:issue_from => @copied_from, :issue_to => self, :relation_type => IssueRelation::TYPE_COPIED_TO)
Chris@1115 1150 unless relation.save
Chris@1115 1151 logger.error "Could not create relation while copying ##{@copied_from.id} to ##{id} due to validation errors: #{relation.errors.full_messages.join(', ')}" if logger
Chris@1115 1152 end
Chris@1115 1153 end
Chris@1115 1154
Chris@1115 1155 unless @copied_from.leaf? || @copy_options[:subtasks] == false
Chris@1295 1156 copy_options = (@copy_options || {}).merge(:subtasks => false)
Chris@1295 1157 copied_issue_ids = {@copied_from.id => self.id}
Chris@1295 1158 @copied_from.reload.descendants.reorder("#{Issue.table_name}.lft").each do |child|
Chris@1295 1159 # Do not copy self when copying an issue as a descendant of the copied issue
Chris@1295 1160 next if child == self
Chris@1295 1161 # Do not copy subtasks of issues that were not copied
Chris@1295 1162 next unless copied_issue_ids[child.parent_id]
Chris@1295 1163 # Do not copy subtasks that are not visible to avoid potential disclosure of private data
Chris@1115 1164 unless child.visible?
Chris@1115 1165 logger.error "Subtask ##{child.id} was not copied during ##{@copied_from.id} copy because it is not visible to the current user" if logger
Chris@1115 1166 next
Chris@1115 1167 end
Chris@1295 1168 copy = Issue.new.copy_from(child, copy_options)
Chris@1115 1169 copy.author = author
Chris@1115 1170 copy.project = project
Chris@1295 1171 copy.parent_issue_id = copied_issue_ids[child.parent_id]
Chris@1115 1172 unless copy.save
Chris@1115 1173 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
Chris@1295 1174 next
Chris@1115 1175 end
Chris@1295 1176 copied_issue_ids[child.id] = copy.id
Chris@1115 1177 end
Chris@1115 1178 end
Chris@1115 1179 @after_create_from_copy_handled = true
Chris@1115 1180 end
Chris@1115 1181
Chris@0 1182 def update_nested_set_attributes
Chris@0 1183 if root_id.nil?
Chris@0 1184 # issue was just created
Chris@0 1185 self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id)
Chris@0 1186 set_default_left_and_right
Chris@0 1187 Issue.update_all("root_id = #{root_id}, lft = #{lft}, rgt = #{rgt}", ["id = ?", id])
Chris@0 1188 if @parent_issue
Chris@0 1189 move_to_child_of(@parent_issue)
Chris@0 1190 end
Chris@0 1191 reload
Chris@0 1192 elsif parent_issue_id != parent_id
Chris@0 1193 former_parent_id = parent_id
Chris@0 1194 # moving an existing issue
Chris@0 1195 if @parent_issue && @parent_issue.root_id == root_id
Chris@0 1196 # inside the same tree
Chris@0 1197 move_to_child_of(@parent_issue)
Chris@0 1198 else
Chris@0 1199 # to another tree
Chris@0 1200 unless root?
Chris@0 1201 move_to_right_of(root)
Chris@0 1202 reload
Chris@0 1203 end
Chris@0 1204 old_root_id = root_id
Chris@0 1205 self.root_id = (@parent_issue.nil? ? id : @parent_issue.root_id )
Chris@0 1206 target_maxright = nested_set_scope.maximum(right_column_name) || 0
Chris@0 1207 offset = target_maxright + 1 - lft
Chris@0 1208 Issue.update_all("root_id = #{root_id}, lft = lft + #{offset}, rgt = rgt + #{offset}",
Chris@0 1209 ["root_id = ? AND lft >= ? AND rgt <= ? ", old_root_id, lft, rgt])
Chris@0 1210 self[left_column_name] = lft + offset
Chris@0 1211 self[right_column_name] = rgt + offset
Chris@0 1212 if @parent_issue
Chris@0 1213 move_to_child_of(@parent_issue)
Chris@0 1214 end
Chris@0 1215 end
Chris@0 1216 reload
Chris@0 1217 # delete invalid relations of all descendants
Chris@0 1218 self_and_descendants.each do |issue|
Chris@0 1219 issue.relations.each do |relation|
Chris@0 1220 relation.destroy unless relation.valid?
Chris@0 1221 end
Chris@0 1222 end
Chris@0 1223 # update former parent
Chris@0 1224 recalculate_attributes_for(former_parent_id) if former_parent_id
Chris@0 1225 end
Chris@0 1226 remove_instance_variable(:@parent_issue) if instance_variable_defined?(:@parent_issue)
Chris@0 1227 end
Chris@441 1228
Chris@0 1229 def update_parent_attributes
Chris@0 1230 recalculate_attributes_for(parent_id) if parent_id
Chris@0 1231 end
Chris@0 1232
Chris@0 1233 def recalculate_attributes_for(issue_id)
Chris@0 1234 if issue_id && p = Issue.find_by_id(issue_id)
Chris@0 1235 # priority = highest priority of children
Chris@1115 1236 if priority_position = p.children.maximum("#{IssuePriority.table_name}.position", :joins => :priority)
Chris@0 1237 p.priority = IssuePriority.find_by_position(priority_position)
Chris@0 1238 end
Chris@441 1239
Chris@0 1240 # start/due dates = lowest/highest dates of children
Chris@0 1241 p.start_date = p.children.minimum(:start_date)
Chris@0 1242 p.due_date = p.children.maximum(:due_date)
Chris@0 1243 if p.start_date && p.due_date && p.due_date < p.start_date
Chris@0 1244 p.start_date, p.due_date = p.due_date, p.start_date
Chris@0 1245 end
Chris@441 1246
Chris@0 1247 # done ratio = weighted average ratio of leaves
chris@37 1248 unless Issue.use_status_for_done_ratio? && p.status && p.status.default_done_ratio
Chris@0 1249 leaves_count = p.leaves.count
Chris@0 1250 if leaves_count > 0
Chris@0 1251 average = p.leaves.average(:estimated_hours).to_f
Chris@0 1252 if average == 0
Chris@0 1253 average = 1
Chris@0 1254 end
Chris@1115 1255 done = p.leaves.sum("COALESCE(estimated_hours, #{average}) * (CASE WHEN is_closed = #{connection.quoted_true} THEN 100 ELSE COALESCE(done_ratio, 0) END)", :joins => :status).to_f
Chris@0 1256 progress = done / (average * leaves_count)
Chris@0 1257 p.done_ratio = progress.round
Chris@0 1258 end
Chris@0 1259 end
Chris@441 1260
Chris@0 1261 # estimate = sum of leaves estimates
Chris@0 1262 p.estimated_hours = p.leaves.sum(:estimated_hours).to_f
Chris@0 1263 p.estimated_hours = nil if p.estimated_hours == 0.0
Chris@441 1264
Chris@0 1265 # ancestors will be recursively updated
Chris@1115 1266 p.save(:validate => false)
Chris@0 1267 end
Chris@0 1268 end
Chris@441 1269
Chris@0 1270 # Update issues so their versions are not pointing to a
Chris@0 1271 # fixed_version that is not shared with the issue's project
Chris@0 1272 def self.update_versions(conditions=nil)
Chris@0 1273 # Only need to update issues with a fixed_version from
Chris@0 1274 # a different project and that is not systemwide shared
Chris@1115 1275 Issue.scoped(:conditions => conditions).all(
Chris@1115 1276 :conditions => "#{Issue.table_name}.fixed_version_id IS NOT NULL" +
Chris@1115 1277 " AND #{Issue.table_name}.project_id <> #{Version.table_name}.project_id" +
Chris@1115 1278 " AND #{Version.table_name}.sharing <> 'system'",
Chris@1115 1279 :include => [:project, :fixed_version]
Chris@1115 1280 ).each do |issue|
Chris@0 1281 next if issue.project.nil? || issue.fixed_version.nil?
Chris@0 1282 unless issue.project.shared_versions.include?(issue.fixed_version)
Chris@0 1283 issue.init_journal(User.current)
Chris@0 1284 issue.fixed_version = nil
Chris@0 1285 issue.save
Chris@0 1286 end
Chris@0 1287 end
Chris@0 1288 end
Chris@441 1289
Chris@1115 1290 # Callback on file attachment
Chris@909 1291 def attachment_added(obj)
Chris@909 1292 if @current_journal && !obj.new_record?
Chris@909 1293 @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => obj.id, :value => obj.filename)
Chris@909 1294 end
Chris@909 1295 end
Chris@909 1296
Chris@909 1297 # Callback on attachment deletion
Chris@0 1298 def attachment_removed(obj)
Chris@1115 1299 if @current_journal && !obj.new_record?
Chris@1115 1300 @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => obj.id, :old_value => obj.filename)
Chris@1115 1301 @current_journal.save
Chris@1115 1302 end
Chris@0 1303 end
Chris@441 1304
Chris@0 1305 # Default assignment based on category
Chris@0 1306 def default_assign
Chris@0 1307 if assigned_to.nil? && category && category.assigned_to
Chris@0 1308 self.assigned_to = category.assigned_to
Chris@0 1309 end
Chris@0 1310 end
Chris@0 1311
Chris@0 1312 # Updates start/due dates of following issues
Chris@0 1313 def reschedule_following_issues
Chris@0 1314 if start_date_changed? || due_date_changed?
Chris@0 1315 relations_from.each do |relation|
Chris@0 1316 relation.set_issue_to_dates
Chris@0 1317 end
Chris@0 1318 end
Chris@0 1319 end
Chris@0 1320
Chris@0 1321 # Closes duplicates if the issue is being closed
Chris@0 1322 def close_duplicates
Chris@0 1323 if closing?
Chris@0 1324 duplicates.each do |duplicate|
Chris@0 1325 # Reload is need in case the duplicate was updated by a previous duplicate
Chris@0 1326 duplicate.reload
Chris@0 1327 # Don't re-close it if it's already closed
Chris@0 1328 next if duplicate.closed?
Chris@0 1329 # Same user and notes
Chris@0 1330 if @current_journal
Chris@0 1331 duplicate.init_journal(@current_journal.user, @current_journal.notes)
Chris@0 1332 end
Chris@0 1333 duplicate.update_attribute :status, self.status
Chris@0 1334 end
Chris@0 1335 end
Chris@0 1336 end
Chris@441 1337
Chris@1295 1338 # Make sure updated_on is updated when adding a note and set updated_on now
Chris@1295 1339 # so we can set closed_on with the same value on closing
Chris@1115 1340 def force_updated_on_change
Chris@1295 1341 if @current_journal || changed?
Chris@1115 1342 self.updated_on = current_time_from_proper_timezone
Chris@1295 1343 if new_record?
Chris@1295 1344 self.created_on = updated_on
Chris@1295 1345 end
Chris@1295 1346 end
Chris@1295 1347 end
Chris@1295 1348
Chris@1295 1349 # Callback for setting closed_on when the issue is closed.
Chris@1295 1350 # The closed_on attribute stores the time of the last closing
Chris@1295 1351 # and is preserved when the issue is reopened.
Chris@1295 1352 def update_closed_on
Chris@1295 1353 if closing? || (new_record? && closed?)
Chris@1295 1354 self.closed_on = updated_on
Chris@1115 1355 end
Chris@1115 1356 end
Chris@1115 1357
Chris@0 1358 # Saves the changes in a Journal
Chris@0 1359 # Called after_save
Chris@0 1360 def create_journal
Chris@0 1361 if @current_journal
Chris@0 1362 # attributes changes
Chris@1115 1363 if @attributes_before_change
Chris@1295 1364 (Issue.column_names - %w(id root_id lft rgt lock_version created_on updated_on closed_on)).each {|c|
Chris@1115 1365 before = @attributes_before_change[c]
Chris@1115 1366 after = send(c)
Chris@1115 1367 next if before == after || (before.blank? && after.blank?)
Chris@1115 1368 @current_journal.details << JournalDetail.new(:property => 'attr',
Chris@1115 1369 :prop_key => c,
Chris@1115 1370 :old_value => before,
Chris@1115 1371 :value => after)
Chris@1115 1372 }
Chris@1115 1373 end
Chris@1115 1374 if @custom_values_before_change
Chris@1115 1375 # custom fields changes
Chris@1115 1376 custom_field_values.each {|c|
Chris@1115 1377 before = @custom_values_before_change[c.custom_field_id]
Chris@1115 1378 after = c.value
Chris@1115 1379 next if before == after || (before.blank? && after.blank?)
Chris@1115 1380
Chris@1115 1381 if before.is_a?(Array) || after.is_a?(Array)
Chris@1115 1382 before = [before] unless before.is_a?(Array)
Chris@1115 1383 after = [after] unless after.is_a?(Array)
Chris@1115 1384
Chris@1115 1385 # values removed
Chris@1115 1386 (before - after).reject(&:blank?).each do |value|
Chris@1115 1387 @current_journal.details << JournalDetail.new(:property => 'cf',
Chris@1115 1388 :prop_key => c.custom_field_id,
Chris@1115 1389 :old_value => value,
Chris@1115 1390 :value => nil)
Chris@1115 1391 end
Chris@1115 1392 # values added
Chris@1115 1393 (after - before).reject(&:blank?).each do |value|
Chris@1115 1394 @current_journal.details << JournalDetail.new(:property => 'cf',
Chris@1115 1395 :prop_key => c.custom_field_id,
Chris@1115 1396 :old_value => nil,
Chris@1115 1397 :value => value)
Chris@1115 1398 end
Chris@1115 1399 else
Chris@1115 1400 @current_journal.details << JournalDetail.new(:property => 'cf',
Chris@1115 1401 :prop_key => c.custom_field_id,
Chris@1115 1402 :old_value => before,
Chris@1115 1403 :value => after)
Chris@1115 1404 end
Chris@1115 1405 }
Chris@1115 1406 end
Chris@0 1407 @current_journal.save
Chris@0 1408 # reset current journal
Chris@0 1409 init_journal @current_journal.user, @current_journal.notes
Chris@0 1410 end
Chris@0 1411 end
Chris@0 1412
Chris@0 1413 # Query generator for selecting groups of issue counts for a project
Chris@0 1414 # based on specific criteria
Chris@0 1415 #
Chris@0 1416 # Options
Chris@0 1417 # * project - Project to search in.
Chris@0 1418 # * field - String. Issue field to key off of in the grouping.
Chris@0 1419 # * joins - String. The table name to join against.
Chris@0 1420 def self.count_and_group_by(options)
Chris@0 1421 project = options.delete(:project)
Chris@0 1422 select_field = options.delete(:field)
Chris@0 1423 joins = options.delete(:joins)
Chris@0 1424
Chris@441 1425 where = "#{Issue.table_name}.#{select_field}=j.id"
Chris@441 1426
Chris@0 1427 ActiveRecord::Base.connection.select_all("select s.id as status_id,
Chris@0 1428 s.is_closed as closed,
Chris@0 1429 j.id as #{select_field},
Chris@441 1430 count(#{Issue.table_name}.id) as total
Chris@0 1431 from
Chris@441 1432 #{Issue.table_name}, #{Project.table_name}, #{IssueStatus.table_name} s, #{joins} j
Chris@0 1433 where
Chris@441 1434 #{Issue.table_name}.status_id=s.id
Chris@0 1435 and #{where}
Chris@441 1436 and #{Issue.table_name}.project_id=#{Project.table_name}.id
Chris@441 1437 and #{visible_condition(User.current, :project => project)}
Chris@0 1438 group by s.id, s.is_closed, j.id")
Chris@0 1439 end
Chris@0 1440 end