annotate .svn/pristine/42/426db807d25aff82062694229d7d5b11c248c19a.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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