annotate .svn/pristine/03/03b977fa002b4a1e6202e05fed1b5c5f4bee2ed3.svn-base @ 1464:261b3d9a4903 redmine-2.4

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