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