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