annotate app/models/project.rb @ 1628:9c5f8e24dadc live tip

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