annotate app/models/project.rb @ 1296:038ba2d95de8 redmine-2.2

Fix redmine-2.2 branch update (add missing svn files)
author Chris Cannam
date Fri, 14 Jun 2013 09:05:06 +0100
parents 3e4c3460b6ca
children 622f24f53b42 0a574315af3e 261b3d9a4903
rev   line source
Chris@441 1 # Redmine - project management software
Chris@1115 2 # Copyright (C) 2006-2012 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@909 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@909 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 Project < ActiveRecord::Base
Chris@119 19 include Redmine::SafeAttributes
Chris@909 20
Chris@0 21 # Project statuses
Chris@0 22 STATUS_ACTIVE = 1
Chris@1115 23 STATUS_CLOSED = 5
Chris@0 24 STATUS_ARCHIVED = 9
Chris@909 25
chris@37 26 # Maximum length for project identifiers
chris@37 27 IDENTIFIER_MAX_LENGTH = 100
Chris@909 28
Chris@0 29 # Specific overidden Activities
Chris@0 30 has_many :time_entry_activities
Chris@1115 31 has_many :members, :include => [:principal, :roles], :conditions => "#{User.table_name}.type='User' AND #{User.table_name}.status=#{User::STATUS_ACTIVE}"
Chris@0 32 has_many :memberships, :class_name => 'Member'
Chris@909 33 has_many :member_principals, :class_name => 'Member',
Chris@0 34 :include => :principal,
Chris@0 35 :conditions => "#{Principal.table_name}.type='Group' OR (#{Principal.table_name}.type='User' AND #{Principal.table_name}.status=#{User::STATUS_ACTIVE})"
Chris@0 36 has_many :users, :through => :members
Chris@0 37 has_many :principals, :through => :member_principals, :source => :principal
Chris@909 38
Chris@0 39 has_many :enabled_modules, :dependent => :delete_all
Chris@0 40 has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position"
Chris@1115 41 has_many :issues, :dependent => :destroy, :include => [:status, :tracker]
Chris@0 42 has_many :issue_changes, :through => :issues, :source => :journals
Chris@0 43 has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC"
Chris@0 44 has_many :time_entries, :dependent => :delete_all
Chris@0 45 has_many :queries, :dependent => :delete_all
Chris@0 46 has_many :documents, :dependent => :destroy
Chris@441 47 has_many :news, :dependent => :destroy, :include => :author
Chris@0 48 has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name"
Chris@0 49 has_many :boards, :dependent => :destroy, :order => "position ASC"
Chris@1115 50 has_one :repository, :conditions => ["is_default = ?", true]
Chris@1115 51 has_many :repositories, :dependent => :destroy
Chris@0 52 has_many :changesets, :through => :repository
Chris@0 53 has_one :wiki, :dependent => :destroy
Chris@0 54 # Custom field for the project issues
Chris@909 55 has_and_belongs_to_many :issue_custom_fields,
Chris@0 56 :class_name => 'IssueCustomField',
Chris@0 57 :order => "#{CustomField.table_name}.position",
Chris@0 58 :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}",
Chris@0 59 :association_foreign_key => 'custom_field_id'
Chris@909 60
Chris@441 61 acts_as_nested_set :order => 'name', :dependent => :destroy
Chris@0 62 acts_as_attachable :view_permission => :view_files,
Chris@0 63 :delete_permission => :manage_files
Chris@0 64
Chris@0 65 acts_as_customizable
Chris@0 66 acts_as_searchable :columns => ['name', 'identifier', 'description'], :project_key => 'id', :permission => nil
Chris@0 67 acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"},
Chris@0 68 :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o}},
Chris@0 69 :author => nil
Chris@0 70
Chris@119 71 attr_protected :status
Chris@909 72
Chris@0 73 validates_presence_of :name, :identifier
chris@37 74 validates_uniqueness_of :identifier
Chris@0 75 validates_associated :repository, :wiki
chris@37 76 validates_length_of :name, :maximum => 255
Chris@0 77 validates_length_of :homepage, :maximum => 255
chris@37 78 validates_length_of :identifier, :in => 1..IDENTIFIER_MAX_LENGTH
Chris@0 79 # donwcase letters, digits, dashes but not digits only
Chris@1115 80 validates_format_of :identifier, :with => /^(?!\d+$)[a-z0-9\-_]*$/, :if => Proc.new { |p| p.identifier_changed? }
Chris@0 81 # reserved words
Chris@0 82 validates_exclusion_of :identifier, :in => %w( new )
Chris@0 83
Chris@1115 84 after_save :update_position_under_parent, :if => Proc.new {|project| project.name_changed?}
Chris@441 85 before_destroy :delete_all_members
Chris@0 86
Chris@1115 87 scope :has_module, lambda { |mod| { :conditions => ["#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s] } }
Chris@1115 88 scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"}
Chris@1115 89 scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
Chris@1115 90 scope :all_public, { :conditions => { :is_public => true } }
Chris@1115 91 scope :visible, lambda {|*args| {:conditions => Project.visible_condition(args.shift || User.current, *args) }}
Chris@1115 92 scope :allowed_to, lambda {|*args|
Chris@1115 93 user = User.current
Chris@1115 94 permission = nil
Chris@1115 95 if args.first.is_a?(Symbol)
Chris@1115 96 permission = args.shift
Chris@1115 97 else
Chris@1115 98 user = args.shift
Chris@1115 99 permission = args.shift
Chris@1115 100 end
Chris@1115 101 { :conditions => Project.allowed_to_condition(user, permission, *args) }
Chris@1115 102 }
Chris@1115 103 scope :like, lambda {|arg|
Chris@1115 104 if arg.blank?
Chris@1115 105 {}
Chris@1115 106 else
Chris@1115 107 pattern = "%#{arg.to_s.strip.downcase}%"
Chris@1115 108 {:conditions => ["LOWER(identifier) LIKE :p OR LOWER(name) LIKE :p", {:p => pattern}]}
Chris@1115 109 end
Chris@1115 110 }
Chris@909 111
Chris@1115 112 def initialize(attributes=nil, *args)
Chris@119 113 super
Chris@909 114
Chris@119 115 initialized = (attributes || {}).stringify_keys
Chris@909 116 if !initialized.key?('identifier') && Setting.sequential_project_identifiers?
Chris@119 117 self.identifier = Project.next_identifier
Chris@119 118 end
Chris@119 119 if !initialized.key?('is_public')
Chris@119 120 self.is_public = Setting.default_projects_public?
Chris@119 121 end
Chris@119 122 if !initialized.key?('enabled_module_names')
Chris@119 123 self.enabled_module_names = Setting.default_projects_modules
Chris@119 124 end
Chris@119 125 if !initialized.key?('trackers') && !initialized.key?('tracker_ids')
Chris@1115 126 self.trackers = Tracker.sorted.all
Chris@119 127 end
Chris@119 128 end
Chris@909 129
Chris@0 130 def identifier=(identifier)
Chris@0 131 super unless identifier_frozen?
Chris@0 132 end
Chris@909 133
Chris@0 134 def identifier_frozen?
Chris@1115 135 errors[:identifier].blank? && !(new_record? || identifier.blank?)
Chris@0 136 end
Chris@0 137
Chris@0 138 # returns latest created projects
Chris@0 139 # non public projects will be returned only if user is a member of those
Chris@0 140 def self.latest(user=nil, count=5)
Chris@441 141 visible(user).find(:all, :limit => count, :order => "created_on DESC")
Chris@0 142 end
Chris@0 143
Chris@507 144 # Returns true if the project is visible to +user+ or to the current user.
Chris@507 145 def visible?(user=User.current)
Chris@507 146 user.allowed_to?(:view_project, self)
Chris@507 147 end
Chris@909 148
Chris@441 149 # Returns a SQL conditions string used to find all projects visible by the specified user.
Chris@0 150 #
Chris@0 151 # Examples:
Chris@441 152 # Project.visible_condition(admin) => "projects.status = 1"
Chris@441 153 # Project.visible_condition(normal_user) => "((projects.status = 1) AND (projects.is_public = 1 OR projects.id IN (1,3,4)))"
Chris@441 154 # Project.visible_condition(anonymous) => "((projects.status = 1) AND (projects.is_public = 1))"
Chris@441 155 def self.visible_condition(user, options={})
Chris@441 156 allowed_to_condition(user, :view_project, options)
Chris@0 157 end
Chris@909 158
Chris@441 159 # Returns a SQL conditions string used to find all projects for which +user+ has the given +permission+
Chris@441 160 #
Chris@441 161 # Valid options:
Chris@441 162 # * :project => limit the condition to project
Chris@441 163 # * :with_subprojects => limit the condition to project and its subprojects
Chris@441 164 # * :member => limit the condition to the user projects
Chris@0 165 def self.allowed_to_condition(user, permission, options={})
Chris@1115 166 perm = Redmine::AccessControl.permission(permission)
Chris@1115 167 base_statement = (perm && perm.read? ? "#{Project.table_name}.status <> #{Project::STATUS_ARCHIVED}" : "#{Project.table_name}.status = #{Project::STATUS_ACTIVE}")
Chris@1115 168 if perm && perm.project_module
Chris@1115 169 # If the permission belongs to a project module, make sure the module is enabled
Chris@1115 170 base_statement << " AND #{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}')"
Chris@0 171 end
Chris@0 172 if options[:project]
Chris@0 173 project_statement = "#{Project.table_name}.id = #{options[:project].id}"
Chris@0 174 project_statement << " OR (#{Project.table_name}.lft > #{options[:project].lft} AND #{Project.table_name}.rgt < #{options[:project].rgt})" if options[:with_subprojects]
Chris@0 175 base_statement = "(#{project_statement}) AND (#{base_statement})"
Chris@0 176 end
Chris@909 177
Chris@0 178 if user.admin?
Chris@441 179 base_statement
Chris@0 180 else
Chris@441 181 statement_by_role = {}
Chris@441 182 unless options[:member]
Chris@441 183 role = user.logged? ? Role.non_member : Role.anonymous
Chris@441 184 if role.allowed_to?(permission)
Chris@441 185 statement_by_role[role] = "#{Project.table_name}.is_public = #{connection.quoted_true}"
Chris@441 186 end
Chris@441 187 end
Chris@0 188 if user.logged?
Chris@441 189 user.projects_by_role.each do |role, projects|
Chris@1115 190 if role.allowed_to?(permission) && projects.any?
Chris@441 191 statement_by_role[role] = "#{Project.table_name}.id IN (#{projects.collect(&:id).join(',')})"
Chris@441 192 end
Chris@0 193 end
Chris@441 194 end
Chris@441 195 if statement_by_role.empty?
Chris@441 196 "1=0"
Chris@0 197 else
Chris@441 198 if block_given?
Chris@441 199 statement_by_role.each do |role, statement|
Chris@441 200 if s = yield(role, user)
Chris@441 201 statement_by_role[role] = "(#{statement} AND (#{s}))"
Chris@441 202 end
Chris@441 203 end
Chris@441 204 end
Chris@441 205 "((#{base_statement}) AND (#{statement_by_role.values.join(' OR ')}))"
Chris@0 206 end
Chris@0 207 end
Chris@0 208 end
Chris@0 209
Chris@0 210 # Returns the Systemwide and project specific activities
Chris@0 211 def activities(include_inactive=false)
Chris@0 212 if include_inactive
Chris@0 213 return all_activities
Chris@0 214 else
Chris@0 215 return active_activities
Chris@0 216 end
Chris@0 217 end
Chris@0 218
Chris@0 219 # Will create a new Project specific Activity or update an existing one
Chris@0 220 #
Chris@0 221 # This will raise a ActiveRecord::Rollback if the TimeEntryActivity
Chris@0 222 # does not successfully save.
Chris@0 223 def update_or_create_time_entry_activity(id, activity_hash)
Chris@0 224 if activity_hash.respond_to?(:has_key?) && activity_hash.has_key?('parent_id')
Chris@0 225 self.create_time_entry_activity_if_needed(activity_hash)
Chris@0 226 else
Chris@0 227 activity = project.time_entry_activities.find_by_id(id.to_i)
Chris@0 228 activity.update_attributes(activity_hash) if activity
Chris@0 229 end
Chris@0 230 end
Chris@909 231
Chris@0 232 # Create a new TimeEntryActivity if it overrides a system TimeEntryActivity
Chris@0 233 #
Chris@0 234 # This will raise a ActiveRecord::Rollback if the TimeEntryActivity
Chris@0 235 # does not successfully save.
Chris@0 236 def create_time_entry_activity_if_needed(activity)
Chris@0 237 if activity['parent_id']
Chris@909 238
Chris@0 239 parent_activity = TimeEntryActivity.find(activity['parent_id'])
Chris@0 240 activity['name'] = parent_activity.name
Chris@0 241 activity['position'] = parent_activity.position
Chris@0 242
Chris@0 243 if Enumeration.overridding_change?(activity, parent_activity)
Chris@0 244 project_activity = self.time_entry_activities.create(activity)
Chris@0 245
Chris@0 246 if project_activity.new_record?
Chris@0 247 raise ActiveRecord::Rollback, "Overridding TimeEntryActivity was not successfully saved"
Chris@0 248 else
Chris@0 249 self.time_entries.update_all("activity_id = #{project_activity.id}", ["activity_id = ?", parent_activity.id])
Chris@0 250 end
Chris@0 251 end
Chris@0 252 end
Chris@0 253 end
Chris@0 254
Chris@0 255 # Returns a :conditions SQL string that can be used to find the issues associated with this project.
Chris@0 256 #
Chris@0 257 # Examples:
Chris@0 258 # project.project_condition(true) => "(projects.id = 1 OR (projects.lft > 1 AND projects.rgt < 10))"
Chris@0 259 # project.project_condition(false) => "projects.id = 1"
Chris@0 260 def project_condition(with_subprojects)
Chris@0 261 cond = "#{Project.table_name}.id = #{id}"
Chris@0 262 cond = "(#{cond} OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt}))" if with_subprojects
Chris@0 263 cond
Chris@0 264 end
Chris@909 265
Chris@0 266 def self.find(*args)
Chris@0 267 if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/)
Chris@0 268 project = find_by_identifier(*args)
Chris@0 269 raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil?
Chris@0 270 project
Chris@0 271 else
Chris@0 272 super
Chris@0 273 end
Chris@0 274 end
Chris@909 275
Chris@1115 276 def self.find_by_param(*args)
Chris@1115 277 self.find(*args)
Chris@1115 278 end
Chris@1115 279
Chris@1115 280 def reload(*args)
Chris@1115 281 @shared_versions = nil
Chris@1115 282 @rolled_up_versions = nil
Chris@1115 283 @rolled_up_trackers = nil
Chris@1115 284 @all_issue_custom_fields = nil
Chris@1115 285 @all_time_entry_custom_fields = nil
Chris@1115 286 @to_param = nil
Chris@1115 287 @allowed_parents = nil
Chris@1115 288 @allowed_permissions = nil
Chris@1115 289 @actions_allowed = nil
Chris@1115 290 super
Chris@1115 291 end
Chris@1115 292
Chris@0 293 def to_param
Chris@0 294 # id is used for projects with a numeric identifier (compatibility)
Chris@929 295 @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id.to_s : identifier)
Chris@0 296 end
Chris@909 297
Chris@0 298 def active?
Chris@0 299 self.status == STATUS_ACTIVE
Chris@0 300 end
Chris@909 301
chris@37 302 def archived?
chris@37 303 self.status == STATUS_ARCHIVED
chris@37 304 end
Chris@909 305
Chris@0 306 # Archives the project and its descendants
Chris@0 307 def archive
Chris@0 308 # Check that there is no issue of a non descendant project that is assigned
Chris@0 309 # to one of the project or descendant versions
Chris@0 310 v_ids = self_and_descendants.collect {|p| p.version_ids}.flatten
Chris@0 311 if v_ids.any? && Issue.find(:first, :include => :project,
Chris@0 312 :conditions => ["(#{Project.table_name}.lft < ? OR #{Project.table_name}.rgt > ?)" +
Chris@0 313 " AND #{Issue.table_name}.fixed_version_id IN (?)", lft, rgt, v_ids])
Chris@0 314 return false
Chris@0 315 end
Chris@0 316 Project.transaction do
Chris@0 317 archive!
Chris@0 318 end
Chris@0 319 true
Chris@0 320 end
Chris@909 321
Chris@0 322 # Unarchives the project
Chris@0 323 # All its ancestors must be active
Chris@0 324 def unarchive
Chris@0 325 return false if ancestors.detect {|a| !a.active?}
Chris@0 326 update_attribute :status, STATUS_ACTIVE
Chris@0 327 end
Chris@909 328
Chris@1115 329 def close
Chris@1115 330 self_and_descendants.status(STATUS_ACTIVE).update_all :status => STATUS_CLOSED
Chris@1115 331 end
Chris@1115 332
Chris@1115 333 def reopen
Chris@1115 334 self_and_descendants.status(STATUS_CLOSED).update_all :status => STATUS_ACTIVE
Chris@1115 335 end
Chris@1115 336
Chris@0 337 # Returns an array of projects the project can be moved to
Chris@0 338 # by the current user
Chris@0 339 def allowed_parents
Chris@0 340 return @allowed_parents if @allowed_parents
Chris@0 341 @allowed_parents = Project.find(:all, :conditions => Project.allowed_to_condition(User.current, :add_subprojects))
Chris@0 342 @allowed_parents = @allowed_parents - self_and_descendants
Chris@0 343 if User.current.allowed_to?(:add_project, nil, :global => true) || (!new_record? && parent.nil?)
Chris@0 344 @allowed_parents << nil
Chris@0 345 end
Chris@0 346 unless parent.nil? || @allowed_parents.empty? || @allowed_parents.include?(parent)
Chris@0 347 @allowed_parents << parent
Chris@0 348 end
Chris@0 349 @allowed_parents
Chris@0 350 end
Chris@909 351
Chris@0 352 # Sets the parent of the project with authorization check
Chris@0 353 def set_allowed_parent!(p)
Chris@0 354 unless p.nil? || p.is_a?(Project)
Chris@0 355 if p.to_s.blank?
Chris@0 356 p = nil
Chris@0 357 else
Chris@0 358 p = Project.find_by_id(p)
Chris@0 359 return false unless p
Chris@0 360 end
Chris@0 361 end
Chris@0 362 if p.nil?
Chris@0 363 if !new_record? && allowed_parents.empty?
Chris@0 364 return false
Chris@0 365 end
Chris@0 366 elsif !allowed_parents.include?(p)
Chris@0 367 return false
Chris@0 368 end
Chris@0 369 set_parent!(p)
Chris@0 370 end
Chris@909 371
Chris@0 372 # Sets the parent of the project
Chris@0 373 # Argument can be either a Project, a String, a Fixnum or nil
Chris@0 374 def set_parent!(p)
Chris@0 375 unless p.nil? || p.is_a?(Project)
Chris@0 376 if p.to_s.blank?
Chris@0 377 p = nil
Chris@0 378 else
Chris@0 379 p = Project.find_by_id(p)
Chris@0 380 return false unless p
Chris@0 381 end
Chris@0 382 end
Chris@0 383 if p == parent && !p.nil?
Chris@0 384 # Nothing to do
Chris@0 385 true
Chris@0 386 elsif p.nil? || (p.active? && move_possible?(p))
Chris@1115 387 set_or_update_position_under(p)
Chris@0 388 Issue.update_versions_from_hierarchy_change(self)
Chris@0 389 true
Chris@0 390 else
Chris@0 391 # Can not move to the given target
Chris@0 392 false
Chris@0 393 end
Chris@0 394 end
Chris@909 395
Chris@1115 396 # Recalculates all lft and rgt values based on project names
Chris@1115 397 # Unlike Project.rebuild!, these values are recalculated even if the tree "looks" valid
Chris@1115 398 # Used in BuildProjectsTree migration
Chris@1115 399 def self.rebuild_tree!
Chris@1115 400 transaction do
Chris@1115 401 update_all "lft = NULL, rgt = NULL"
Chris@1115 402 rebuild!(false)
Chris@1115 403 end
Chris@1115 404 end
Chris@1115 405
Chris@0 406 # Returns an array of the trackers used by the project and its active sub projects
Chris@0 407 def rolled_up_trackers
Chris@0 408 @rolled_up_trackers ||=
Chris@441 409 Tracker.find(:all, :joins => :projects,
Chris@0 410 :select => "DISTINCT #{Tracker.table_name}.*",
Chris@1115 411 :conditions => ["#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status <> #{STATUS_ARCHIVED}", lft, rgt],
Chris@0 412 :order => "#{Tracker.table_name}.position")
Chris@0 413 end
Chris@909 414
Chris@0 415 # Closes open and locked project versions that are completed
Chris@0 416 def close_completed_versions
Chris@0 417 Version.transaction do
Chris@0 418 versions.find(:all, :conditions => {:status => %w(open locked)}).each do |version|
Chris@0 419 if version.completed?
Chris@0 420 version.update_attribute(:status, 'closed')
Chris@0 421 end
Chris@0 422 end
Chris@0 423 end
Chris@0 424 end
Chris@0 425
Chris@0 426 # Returns a scope of the Versions on subprojects
Chris@0 427 def rolled_up_versions
Chris@0 428 @rolled_up_versions ||=
Chris@0 429 Version.scoped(:include => :project,
Chris@1115 430 :conditions => ["#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status <> #{STATUS_ARCHIVED}", lft, rgt])
Chris@0 431 end
Chris@909 432
Chris@0 433 # Returns a scope of the Versions used by the project
Chris@0 434 def shared_versions
Chris@929 435 if new_record?
Chris@0 436 Version.scoped(:include => :project,
Chris@1115 437 :conditions => "#{Project.table_name}.status <> #{Project::STATUS_ARCHIVED} AND #{Version.table_name}.sharing = 'system'")
Chris@929 438 else
Chris@929 439 @shared_versions ||= begin
Chris@929 440 r = root? ? self : root
Chris@929 441 Version.scoped(:include => :project,
Chris@929 442 :conditions => "#{Project.table_name}.id = #{id}" +
Chris@1115 443 " OR (#{Project.table_name}.status <> #{Project::STATUS_ARCHIVED} AND (" +
Chris@0 444 " #{Version.table_name}.sharing = 'system'" +
Chris@441 445 " OR (#{Project.table_name}.lft >= #{r.lft} AND #{Project.table_name}.rgt <= #{r.rgt} AND #{Version.table_name}.sharing = 'tree')" +
Chris@0 446 " OR (#{Project.table_name}.lft < #{lft} AND #{Project.table_name}.rgt > #{rgt} AND #{Version.table_name}.sharing IN ('hierarchy', 'descendants'))" +
Chris@0 447 " OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt} AND #{Version.table_name}.sharing = 'hierarchy')" +
Chris@0 448 "))")
Chris@929 449 end
Chris@441 450 end
Chris@0 451 end
Chris@0 452
Chris@0 453 # Returns a hash of project users grouped by role
Chris@0 454 def users_by_role
Chris@0 455 members.find(:all, :include => [:user, :roles]).inject({}) do |h, m|
Chris@0 456 m.roles.each do |r|
Chris@0 457 h[r] ||= []
Chris@0 458 h[r] << m.user
Chris@0 459 end
Chris@0 460 h
Chris@0 461 end
Chris@0 462 end
Chris@909 463
Chris@0 464 # Deletes all project's members
Chris@0 465 def delete_all_members
Chris@0 466 me, mr = Member.table_name, MemberRole.table_name
Chris@0 467 connection.delete("DELETE FROM #{mr} WHERE #{mr}.member_id IN (SELECT #{me}.id FROM #{me} WHERE #{me}.project_id = #{id})")
Chris@0 468 Member.delete_all(['project_id = ?', id])
Chris@0 469 end
Chris@909 470
Chris@909 471 # Users/groups issues can be assigned to
Chris@0 472 def assignable_users
Chris@909 473 assignable = Setting.issue_group_assignment? ? member_principals : members
Chris@909 474 assignable.select {|m| m.roles.detect {|role| role.assignable?}}.collect {|m| m.principal}.sort
Chris@0 475 end
Chris@909 476
Chris@0 477 # Returns the mail adresses of users that should be always notified on project events
Chris@0 478 def recipients
chris@37 479 notified_users.collect {|user| user.mail}
Chris@0 480 end
Chris@909 481
Chris@0 482 # Returns the users that should be notified on project events
Chris@0 483 def notified_users
chris@37 484 # TODO: User part should be extracted to User#notify_about?
Chris@1115 485 members.select {|m| m.principal.present? && (m.mail_notification? || m.principal.mail_notification == 'all')}.collect {|m| m.principal}
Chris@0 486 end
Chris@909 487
Chris@0 488 # Returns an array of all custom fields enabled for project issues
Chris@0 489 # (explictly associated custom fields and custom fields enabled for all projects)
Chris@0 490 def all_issue_custom_fields
Chris@0 491 @all_issue_custom_fields ||= (IssueCustomField.for_all + issue_custom_fields).uniq.sort
Chris@0 492 end
Chris@441 493
Chris@441 494 # Returns an array of all custom fields enabled for project time entries
Chris@441 495 # (explictly associated custom fields and custom fields enabled for all projects)
Chris@441 496 def all_time_entry_custom_fields
Chris@441 497 @all_time_entry_custom_fields ||= (TimeEntryCustomField.for_all + time_entry_custom_fields).uniq.sort
Chris@441 498 end
Chris@909 499
Chris@0 500 def project
Chris@0 501 self
Chris@0 502 end
Chris@909 503
Chris@0 504 def <=>(project)
Chris@0 505 name.downcase <=> project.name.downcase
Chris@0 506 end
Chris@909 507
Chris@0 508 def to_s
Chris@0 509 name
Chris@0 510 end
Chris@909 511
Chris@0 512 # Returns a short description of the projects (first lines)
Chris@0 513 def short_description(length = 255)
Chris@0 514 description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description
Chris@0 515 end
chris@22 516
chris@22 517 def css_classes
chris@22 518 s = 'project'
chris@22 519 s << ' root' if root?
chris@22 520 s << ' child' if child?
chris@22 521 s << (leaf? ? ' leaf' : ' parent')
Chris@1115 522 unless active?
Chris@1115 523 if archived?
Chris@1115 524 s << ' archived'
Chris@1115 525 else
Chris@1115 526 s << ' closed'
Chris@1115 527 end
Chris@1115 528 end
chris@22 529 s
chris@22 530 end
chris@22 531
chris@22 532 # The earliest start date of a project, based on it's issues and versions
chris@22 533 def start_date
Chris@119 534 [
Chris@119 535 issues.minimum('start_date'),
Chris@119 536 shared_versions.collect(&:effective_date),
Chris@119 537 shared_versions.collect(&:start_date)
Chris@119 538 ].flatten.compact.min
chris@22 539 end
chris@22 540
chris@22 541 # The latest due date of an issue or version
chris@22 542 def due_date
Chris@119 543 [
Chris@119 544 issues.maximum('due_date'),
Chris@119 545 shared_versions.collect(&:effective_date),
Chris@119 546 shared_versions.collect {|v| v.fixed_issues.maximum('due_date')}
Chris@119 547 ].flatten.compact.max
chris@22 548 end
chris@22 549
chris@22 550 def overdue?
chris@22 551 active? && !due_date.nil? && (due_date < Date.today)
chris@22 552 end
chris@22 553
chris@22 554 # Returns the percent completed for this project, based on the
chris@22 555 # progress on it's versions.
chris@22 556 def completed_percent(options={:include_subprojects => false})
chris@22 557 if options.delete(:include_subprojects)
chris@22 558 total = self_and_descendants.collect(&:completed_percent).sum
chris@22 559
chris@22 560 total / self_and_descendants.count
chris@22 561 else
chris@22 562 if versions.count > 0
chris@22 563 total = versions.collect(&:completed_pourcent).sum
chris@22 564
chris@22 565 total / versions.count
chris@22 566 else
chris@22 567 100
chris@22 568 end
chris@22 569 end
chris@22 570 end
Chris@909 571
Chris@1115 572 # Return true if this project allows to do the specified action.
Chris@0 573 # action can be:
Chris@0 574 # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
Chris@0 575 # * a permission Symbol (eg. :edit_project)
Chris@0 576 def allows_to?(action)
Chris@1115 577 if archived?
Chris@1115 578 # No action allowed on archived projects
Chris@1115 579 return false
Chris@1115 580 end
Chris@1115 581 unless active? || Redmine::AccessControl.read_action?(action)
Chris@1115 582 # No write action allowed on closed projects
Chris@1115 583 return false
Chris@1115 584 end
Chris@1115 585 # No action allowed on disabled modules
Chris@0 586 if action.is_a? Hash
Chris@0 587 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
Chris@0 588 else
Chris@0 589 allowed_permissions.include? action
Chris@0 590 end
Chris@0 591 end
Chris@909 592
Chris@0 593 def module_enabled?(module_name)
Chris@0 594 module_name = module_name.to_s
Chris@0 595 enabled_modules.detect {|m| m.name == module_name}
Chris@0 596 end
Chris@909 597
Chris@0 598 def enabled_module_names=(module_names)
Chris@0 599 if module_names && module_names.is_a?(Array)
Chris@119 600 module_names = module_names.collect(&:to_s).reject(&:blank?)
Chris@441 601 self.enabled_modules = module_names.collect {|name| enabled_modules.detect {|mod| mod.name == name} || EnabledModule.new(:name => name)}
Chris@0 602 else
Chris@0 603 enabled_modules.clear
Chris@0 604 end
Chris@0 605 end
Chris@909 606
Chris@119 607 # Returns an array of the enabled modules names
Chris@119 608 def enabled_module_names
Chris@119 609 enabled_modules.collect(&:name)
Chris@119 610 end
Chris@507 611
Chris@507 612 # Enable a specific module
Chris@507 613 #
Chris@507 614 # Examples:
Chris@507 615 # project.enable_module!(:issue_tracking)
Chris@507 616 # project.enable_module!("issue_tracking")
Chris@507 617 def enable_module!(name)
Chris@507 618 enabled_modules << EnabledModule.new(:name => name.to_s) unless module_enabled?(name)
Chris@507 619 end
Chris@507 620
Chris@507 621 # Disable a module if it exists
Chris@507 622 #
Chris@507 623 # Examples:
Chris@507 624 # project.disable_module!(:issue_tracking)
Chris@507 625 # project.disable_module!("issue_tracking")
Chris@507 626 # project.disable_module!(project.enabled_modules.first)
Chris@507 627 def disable_module!(target)
Chris@507 628 target = enabled_modules.detect{|mod| target.to_s == mod.name} unless enabled_modules.include?(target)
Chris@507 629 target.destroy unless target.blank?
Chris@507 630 end
Chris@507 631
Chris@119 632 safe_attributes 'name',
Chris@119 633 'description',
Chris@119 634 'homepage',
Chris@119 635 'is_public',
Chris@119 636 'identifier',
Chris@119 637 'custom_field_values',
Chris@119 638 'custom_fields',
Chris@119 639 'tracker_ids',
Chris@119 640 'issue_custom_field_ids'
chris@22 641
Chris@119 642 safe_attributes 'enabled_module_names',
Chris@119 643 :if => lambda {|project, user| project.new_record? || user.allowed_to?(:select_project_modules, project) }
Chris@909 644
chris@22 645 # Returns an array of projects that are in this project's hierarchy
chris@22 646 #
chris@22 647 # Example: parents, children, siblings
chris@22 648 def hierarchy
chris@22 649 parents = project.self_and_ancestors || []
chris@22 650 descendants = project.descendants || []
chris@22 651 project_hierarchy = parents | descendants # Set union
chris@22 652 end
Chris@909 653
Chris@0 654 # Returns an auto-generated project identifier based on the last identifier used
Chris@0 655 def self.next_identifier
Chris@0 656 p = Project.find(:first, :order => 'created_on DESC')
Chris@0 657 p.nil? ? nil : p.identifier.to_s.succ
Chris@0 658 end
Chris@0 659
Chris@0 660 # Copies and saves the Project instance based on the +project+.
Chris@0 661 # Duplicates the source project's:
Chris@0 662 # * Wiki
Chris@0 663 # * Versions
Chris@0 664 # * Categories
Chris@0 665 # * Issues
Chris@0 666 # * Members
Chris@0 667 # * Queries
Chris@0 668 #
Chris@0 669 # Accepts an +options+ argument to specify what to copy
Chris@0 670 #
Chris@0 671 # Examples:
Chris@0 672 # project.copy(1) # => copies everything
Chris@0 673 # project.copy(1, :only => 'members') # => copies members only
Chris@0 674 # project.copy(1, :only => ['members', 'versions']) # => copies members and versions
Chris@0 675 def copy(project, options={})
Chris@0 676 project = project.is_a?(Project) ? project : Project.find(project)
Chris@909 677
Chris@0 678 to_be_copied = %w(wiki versions issue_categories issues members queries boards)
Chris@0 679 to_be_copied = to_be_copied & options[:only].to_a unless options[:only].nil?
Chris@909 680
Chris@0 681 Project.transaction do
Chris@0 682 if save
Chris@0 683 reload
Chris@0 684 to_be_copied.each do |name|
Chris@0 685 send "copy_#{name}", project
Chris@0 686 end
Chris@0 687 Redmine::Hook.call_hook(:model_project_copy_before_save, :source_project => project, :destination_project => self)
Chris@0 688 save
Chris@0 689 end
Chris@0 690 end
Chris@0 691 end
Chris@0 692
Chris@909 693
Chris@0 694 # Copies +project+ and returns the new instance. This will not save
Chris@0 695 # the copy
Chris@0 696 def self.copy_from(project)
Chris@0 697 begin
Chris@0 698 project = project.is_a?(Project) ? project : Project.find(project)
Chris@0 699 if project
Chris@0 700 # clear unique attributes
Chris@0 701 attributes = project.attributes.dup.except('id', 'name', 'identifier', 'status', 'parent_id', 'lft', 'rgt')
Chris@0 702 copy = Project.new(attributes)
Chris@0 703 copy.enabled_modules = project.enabled_modules
Chris@0 704 copy.trackers = project.trackers
Chris@0 705 copy.custom_values = project.custom_values.collect {|v| v.clone}
Chris@0 706 copy.issue_custom_fields = project.issue_custom_fields
Chris@0 707 return copy
Chris@0 708 else
Chris@0 709 return nil
Chris@0 710 end
Chris@0 711 rescue ActiveRecord::RecordNotFound
Chris@0 712 return nil
Chris@0 713 end
Chris@0 714 end
chris@37 715
chris@37 716 # Yields the given block for each project with its level in the tree
chris@37 717 def self.project_tree(projects, &block)
chris@37 718 ancestors = []
chris@37 719 projects.sort_by(&:lft).each do |project|
Chris@909 720 while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
chris@37 721 ancestors.pop
chris@37 722 end
chris@37 723 yield project, ancestors.size
chris@37 724 ancestors << project
chris@37 725 end
chris@37 726 end
Chris@909 727
Chris@0 728 private
Chris@909 729
Chris@0 730 # Copies wiki from +project+
Chris@0 731 def copy_wiki(project)
Chris@0 732 # Check that the source project has a wiki first
Chris@0 733 unless project.wiki.nil?
Chris@1294 734 wiki = self.wiki || Wiki.new
Chris@0 735 wiki.attributes = project.wiki.attributes.dup.except("id", "project_id")
Chris@0 736 wiki_pages_map = {}
Chris@0 737 project.wiki.pages.each do |page|
Chris@0 738 # Skip pages without content
Chris@0 739 next if page.content.nil?
Chris@0 740 new_wiki_content = WikiContent.new(page.content.attributes.dup.except("id", "page_id", "updated_on"))
Chris@0 741 new_wiki_page = WikiPage.new(page.attributes.dup.except("id", "wiki_id", "created_on", "parent_id"))
Chris@0 742 new_wiki_page.content = new_wiki_content
Chris@0 743 wiki.pages << new_wiki_page
Chris@0 744 wiki_pages_map[page.id] = new_wiki_page
Chris@0 745 end
Chris@1294 746
Chris@1294 747 self.wiki = wiki
Chris@0 748 wiki.save
Chris@0 749 # Reproduce page hierarchy
Chris@0 750 project.wiki.pages.each do |page|
Chris@0 751 if page.parent_id && wiki_pages_map[page.id]
Chris@0 752 wiki_pages_map[page.id].parent = wiki_pages_map[page.parent_id]
Chris@0 753 wiki_pages_map[page.id].save
Chris@0 754 end
Chris@0 755 end
Chris@0 756 end
Chris@0 757 end
Chris@0 758
Chris@0 759 # Copies versions from +project+
Chris@0 760 def copy_versions(project)
Chris@0 761 project.versions.each do |version|
Chris@0 762 new_version = Version.new
Chris@0 763 new_version.attributes = version.attributes.dup.except("id", "project_id", "created_on", "updated_on")
Chris@0 764 self.versions << new_version
Chris@0 765 end
Chris@0 766 end
Chris@0 767
Chris@0 768 # Copies issue categories from +project+
Chris@0 769 def copy_issue_categories(project)
Chris@0 770 project.issue_categories.each do |issue_category|
Chris@0 771 new_issue_category = IssueCategory.new
Chris@0 772 new_issue_category.attributes = issue_category.attributes.dup.except("id", "project_id")
Chris@0 773 self.issue_categories << new_issue_category
Chris@0 774 end
Chris@0 775 end
Chris@909 776
Chris@0 777 # Copies issues from +project+
Chris@0 778 def copy_issues(project)
Chris@0 779 # Stores the source issue id as a key and the copied issues as the
Chris@0 780 # value. Used to map the two togeather for issue relations.
Chris@0 781 issues_map = {}
Chris@909 782
Chris@1115 783 # Store status and reopen locked/closed versions
Chris@1115 784 version_statuses = versions.reject(&:open?).map {|version| [version, version.status]}
Chris@1115 785 version_statuses.each do |version, status|
Chris@1115 786 version.update_attribute :status, 'open'
Chris@1115 787 end
Chris@1115 788
Chris@0 789 # Get issues sorted by root_id, lft so that parent issues
Chris@0 790 # get copied before their children
Chris@0 791 project.issues.find(:all, :order => 'root_id, lft').each do |issue|
Chris@0 792 new_issue = Issue.new
Chris@1115 793 new_issue.copy_from(issue, :subtasks => false, :link => false)
Chris@0 794 new_issue.project = self
Chris@1115 795 # Reassign fixed_versions by name, since names are unique per project
Chris@1115 796 if issue.fixed_version && issue.fixed_version.project == project
Chris@1115 797 new_issue.fixed_version = self.versions.detect {|v| v.name == issue.fixed_version.name}
Chris@0 798 end
Chris@1115 799 # Reassign the category by name, since names are unique per project
Chris@0 800 if issue.category
Chris@1115 801 new_issue.category = self.issue_categories.detect {|c| c.name == issue.category.name}
Chris@0 802 end
Chris@0 803 # Parent issue
Chris@0 804 if issue.parent_id
Chris@0 805 if copied_parent = issues_map[issue.parent_id]
Chris@0 806 new_issue.parent_issue_id = copied_parent.id
Chris@0 807 end
Chris@0 808 end
Chris@909 809
Chris@0 810 self.issues << new_issue
Chris@119 811 if new_issue.new_record?
Chris@119 812 logger.info "Project#copy_issues: issue ##{issue.id} could not be copied: #{new_issue.errors.full_messages}" if logger && logger.info
Chris@119 813 else
Chris@119 814 issues_map[issue.id] = new_issue unless new_issue.new_record?
Chris@119 815 end
Chris@0 816 end
Chris@0 817
Chris@1115 818 # Restore locked/closed version statuses
Chris@1115 819 version_statuses.each do |version, status|
Chris@1115 820 version.update_attribute :status, status
Chris@1115 821 end
Chris@1115 822
Chris@0 823 # Relations after in case issues related each other
Chris@0 824 project.issues.each do |issue|
Chris@0 825 new_issue = issues_map[issue.id]
Chris@119 826 unless new_issue
Chris@119 827 # Issue was not copied
Chris@119 828 next
Chris@119 829 end
Chris@909 830
Chris@0 831 # Relations
Chris@0 832 issue.relations_from.each do |source_relation|
Chris@0 833 new_issue_relation = IssueRelation.new
Chris@0 834 new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id")
Chris@0 835 new_issue_relation.issue_to = issues_map[source_relation.issue_to_id]
Chris@0 836 if new_issue_relation.issue_to.nil? && Setting.cross_project_issue_relations?
Chris@0 837 new_issue_relation.issue_to = source_relation.issue_to
Chris@0 838 end
Chris@0 839 new_issue.relations_from << new_issue_relation
Chris@0 840 end
Chris@909 841
Chris@0 842 issue.relations_to.each do |source_relation|
Chris@0 843 new_issue_relation = IssueRelation.new
Chris@0 844 new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id")
Chris@0 845 new_issue_relation.issue_from = issues_map[source_relation.issue_from_id]
Chris@0 846 if new_issue_relation.issue_from.nil? && Setting.cross_project_issue_relations?
Chris@0 847 new_issue_relation.issue_from = source_relation.issue_from
Chris@0 848 end
Chris@0 849 new_issue.relations_to << new_issue_relation
Chris@0 850 end
Chris@0 851 end
Chris@0 852 end
Chris@0 853
Chris@0 854 # Copies members from +project+
Chris@0 855 def copy_members(project)
Chris@119 856 # Copy users first, then groups to handle members with inherited and given roles
Chris@119 857 members_to_copy = []
Chris@119 858 members_to_copy += project.memberships.select {|m| m.principal.is_a?(User)}
Chris@119 859 members_to_copy += project.memberships.select {|m| !m.principal.is_a?(User)}
Chris@909 860
Chris@119 861 members_to_copy.each do |member|
Chris@0 862 new_member = Member.new
Chris@0 863 new_member.attributes = member.attributes.dup.except("id", "project_id", "created_on")
Chris@0 864 # only copy non inherited roles
Chris@0 865 # inherited roles will be added when copying the group membership
Chris@0 866 role_ids = member.member_roles.reject(&:inherited?).collect(&:role_id)
Chris@0 867 next if role_ids.empty?
Chris@0 868 new_member.role_ids = role_ids
Chris@0 869 new_member.project = self
Chris@0 870 self.members << new_member
Chris@0 871 end
Chris@0 872 end
Chris@0 873
Chris@0 874 # Copies queries from +project+
Chris@0 875 def copy_queries(project)
Chris@0 876 project.queries.each do |query|
Chris@1115 877 new_query = ::Query.new
Chris@0 878 new_query.attributes = query.attributes.dup.except("id", "project_id", "sort_criteria")
Chris@0 879 new_query.sort_criteria = query.sort_criteria if query.sort_criteria
Chris@0 880 new_query.project = self
Chris@909 881 new_query.user_id = query.user_id
Chris@0 882 self.queries << new_query
Chris@0 883 end
Chris@0 884 end
Chris@0 885
Chris@0 886 # Copies boards from +project+
Chris@0 887 def copy_boards(project)
Chris@0 888 project.boards.each do |board|
Chris@0 889 new_board = Board.new
Chris@0 890 new_board.attributes = board.attributes.dup.except("id", "project_id", "topics_count", "messages_count", "last_message_id")
Chris@0 891 new_board.project = self
Chris@0 892 self.boards << new_board
Chris@0 893 end
Chris@0 894 end
Chris@909 895
Chris@0 896 def allowed_permissions
Chris@0 897 @allowed_permissions ||= begin
Chris@0 898 module_names = enabled_modules.all(:select => :name).collect {|m| m.name}
Chris@0 899 Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name}
Chris@0 900 end
Chris@0 901 end
Chris@0 902
Chris@0 903 def allowed_actions
Chris@0 904 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
Chris@0 905 end
Chris@0 906
Chris@0 907 # Returns all the active Systemwide and project specific activities
Chris@0 908 def active_activities
Chris@0 909 overridden_activity_ids = self.time_entry_activities.collect(&:parent_id)
Chris@909 910
Chris@0 911 if overridden_activity_ids.empty?
Chris@0 912 return TimeEntryActivity.shared.active
Chris@0 913 else
Chris@0 914 return system_activities_and_project_overrides
Chris@0 915 end
Chris@0 916 end
Chris@0 917
Chris@0 918 # Returns all the Systemwide and project specific activities
Chris@0 919 # (inactive and active)
Chris@0 920 def all_activities
Chris@0 921 overridden_activity_ids = self.time_entry_activities.collect(&:parent_id)
Chris@0 922
Chris@0 923 if overridden_activity_ids.empty?
Chris@0 924 return TimeEntryActivity.shared
Chris@0 925 else
Chris@0 926 return system_activities_and_project_overrides(true)
Chris@0 927 end
Chris@0 928 end
Chris@0 929
Chris@0 930 # Returns the systemwide active activities merged with the project specific overrides
Chris@0 931 def system_activities_and_project_overrides(include_inactive=false)
Chris@0 932 if include_inactive
Chris@0 933 return TimeEntryActivity.shared.
Chris@0 934 find(:all,
Chris@0 935 :conditions => ["id NOT IN (?)", self.time_entry_activities.collect(&:parent_id)]) +
Chris@0 936 self.time_entry_activities
Chris@0 937 else
Chris@0 938 return TimeEntryActivity.shared.active.
Chris@0 939 find(:all,
Chris@0 940 :conditions => ["id NOT IN (?)", self.time_entry_activities.collect(&:parent_id)]) +
Chris@0 941 self.time_entry_activities.active
Chris@0 942 end
Chris@0 943 end
Chris@909 944
Chris@0 945 # Archives subprojects recursively
Chris@0 946 def archive!
Chris@0 947 children.each do |subproject|
Chris@0 948 subproject.send :archive!
Chris@0 949 end
Chris@0 950 update_attribute :status, STATUS_ARCHIVED
Chris@0 951 end
Chris@1115 952
Chris@1115 953 def update_position_under_parent
Chris@1115 954 set_or_update_position_under(parent)
Chris@1115 955 end
Chris@1115 956
Chris@1115 957 # Inserts/moves the project so that target's children or root projects stay alphabetically sorted
Chris@1115 958 def set_or_update_position_under(target_parent)
Chris@1115 959 sibs = (target_parent.nil? ? self.class.roots : target_parent.children)
Chris@1115 960 to_be_inserted_before = sibs.sort_by {|c| c.name.to_s.downcase}.detect {|c| c.name.to_s.downcase > name.to_s.downcase }
Chris@1115 961
Chris@1115 962 if to_be_inserted_before
Chris@1115 963 move_to_left_of(to_be_inserted_before)
Chris@1115 964 elsif target_parent.nil?
Chris@1115 965 if sibs.empty?
Chris@1115 966 # move_to_root adds the project in first (ie. left) position
Chris@1115 967 move_to_root
Chris@1115 968 else
Chris@1115 969 move_to_right_of(sibs.last) unless self == sibs.last
Chris@1115 970 end
Chris@1115 971 else
Chris@1115 972 # move_to_child_of adds the project in last (ie.right) position
Chris@1115 973 move_to_child_of(target_parent)
Chris@1115 974 end
Chris@1115 975 end
Chris@0 976 end