annotate .svn/pristine/13/1382b1d3f93b8b3247729bbc8f37c6ee649306fe.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

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