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