annotate .svn/pristine/a6/a6d8d977a7db837089d88e0eb10b4689db86305a.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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