2011-03-15 18:39:59 +03:00
# Redmine - project management software
2013-01-12 13:29:31 +04:00
# Copyright (C) 2006-2013 Jean-Philippe Lang
2007-03-12 20:59:02 +03:00
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
2011-08-21 12:46:33 +04:00
#
2007-03-12 20:59:02 +03:00
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
2011-08-21 12:46:33 +04:00
#
2007-03-12 20:59:02 +03:00
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
class Project < ActiveRecord :: Base
2010-12-12 16:19:07 +03:00
include Redmine :: SafeAttributes
2011-08-21 12:46:33 +04:00
2007-05-27 21:42:04 +04:00
# Project statuses
STATUS_ACTIVE = 1
2012-06-25 21:49:35 +04:00
STATUS_CLOSED = 5
2007-05-27 21:42:04 +04:00
STATUS_ARCHIVED = 9
2011-08-21 12:46:33 +04:00
2010-11-14 15:33:14 +03:00
# Maximum length for project identifiers
IDENTIFIER_MAX_LENGTH = 100
2011-08-21 12:46:33 +04:00
2009-10-22 02:34:45 +04:00
# Specific overidden Activities
2009-11-21 15:00:49 +03:00
has_many :time_entry_activities
2012-12-08 14:45:34 +04:00
has_many :members , :include = > [ :principal , :roles ] , :conditions = > " #{ Principal . table_name } .type='User' AND #{ Principal . table_name } .status= #{ Principal :: STATUS_ACTIVE } "
2009-12-26 19:14:55 +03:00
has_many :memberships , :class_name = > 'Member'
2011-08-21 12:46:33 +04:00
has_many :member_principals , :class_name = > 'Member' ,
2009-09-12 12:36:46 +04:00
:include = > :principal ,
2012-12-08 14:45:34 +04:00
:conditions = > " #{ Principal . table_name } .type='Group' OR ( #{ Principal . table_name } .type='User' AND #{ Principal . table_name } .status= #{ Principal :: STATUS_ACTIVE } ) "
2011-08-21 12:46:33 +04:00
2007-09-14 15:34:08 +04:00
has_many :enabled_modules , :dependent = > :delete_all
2007-11-20 23:29:03 +03:00
has_and_belongs_to_many :trackers , :order = > " #{ Tracker . table_name } .position "
2012-03-04 15:53:15 +04:00
has_many :issues , :dependent = > :destroy , :include = > [ :status , :tracker ]
2007-08-16 14:38:23 +04:00
has_many :issue_changes , :through = > :issues , :source = > :journals
2007-06-25 23:17:58 +04:00
has_many :versions , :dependent = > :destroy , :order = > " #{ Version . table_name } .effective_date DESC, #{ Version . table_name } .name DESC "
2013-12-18 22:39:09 +04:00
has_many :time_entries , :dependent = > :destroy
2012-12-09 18:10:49 +04:00
has_many :queries , :class_name = > 'IssueQuery' , :dependent = > :delete_all
2007-03-12 20:59:02 +03:00
has_many :documents , :dependent = > :destroy
2011-03-20 23:26:59 +03:00
has_many :news , :dependent = > :destroy , :include = > :author
2007-03-16 01:11:02 +03:00
has_many :issue_categories , :dependent = > :delete_all , :order = > " #{ IssueCategory . table_name } .name "
2008-04-01 21:43:20 +04:00
has_many :boards , :dependent = > :destroy , :order = > " position ASC "
2012-01-15 22:19:19 +04:00
has_one :repository , :conditions = > [ " is_default = ? " , true ]
has_many :repositories , :dependent = > :destroy
2007-09-08 00:07:54 +04:00
has_many :changesets , :through = > :repository
2007-03-12 20:59:02 +03:00
has_one :wiki , :dependent = > :destroy
2007-11-12 20:23:14 +03:00
# Custom field for the project issues
2011-08-21 12:46:33 +04:00
has_and_belongs_to_many :issue_custom_fields ,
2007-11-12 20:23:14 +03:00
:class_name = > 'IssueCustomField' ,
:order = > " #{ CustomField . table_name } .position " ,
:join_table = > " #{ table_name_prefix } custom_fields_projects #{ table_name_suffix } " ,
:association_foreign_key = > 'custom_field_id'
2011-08-21 12:46:33 +04:00
2014-01-21 10:51:39 +04:00
acts_as_nested_set :dependent = > :destroy
2008-12-09 21:00:27 +03:00
acts_as_attachable :view_permission = > :view_files ,
:delete_permission = > :manage_files
2007-09-27 21:28:22 +04:00
2008-06-28 00:13:56 +04:00
acts_as_customizable
2010-02-25 00:05:14 +03:00
acts_as_searchable :columns = > [ 'name' , 'identifier' , 'description' ] , :project_key = > 'id' , :permission = > nil
2007-09-27 21:28:22 +04:00
acts_as_event :title = > Proc . new { | o | " #{ l ( :label_project ) } : #{ o . name } " } ,
2010-02-25 00:08:08 +03:00
:url = > Proc . new { | o | { :controller = > 'projects' , :action = > 'show' , :id = > o } } ,
2008-07-12 13:42:18 +04:00
:author = > nil
2007-09-27 21:28:22 +04:00
2011-01-06 23:36:31 +03:00
attr_protected :status
2011-08-21 12:46:33 +04:00
2008-01-20 21:37:51 +03:00
validates_presence_of :name , :identifier
2010-11-11 16:39:14 +03:00
validates_uniqueness_of :identifier
2007-03-12 20:59:02 +03:00
validates_associated :repository , :wiki
2010-11-14 15:33:14 +03:00
validates_length_of :name , :maximum = > 255
2008-05-25 17:37:29 +04:00
validates_length_of :homepage , :maximum = > 255
2010-11-14 15:33:14 +03:00
validates_length_of :identifier , :in = > 1 .. IDENTIFIER_MAX_LENGTH
2009-05-25 22:01:27 +04:00
# donwcase letters, digits, dashes but not digits only
2012-12-08 14:59:31 +04:00
validates_format_of :identifier , :with = > / \ A(?! \ d+$)[a-z0-9 \ -_]* \ z / , :if = > Proc . new { | p | p . identifier_changed? }
2009-07-18 12:06:51 +04:00
# reserved words
validates_exclusion_of :identifier , :in = > %w( new )
2012-08-10 20:50:15 +04:00
after_save :update_position_under_parent , :if = > Proc . new { | project | project . name_changed? }
2013-02-02 16:50:45 +04:00
after_save :update_inherited_members , :if = > Proc . new { | project | project . inherit_members_changed? }
2011-03-20 14:46:01 +03:00
before_destroy :delete_all_members
2008-09-13 20:31:11 +04:00
2012-12-07 21:59:20 +04:00
scope :has_module , lambda { | mod |
where ( " #{ Project . table_name } .id IN (SELECT em.project_id FROM #{ EnabledModule . table_name } em WHERE em.name=?) " , mod . to_s )
}
scope :active , lambda { where ( :status = > STATUS_ACTIVE ) }
scope :status , lambda { | arg | where ( arg . blank? ? nil : { :status = > arg . to_i } ) }
scope :all_public , lambda { where ( :is_public = > true ) }
scope :visible , lambda { | * args | where ( Project . visible_condition ( args . shift || User . current , * args ) ) }
2013-04-21 08:56:26 +04:00
scope :allowed_to , lambda { | * args |
2012-01-21 18:26:51 +04:00
user = User . current
permission = nil
if args . first . is_a? ( Symbol )
permission = args . shift
else
user = args . shift
permission = args . shift
end
2012-12-07 21:59:20 +04:00
where ( Project . allowed_to_condition ( user , permission , * args ) )
2012-01-21 18:26:51 +04:00
}
2012-04-27 03:51:10 +04:00
scope :like , lambda { | arg |
2011-12-05 02:31:02 +04:00
if arg . blank?
2012-12-07 21:59:20 +04:00
where ( nil )
2011-12-05 02:31:02 +04:00
else
pattern = " % #{ arg . to_s . strip . downcase } % "
2012-12-07 21:59:20 +04:00
where ( " LOWER(identifier) LIKE :p OR LOWER(name) LIKE :p " , :p = > pattern )
2011-12-05 02:31:02 +04:00
end
}
2011-08-21 12:46:33 +04:00
2011-12-18 17:26:20 +04:00
def initialize ( attributes = nil , * args )
2010-12-03 19:15:16 +03:00
super
2011-08-21 12:46:33 +04:00
2010-12-03 19:15:16 +03:00
initialized = ( attributes || { } ) . stringify_keys
2011-08-21 12:46:33 +04:00
if ! initialized . key? ( 'identifier' ) && Setting . sequential_project_identifiers?
2010-12-03 19:15:16 +03:00
self . identifier = Project . next_identifier
end
if ! initialized . key? ( 'is_public' )
self . is_public = Setting . default_projects_public?
end
if ! initialized . key? ( 'enabled_module_names' )
self . enabled_module_names = Setting . default_projects_modules
end
if ! initialized . key? ( 'trackers' ) && ! initialized . key? ( 'tracker_ids' )
2013-02-15 12:28:34 +04:00
default = Setting . default_projects_tracker_ids
if default . is_a? ( Array )
self . trackers = Tracker . where ( :id = > default . map ( & :to_i ) ) . sorted . all
else
self . trackers = Tracker . sorted . all
end
2010-12-03 19:15:16 +03:00
end
end
2011-08-21 12:46:33 +04:00
2007-04-01 23:43:59 +04:00
def identifier = ( identifier )
super unless identifier_frozen?
end
2011-08-21 12:46:33 +04:00
2007-04-01 23:43:59 +04:00
def identifier_frozen?
2012-05-25 20:43:18 +04:00
errors [ :identifier ] . blank? && ! ( new_record? || identifier . blank? )
2007-04-01 23:43:59 +04:00
end
2007-11-08 22:00:37 +03:00
2007-03-12 20:59:02 +03:00
# returns latest created projects
# non public projects will be returned only if user is a member of those
def self . latest ( user = nil , count = 5 )
2013-02-17 14:14:08 +04:00
visible ( user ) . limit ( count ) . order ( " created_on DESC " ) . all
end
2007-03-12 20:59:02 +03:00
2011-07-09 15:41:04 +04:00
# Returns true if the project is visible to +user+ or to the current user.
def visible? ( user = User . current )
user . allowed_to? ( :view_project , self )
end
2011-08-21 12:46:33 +04:00
2011-04-05 16:50:19 +04:00
# Returns a SQL conditions string used to find all projects visible by the specified user.
2009-03-21 03:39:53 +03:00
#
# Examples:
2011-04-05 16:50:19 +04:00
# Project.visible_condition(admin) => "projects.status = 1"
# Project.visible_condition(normal_user) => "((projects.status = 1) AND (projects.is_public = 1 OR projects.id IN (1,3,4)))"
# Project.visible_condition(anonymous) => "((projects.status = 1) AND (projects.is_public = 1))"
def self . visible_condition ( user , options = { } )
allowed_to_condition ( user , :view_project , options )
2007-05-27 21:42:04 +04:00
end
2011-08-21 12:46:33 +04:00
2011-04-05 16:50:19 +04:00
# Returns a SQL conditions string used to find all projects for which +user+ has the given +permission+
#
# Valid options:
# * :project => limit the condition to project
# * :with_subprojects => limit the condition to project and its subprojects
# * :member => limit the condition to the user projects
2008-03-11 22:33:38 +03:00
def self . allowed_to_condition ( user , permission , options = { } )
2012-06-25 21:49:35 +04:00
perm = Redmine :: AccessControl . permission ( permission )
base_statement = ( perm && perm . read? ? " #{ Project . table_name } .status <> #{ Project :: STATUS_ARCHIVED } " : " #{ Project . table_name } .status = #{ Project :: STATUS_ACTIVE } " )
if perm && perm . project_module
# If the permission belongs to a project module, make sure the module is enabled
base_statement << " AND #{ Project . table_name } .id IN (SELECT em.project_id FROM #{ EnabledModule . table_name } em WHERE em.name=' #{ perm . project_module } ') "
2008-09-25 22:51:03 +04:00
end
2008-03-11 22:33:38 +03:00
if options [ :project ]
project_statement = " #{ Project . table_name } .id = #{ options [ :project ] . id } "
2009-01-24 14:31:15 +03:00
project_statement << " OR ( #{ Project . table_name } .lft > #{ options [ :project ] . lft } AND #{ Project . table_name } .rgt < #{ options [ :project ] . rgt } ) " if options [ :with_subprojects ]
2008-03-11 22:33:38 +03:00
base_statement = " ( #{ project_statement } ) AND ( #{ base_statement } ) "
end
2011-08-21 12:46:33 +04:00
2008-02-27 23:50:19 +03:00
if user . admin?
2011-03-15 18:39:59 +03:00
base_statement
2008-02-27 23:50:19 +03:00
else
2011-03-15 18:39:59 +03:00
statement_by_role = { }
2011-04-14 21:49:20 +04:00
unless options [ :member ]
2013-06-12 23:38:43 +04:00
role = user . builtin_role
2011-04-14 21:49:20 +04:00
if role . allowed_to? ( permission )
statement_by_role [ role ] = " #{ Project . table_name } .is_public = #{ connection . quoted_true } "
2011-03-15 18:39:59 +03:00
end
2011-04-14 21:49:20 +04:00
end
if user . logged?
2011-03-15 18:39:59 +03:00
user . projects_by_role . each do | role , projects |
2012-08-27 14:09:08 +04:00
if role . allowed_to? ( permission ) && projects . any?
2011-03-15 18:39:59 +03:00
statement_by_role [ role ] = " #{ Project . table_name } .id IN ( #{ projects . collect ( & :id ) . join ( ',' ) } ) "
end
2009-11-15 16:22:08 +03:00
end
2008-07-10 16:31:49 +04:00
end
2011-03-15 18:39:59 +03:00
if statement_by_role . empty?
" 1=0 "
else
2011-04-11 21:53:15 +04:00
if block_given?
statement_by_role . each do | role , statement |
if s = yield ( role , user )
statement_by_role [ role ] = " ( #{ statement } AND ( #{ s } )) "
end
end
end
2011-03-15 18:39:59 +03:00
" (( #{ base_statement } ) AND ( #{ statement_by_role . values . join ( ' OR ' ) } )) "
end
2008-02-27 23:50:19 +03:00
end
end
2009-03-21 03:39:53 +03:00
2013-03-12 22:07:37 +04:00
def principals
@principals || = Principal . active . joins ( :members ) . where ( " #{ Member . table_name } .project_id = ? " , id ) . uniq
end
2013-03-12 22:32:36 +04:00
def users
@users || = User . active . joins ( :members ) . where ( " #{ Member . table_name } .project_id = ? " , id ) . uniq
end
2009-10-22 02:34:45 +04:00
# Returns the Systemwide and project specific activities
def activities ( include_inactive = false )
if include_inactive
return all_activities
else
return active_activities
end
end
2009-10-22 02:34:39 +04:00
2009-10-22 02:34:52 +04:00
# Will create a new Project specific Activity or update an existing one
#
# This will raise a ActiveRecord::Rollback if the TimeEntryActivity
# does not successfully save.
def update_or_create_time_entry_activity ( id , activity_hash )
2009-10-22 02:34:45 +04:00
if activity_hash . respond_to? ( :has_key? ) && activity_hash . has_key? ( 'parent_id' )
2009-10-22 02:34:52 +04:00
self . create_time_entry_activity_if_needed ( activity_hash )
2009-10-22 02:34:39 +04:00
else
2009-10-22 02:34:45 +04:00
activity = project . time_entry_activities . find_by_id ( id . to_i )
activity . update_attributes ( activity_hash ) if activity
end
end
2011-08-21 12:46:33 +04:00
2009-10-22 02:34:52 +04:00
# Create a new TimeEntryActivity if it overrides a system TimeEntryActivity
#
# This will raise a ActiveRecord::Rollback if the TimeEntryActivity
# does not successfully save.
def create_time_entry_activity_if_needed ( activity )
2009-10-22 02:34:45 +04:00
if activity [ 'parent_id' ]
parent_activity = TimeEntryActivity . find ( activity [ 'parent_id' ] )
activity [ 'name' ] = parent_activity . name
activity [ 'position' ] = parent_activity . position
if Enumeration . overridding_change? ( activity , parent_activity )
2009-10-22 02:34:52 +04:00
project_activity = self . time_entry_activities . create ( activity )
if project_activity . new_record?
raise ActiveRecord :: Rollback , " Overridding TimeEntryActivity was not successfully saved "
else
2014-01-10 16:49:20 +04:00
self . time_entries .
where ( [ " activity_id = ? " , parent_activity . id ] ) .
update_all ( " activity_id = #{ project_activity . id } " )
2009-10-22 02:34:52 +04:00
end
2009-10-22 02:34:45 +04:00
end
2009-10-22 02:34:39 +04:00
end
end
2009-03-21 03:39:53 +03:00
# Returns a :conditions SQL string that can be used to find the issues associated with this project.
#
# Examples:
# project.project_condition(true) => "(projects.id = 1 OR (projects.lft > 1 AND projects.rgt < 10))"
# project.project_condition(false) => "projects.id = 1"
2008-03-27 21:22:12 +03:00
def project_condition ( with_subprojects )
cond = " #{ Project . table_name } .id = #{ id } "
2009-01-24 14:31:15 +03:00
cond = " ( #{ cond } OR ( #{ Project . table_name } .lft > #{ lft } AND #{ Project . table_name } .rgt < #{ rgt } )) " if with_subprojects
2008-03-27 21:22:12 +03:00
cond
end
2011-08-21 12:46:33 +04:00
2007-12-18 00:00:56 +03:00
def self . find ( * args )
if args . first && args . first . is_a? ( String ) && ! args . first . match ( / ^ \ d*$ / )
project = find_by_identifier ( * args )
raise ActiveRecord :: RecordNotFound , " Couldn't find Project with identifier= #{ args . first } " if project . nil?
project
else
super
end
end
2011-08-21 12:46:33 +04:00
2012-04-25 21:17:49 +04:00
def self . find_by_param ( * args )
self . find ( * args )
end
2013-02-28 22:47:25 +04:00
alias :base_reload :reload
2012-02-24 22:38:05 +04:00
def reload ( * args )
2013-03-12 22:07:37 +04:00
@principals = nil
2013-03-12 22:32:36 +04:00
@users = nil
2012-02-24 22:38:05 +04:00
@shared_versions = nil
@rolled_up_versions = nil
@rolled_up_trackers = nil
@all_issue_custom_fields = nil
@all_time_entry_custom_fields = nil
@to_param = nil
@allowed_parents = nil
@allowed_permissions = nil
@actions_allowed = nil
2013-01-06 18:03:49 +04:00
@start_date = nil
@due_date = nil
2013-02-28 22:47:25 +04:00
base_reload ( * args )
2012-02-24 22:38:05 +04:00
end
2007-12-18 00:00:56 +03:00
def to_param
2008-05-29 21:42:10 +04:00
# id is used for projects with a numeric identifier (compatibility)
2012-02-06 18:44:40 +04:00
@to_param || = ( identifier . to_s =~ %r{ ^ \ d*$ } ? id . to_s : identifier )
2007-12-18 00:00:56 +03:00
end
2011-08-21 12:46:33 +04:00
2007-05-27 21:42:04 +04:00
def active?
self . status == STATUS_ACTIVE
end
2011-08-21 12:46:33 +04:00
2010-10-23 13:48:01 +04:00
def archived?
self . status == STATUS_ARCHIVED
end
2011-08-21 12:46:33 +04:00
2009-12-06 13:28:20 +03:00
# Archives the project and its descendants
2007-05-27 21:42:04 +04:00
def archive
2009-12-06 13:28:20 +03:00
# Check that there is no issue of a non descendant project that is assigned
# to one of the project or descendant versions
v_ids = self_and_descendants . collect { | p | p . version_ids } . flatten
2012-12-03 22:21:32 +04:00
if v_ids . any? &&
Issue .
includes ( :project ) .
where ( " #{ Project . table_name } .lft < ? OR #{ Project . table_name } .rgt > ? " , lft , rgt ) .
where ( " #{ Issue . table_name } .fixed_version_id IN (?) " , v_ids ) .
exists?
2009-12-06 13:28:20 +03:00
return false
2007-03-12 20:59:02 +03:00
end
2009-12-06 13:28:20 +03:00
Project . transaction do
archive!
end
true
2007-05-27 21:42:04 +04:00
end
2011-08-21 12:46:33 +04:00
2009-01-24 14:31:15 +03:00
# Unarchives the project
# All its ancestors must be active
2007-05-27 21:42:04 +04:00
def unarchive
2009-01-24 14:31:15 +03:00
return false if ancestors . detect { | a | ! a . active? }
2007-05-27 21:42:04 +04:00
update_attribute :status , STATUS_ACTIVE
end
2011-08-21 12:46:33 +04:00
2012-06-25 21:49:35 +04:00
def close
self_and_descendants . status ( STATUS_ACTIVE ) . update_all :status = > STATUS_CLOSED
end
def reopen
self_and_descendants . status ( STATUS_CLOSED ) . update_all :status = > STATUS_ACTIVE
end
2009-01-24 14:31:15 +03:00
# Returns an array of projects the project can be moved to
2009-11-15 16:22:08 +03:00
# by the current user
def allowed_parents
return @allowed_parents if @allowed_parents
2012-12-03 00:23:48 +04:00
@allowed_parents = Project . where ( Project . allowed_to_condition ( User . current , :add_subprojects ) ) . all
2009-12-24 19:18:15 +03:00
@allowed_parents = @allowed_parents - self_and_descendants
2010-02-10 23:38:23 +03:00
if User . current . allowed_to? ( :add_project , nil , :global = > true ) || ( ! new_record? && parent . nil? )
2009-12-24 19:18:15 +03:00
@allowed_parents << nil
end
2009-11-15 16:22:08 +03:00
unless parent . nil? || @allowed_parents . empty? || @allowed_parents . include? ( parent )
@allowed_parents << parent
end
@allowed_parents
end
2011-08-21 12:46:33 +04:00
2009-11-15 16:22:08 +03:00
# Sets the parent of the project with authorization check
def set_allowed_parent! ( p )
unless p . nil? || p . is_a? ( Project )
if p . to_s . blank?
p = nil
else
p = Project . find_by_id ( p )
return false unless p
end
end
if p . nil?
if ! new_record? && allowed_parents . empty?
return false
end
elsif ! allowed_parents . include? ( p )
return false
end
set_parent! ( p )
2009-01-24 14:31:15 +03:00
end
2011-08-21 12:46:33 +04:00
2009-01-24 14:31:15 +03:00
# Sets the parent of the project
# Argument can be either a Project, a String, a Fixnum or nil
def set_parent! ( p )
unless p . nil? || p . is_a? ( Project )
if p . to_s . blank?
p = nil
else
p = Project . find_by_id ( p )
return false unless p
end
end
if p == parent && ! p . nil?
# Nothing to do
true
elsif p . nil? || ( p . active? && move_possible? ( p ) )
2012-08-10 20:50:15 +04:00
set_or_update_position_under ( p )
2009-12-08 23:47:52 +03:00
Issue . update_versions_from_hierarchy_change ( self )
2009-01-24 14:31:15 +03:00
true
else
# Can not move to the given target
false
end
2007-03-12 20:59:02 +03:00
end
2011-08-21 12:46:33 +04:00
2012-11-23 21:50:46 +04:00
# Recalculates all lft and rgt values based on project names
# Unlike Project.rebuild!, these values are recalculated even if the tree "looks" valid
2012-11-23 21:53:12 +04:00
# Used in BuildProjectsTree migration
2012-11-23 21:50:46 +04:00
def self . rebuild_tree!
transaction do
update_all " lft = NULL, rgt = NULL "
rebuild! ( false )
2014-01-21 10:29:48 +04:00
all . each { | p | p . set_or_update_position_under ( p . parent ) }
2012-11-23 21:50:46 +04:00
end
end
2009-01-25 14:15:28 +03:00
# Returns an array of the trackers used by the project and its active sub projects
2008-01-17 00:27:48 +03:00
def rolled_up_trackers
@rolled_up_trackers || =
2012-12-03 00:43:16 +04:00
Tracker .
joins ( :projects ) .
2013-02-09 19:20:07 +04:00
joins ( " JOIN #{ EnabledModule . table_name } ON #{ EnabledModule . table_name } .project_id = #{ Project . table_name } .id AND #{ EnabledModule . table_name } .name = 'issue_tracking' " ) .
2012-12-03 00:43:16 +04:00
select ( " DISTINCT #{ Tracker . table_name } .* " ) .
where ( " #{ Project . table_name } .lft >= ? AND #{ Project . table_name } .rgt <= ? AND #{ Project . table_name } .status <> #{ STATUS_ARCHIVED } " , lft , rgt ) .
sorted .
all
2008-01-17 00:27:48 +03:00
end
2011-08-21 12:46:33 +04:00
2009-11-09 21:53:12 +03:00
# Closes open and locked project versions that are completed
def close_completed_versions
Version . transaction do
2012-12-03 00:23:48 +04:00
versions . where ( :status = > %w( open locked ) ) . all . each do | version |
2009-11-09 21:53:12 +03:00
if version . completed?
version . update_attribute ( :status , 'closed' )
end
end
end
end
2010-05-27 21:16:10 +04:00
# Returns a scope of the Versions on subprojects
def rolled_up_versions
@rolled_up_versions || =
2013-05-18 23:58:43 +04:00
Version .
includes ( :project ) .
where ( " #{ Project . table_name } .lft >= ? AND #{ Project . table_name } .rgt <= ? AND #{ Project . table_name } .status <> ? " , lft , rgt , STATUS_ARCHIVED )
2010-05-27 21:16:10 +04:00
end
2011-08-21 12:46:33 +04:00
2009-12-06 13:28:20 +03:00
# Returns a scope of the Versions used by the project
def shared_versions
2012-02-13 22:57:12 +04:00
if new_record?
2013-05-18 23:58:43 +04:00
Version .
includes ( :project ) .
where ( " #{ Project . table_name } .status <> ? AND #{ Version . table_name } .sharing = 'system' " , STATUS_ARCHIVED )
2012-02-13 22:57:12 +04:00
else
@shared_versions || = begin
r = root? ? self : root
2013-05-18 23:58:43 +04:00
Version .
includes ( :project ) .
where ( " #{ Project . table_name } .id = #{ id } " +
" OR ( #{ Project . table_name } .status <> #{ Project :: STATUS_ARCHIVED } AND ( " +
" #{ Version . table_name } .sharing = 'system' " +
" OR ( #{ Project . table_name } .lft >= #{ r . lft } AND #{ Project . table_name } .rgt <= #{ r . rgt } AND #{ Version . table_name } .sharing = 'tree') " +
" OR ( #{ Project . table_name } .lft < #{ lft } AND #{ Project . table_name } .rgt > #{ rgt } AND #{ Version . table_name } .sharing IN ('hierarchy', 'descendants')) " +
" OR ( #{ Project . table_name } .lft > #{ lft } AND #{ Project . table_name } .rgt < #{ rgt } AND #{ Version . table_name } .sharing = 'hierarchy') " +
" )) " )
2012-02-13 22:57:12 +04:00
end
2011-03-29 00:29:43 +04:00
end
2009-12-06 13:28:20 +03:00
end
2009-05-12 23:04:52 +04:00
# Returns a hash of project users grouped by role
def users_by_role
2012-12-03 00:23:48 +04:00
members . includes ( :user , :roles ) . all . inject ( { } ) do | h , m |
2009-05-12 23:04:52 +04:00
m . roles . each do | r |
h [ r ] || = [ ]
h [ r ] << m . user
end
h
end
end
2011-08-21 12:46:33 +04:00
2007-12-10 20:58:07 +03:00
# Deletes all project's members
def delete_all_members
2009-05-10 14:54:31 +04:00
me , mr = Member . table_name , MemberRole . table_name
connection . delete ( " DELETE FROM #{ mr } WHERE #{ mr } .member_id IN (SELECT #{ me } .id FROM #{ me } WHERE #{ me } .project_id = #{ id } ) " )
2007-12-10 20:58:07 +03:00
Member . delete_all ( [ 'project_id = ?' , id ] )
end
2011-08-21 12:46:33 +04:00
2011-07-23 22:18:13 +04:00
# Users/groups issues can be assigned to
2007-10-09 23:07:19 +04:00
def assignable_users
2011-07-23 22:18:13 +04:00
assignable = Setting . issue_group_assignment? ? member_principals : members
assignable . select { | m | m . roles . detect { | role | role . assignable? } } . collect { | m | m . principal } . sort
2007-10-09 23:07:19 +04:00
end
2011-08-21 12:46:33 +04:00
2007-10-20 16:47:05 +04:00
# Returns the mail adresses of users that should be always notified on project events
def recipients
2010-10-11 01:42:24 +04:00
notified_users . collect { | user | user . mail }
2007-10-20 16:47:05 +04:00
end
2011-08-21 12:46:33 +04:00
2009-12-04 00:28:14 +03:00
# Returns the users that should be notified on project events
def notified_users
2010-10-11 01:42:24 +04:00
# TODO: User part should be extracted to User#notify_about?
2012-09-22 14:12:59 +04:00
members . select { | m | m . principal . present? && ( m . mail_notification? || m . principal . mail_notification == 'all' ) } . collect { | m | m . principal }
2009-12-04 00:28:14 +03:00
end
2011-08-21 12:46:33 +04:00
2013-06-01 14:26:17 +04:00
# Returns a scope of all custom fields enabled for project issues
2007-03-12 20:59:02 +03:00
# (explictly associated custom fields and custom fields enabled for all projects)
2008-06-28 00:13:56 +04:00
def all_issue_custom_fields
2013-06-01 14:26:17 +04:00
@all_issue_custom_fields || = IssueCustomField .
sorted .
where ( " is_for_all = ? OR id IN (SELECT DISTINCT cfp.custom_field_id " +
" FROM #{ table_name_prefix } custom_fields_projects #{ table_name_suffix } cfp " +
" WHERE cfp.project_id = ?) " , true , id )
2007-03-12 20:59:02 +03:00
end
2011-04-04 15:53:29 +04:00
# Returns an array of all custom fields enabled for project time entries
# (explictly associated custom fields and custom fields enabled for all projects)
def all_time_entry_custom_fields
@all_time_entry_custom_fields || = ( TimeEntryCustomField . for_all + time_entry_custom_fields ) . uniq . sort
end
2011-08-21 12:46:33 +04:00
2008-05-18 20:15:22 +04:00
def project
self
end
2011-08-21 12:46:33 +04:00
2007-09-05 21:24:22 +04:00
def <=> ( project )
2007-11-20 01:28:43 +03:00
name . downcase < = > project . name . downcase
2007-09-05 21:24:22 +04:00
end
2011-08-21 12:46:33 +04:00
2008-01-20 21:37:51 +03:00
def to_s
name
end
2011-08-21 12:46:33 +04:00
2008-01-20 21:37:51 +03:00
# Returns a short description of the projects (first lines)
def short_description ( length = 255 )
2009-01-24 14:31:15 +03:00
description . gsub ( / ^(.{ #{ length } }[^ \ n \ r]*).*$ /m , '\1...' ) . strip if description
2008-01-20 21:37:51 +03:00
end
2010-09-10 07:09:02 +04:00
2010-09-11 03:07:10 +04:00
def css_classes
s = 'project'
s << ' root' if root?
s << ' child' if child?
s << ( leaf? ? ' leaf' : ' parent' )
2012-06-25 21:49:35 +04:00
unless active?
if archived?
s << ' archived'
else
s << ' closed'
end
end
2010-09-11 03:07:10 +04:00
s
end
2010-09-10 07:09:02 +04:00
# The earliest start date of a project, based on it's issues and versions
def start_date
2013-01-06 18:03:49 +04:00
@start_date || = [
2010-12-07 22:42:36 +03:00
issues . minimum ( 'start_date' ) ,
2013-01-06 18:03:49 +04:00
shared_versions . minimum ( 'effective_date' ) ,
Issue . fixed_version ( shared_versions ) . minimum ( 'start_date' )
] . compact . min
2010-09-10 07:09:02 +04:00
end
2010-09-26 21:35:18 +04:00
# The latest due date of an issue or version
2010-09-10 07:09:02 +04:00
def due_date
2013-01-06 18:03:49 +04:00
@due_date || = [
2010-12-07 22:42:36 +03:00
issues . maximum ( 'due_date' ) ,
2013-01-06 18:03:49 +04:00
shared_versions . maximum ( 'effective_date' ) ,
Issue . fixed_version ( shared_versions ) . maximum ( 'due_date' )
] . compact . max
2010-09-10 07:09:02 +04:00
end
def overdue?
active? && ! due_date . nil? && ( due_date < Date . today )
end
# Returns the percent completed for this project, based on the
# progress on it's versions.
def completed_percent ( options = { :include_subprojects = > false } )
if options . delete ( :include_subprojects )
total = self_and_descendants . collect ( & :completed_percent ) . sum
total / self_and_descendants . count
else
if versions . count > 0
2013-01-04 12:30:25 +04:00
total = versions . collect ( & :completed_percent ) . sum
2010-09-10 07:09:02 +04:00
total / versions . count
else
100
end
end
end
2011-08-21 12:46:33 +04:00
2012-06-25 22:44:25 +04:00
# Return true if this project allows to do the specified action.
2009-03-21 03:39:53 +03:00
# action can be:
# * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
# * a permission Symbol (eg. :edit_project)
2007-09-14 15:34:08 +04:00
def allows_to? ( action )
2012-06-25 22:44:25 +04:00
if archived?
# No action allowed on archived projects
return false
end
unless active? || Redmine :: AccessControl . read_action? ( action )
# No write action allowed on closed projects
return false
end
# No action allowed on disabled modules
2007-09-14 15:34:08 +04:00
if action . is_a? Hash
allowed_actions . include? " #{ action [ :controller ] } / #{ action [ :action ] } "
else
allowed_permissions . include? action
end
end
2011-08-21 12:46:33 +04:00
2007-09-14 15:34:08 +04:00
def module_enabled? ( module_name )
module_name = module_name . to_s
enabled_modules . detect { | m | m . name == module_name }
end
2011-08-21 12:46:33 +04:00
2007-09-14 15:34:08 +04:00
def enabled_module_names = ( module_names )
2009-02-15 19:26:48 +03:00
if module_names && module_names . is_a? ( Array )
2010-12-03 19:15:16 +03:00
module_names = module_names . collect ( & :to_s ) . reject ( & :blank? )
2011-03-30 21:16:25 +04:00
self . enabled_modules = module_names . collect { | name | enabled_modules . detect { | mod | mod . name == name } || EnabledModule . new ( :name = > name ) }
2009-02-15 19:26:48 +03:00
else
enabled_modules . clear
2007-09-14 15:34:08 +04:00
end
end
2011-08-21 12:46:33 +04:00
2010-12-03 19:15:16 +03:00
# Returns an array of the enabled modules names
def enabled_module_names
enabled_modules . collect ( & :name )
end
2011-06-20 03:45:22 +04:00
# Enable a specific module
#
# Examples:
# project.enable_module!(:issue_tracking)
# project.enable_module!("issue_tracking")
def enable_module! ( name )
enabled_modules << EnabledModule . new ( :name = > name . to_s ) unless module_enabled? ( name )
end
# Disable a module if it exists
#
# Examples:
# project.disable_module!(:issue_tracking)
# project.disable_module!("issue_tracking")
# project.disable_module!(project.enabled_modules.first)
def disable_module! ( target )
target = enabled_modules . detect { | mod | target . to_s == mod . name } unless enabled_modules . include? ( target )
target . destroy unless target . blank?
end
2010-12-12 16:19:07 +03:00
safe_attributes 'name' ,
'description' ,
'homepage' ,
'is_public' ,
'identifier' ,
'custom_field_values' ,
'custom_fields' ,
2010-12-17 19:10:46 +03:00
'tracker_ids' ,
'issue_custom_field_ids'
2010-09-14 23:02:25 +04:00
2011-01-06 23:36:31 +03:00
safe_attributes 'enabled_module_names' ,
:if = > lambda { | project , user | project . new_record? || user . allowed_to? ( :select_project_modules , project ) }
2011-08-21 12:46:33 +04:00
2013-02-02 16:50:45 +04:00
safe_attributes 'inherit_members' ,
2013-02-02 17:18:04 +04:00
:if = > lambda { | project , user | project . parent . nil? || project . parent . visible? ( user ) }
2013-02-02 16:50:45 +04:00
2010-09-14 23:02:25 +04:00
# Returns an array of projects that are in this project's hierarchy
#
# Example: parents, children, siblings
def hierarchy
parents = project . self_and_ancestors || [ ]
descendants = project . descendants || [ ]
project_hierarchy = parents | descendants # Set union
end
2011-08-21 12:46:33 +04:00
2008-08-31 16:59:57 +04:00
# Returns an auto-generated project identifier based on the last identifier used
def self . next_identifier
2013-06-08 13:50:34 +04:00
p = Project . order ( 'id DESC' ) . first
2008-08-31 16:59:57 +04:00
p . nil? ? nil : p . identifier . to_s . succ
end
2007-03-12 20:59:02 +03:00
2009-05-04 01:25:37 +04:00
# Copies and saves the Project instance based on the +project+.
2009-10-24 17:30:23 +04:00
# Duplicates the source project's:
# * Wiki
# * Versions
# * Categories
2009-05-04 01:25:37 +04:00
# * Issues
# * Members
# * Queries
2009-10-24 17:30:23 +04:00
#
# Accepts an +options+ argument to specify what to copy
#
# Examples:
# project.copy(1) # => copies everything
# project.copy(1, :only => 'members') # => copies members only
# project.copy(1, :only => ['members', 'versions']) # => copies members and versions
def copy ( project , options = { } )
2009-05-04 01:25:37 +04:00
project = project . is_a? ( Project ) ? project : Project . find ( project )
2011-08-21 12:46:33 +04:00
2009-10-25 14:23:46 +03:00
to_be_copied = %w( wiki versions issue_categories issues members queries boards )
2009-10-24 17:30:23 +04:00
to_be_copied = to_be_copied & options [ :only ] . to_a unless options [ :only ] . nil?
2011-08-21 12:46:33 +04:00
2009-10-24 17:30:23 +04:00
Project . transaction do
2009-10-25 13:30:39 +03:00
if save
reload
to_be_copied . each do | name |
send " copy_ #{ name } " , project
end
Redmine :: Hook . call_hook ( :model_project_copy_before_save , :source_project = > project , :destination_project = > self )
save
2009-05-04 01:25:37 +04:00
end
end
end
2012-12-04 03:03:33 +04:00
# Returns a new unsaved Project instance with attributes copied from +project+
2009-05-04 01:25:37 +04:00
def self . copy_from ( project )
2012-12-04 03:03:33 +04:00
project = project . is_a? ( Project ) ? project : Project . find ( project )
# clear unique attributes
attributes = project . attributes . dup . except ( 'id' , 'name' , 'identifier' , 'status' , 'parent_id' , 'lft' , 'rgt' )
copy = Project . new ( attributes )
copy . enabled_modules = project . enabled_modules
copy . trackers = project . trackers
copy . custom_values = project . custom_values . collect { | v | v . clone }
copy . issue_custom_fields = project . issue_custom_fields
copy
2009-05-04 01:25:37 +04:00
end
2010-10-23 02:38:45 +04:00
# Yields the given block for each project with its level in the tree
def self . project_tree ( projects , & block )
ancestors = [ ]
projects . sort_by ( & :lft ) . each do | project |
2011-08-21 12:46:33 +04:00
while ( ancestors . any? && ! project . is_descendant_of? ( ancestors . last ) )
2010-10-23 02:38:45 +04:00
ancestors . pop
end
yield project , ancestors . size
ancestors << project
end
end
2011-08-21 12:46:33 +04:00
2009-10-24 17:30:23 +04:00
private
2011-08-21 12:46:33 +04:00
2013-02-02 16:50:45 +04:00
def after_parent_changed ( parent_was )
remove_inherited_member_roles
add_inherited_member_roles
end
def update_inherited_members
if parent
if inherit_members? && ! inherit_members_was
remove_inherited_member_roles
add_inherited_member_roles
elsif ! inherit_members? && inherit_members_was
remove_inherited_member_roles
end
end
end
def remove_inherited_member_roles
member_roles = memberships . map ( & :member_roles ) . flatten
member_role_ids = member_roles . map ( & :id )
member_roles . each do | member_role |
if member_role . inherited_from && ! member_role_ids . include? ( member_role . inherited_from )
member_role . destroy
end
end
end
def add_inherited_member_roles
if inherit_members? && parent
parent . memberships . each do | parent_member |
member = Member . find_or_new ( self . id , parent_member . user_id )
parent_member . member_roles . each do | parent_member_role |
member . member_roles << MemberRole . new ( :role = > parent_member_role . role , :inherited_from = > parent_member_role . id )
end
member . save!
end
end
end
2009-10-24 17:30:23 +04:00
# Copies wiki from +project+
def copy_wiki ( project )
2009-10-25 13:30:39 +03:00
# Check that the source project has a wiki first
unless project . wiki . nil?
2013-02-07 23:24:57 +04:00
wiki = self . wiki || Wiki . new
2009-10-25 13:35:59 +03:00
wiki . attributes = project . wiki . attributes . dup . except ( " id " , " project_id " )
2010-02-11 22:30:53 +03:00
wiki_pages_map = { }
2009-10-25 13:30:39 +03:00
project . wiki . pages . each do | page |
2010-02-11 22:30:53 +03:00
# Skip pages without content
next if page . content . nil?
2009-10-25 13:44:03 +03:00
new_wiki_content = WikiContent . new ( page . content . attributes . dup . except ( " id " , " page_id " , " updated_on " ) )
new_wiki_page = WikiPage . new ( page . attributes . dup . except ( " id " , " wiki_id " , " created_on " , " parent_id " ) )
2009-10-25 13:30:39 +03:00
new_wiki_page . content = new_wiki_content
wiki . pages << new_wiki_page
2010-02-11 22:30:53 +03:00
wiki_pages_map [ page . id ] = new_wiki_page
end
2013-02-07 23:24:57 +04:00
self . wiki = wiki
2010-02-11 22:30:53 +03:00
wiki . save
# Reproduce page hierarchy
project . wiki . pages . each do | page |
if page . parent_id && wiki_pages_map [ page . id ]
wiki_pages_map [ page . id ] . parent = wiki_pages_map [ page . parent_id ]
wiki_pages_map [ page . id ] . save
end
2009-10-25 13:30:39 +03:00
end
2009-10-24 17:30:23 +04:00
end
end
# Copies versions from +project+
def copy_versions ( project )
project . versions . each do | version |
new_version = Version . new
2009-10-25 13:44:03 +03:00
new_version . attributes = version . attributes . dup . except ( " id " , " project_id " , " created_on " , " updated_on " )
2009-10-24 17:30:23 +04:00
self . versions << new_version
end
end
# Copies issue categories from +project+
def copy_issue_categories ( project )
project . issue_categories . each do | issue_category |
new_issue_category = IssueCategory . new
2009-10-25 13:35:59 +03:00
new_issue_category . attributes = issue_category . attributes . dup . except ( " id " , " project_id " )
2009-10-24 17:30:23 +04:00
self . issue_categories << new_issue_category
end
end
2011-08-21 12:46:33 +04:00
2009-10-24 17:30:23 +04:00
# Copies issues from +project+
def copy_issues ( project )
2009-12-13 06:37:21 +03:00
# Stores the source issue id as a key and the copied issues as the
# value. Used to map the two togeather for issue relations.
issues_map = { }
2011-08-21 12:46:33 +04:00
2012-09-09 14:11:49 +04:00
# Store status and reopen locked/closed versions
version_statuses = versions . reject ( & :open? ) . map { | version | [ version , version . status ] }
version_statuses . each do | version , status |
version . update_attribute :status , 'open'
end
2010-03-13 17:56:49 +03:00
# Get issues sorted by root_id, lft so that parent issues
# get copied before their children
2014-01-09 04:05:33 +04:00
project . issues . reorder ( 'root_id, lft' ) . each do | issue |
2009-10-24 17:30:23 +04:00
new_issue = Issue . new
2012-09-28 02:27:37 +04:00
new_issue . copy_from ( issue , :subtasks = > false , :link = > false )
2010-03-13 17:56:49 +03:00
new_issue . project = self
2013-05-13 23:52:37 +04:00
# Changing project resets the custom field values
# TODO: handle this in Issue#project=
new_issue . custom_field_values = issue . custom_field_values . inject ( { } ) { | h , v | h [ v . custom_field_id ] = v . value ; h }
2012-09-09 13:51:44 +04:00
# Reassign fixed_versions by name, since names are unique per project
2012-09-09 14:01:03 +04:00
if issue . fixed_version && issue . fixed_version . project == project
2012-09-09 13:51:44 +04:00
new_issue . fixed_version = self . versions . detect { | v | v . name == issue . fixed_version . name }
2009-10-24 17:30:23 +04:00
end
2012-09-09 13:51:44 +04:00
# Reassign the category by name, since names are unique per project
2009-10-24 17:30:23 +04:00
if issue . category
2012-09-09 13:51:44 +04:00
new_issue . category = self . issue_categories . detect { | c | c . name == issue . category . name }
2009-10-24 17:30:23 +04:00
end
2010-03-13 17:56:49 +03:00
# Parent issue
if issue . parent_id
if copied_parent = issues_map [ issue . parent_id ]
new_issue . parent_issue_id = copied_parent . id
end
end
2011-08-21 12:46:33 +04:00
2009-10-24 17:30:23 +04:00
self . issues << new_issue
2010-12-10 20:37:24 +03:00
if new_issue . new_record?
logger . info " Project # copy_issues: issue # #{ issue . id } could not be copied: #{ new_issue . errors . full_messages } " if logger && logger . info
else
issues_map [ issue . id ] = new_issue unless new_issue . new_record?
end
2009-12-13 06:37:21 +03:00
end
2012-09-09 14:11:49 +04:00
# Restore locked/closed version statuses
version_statuses . each do | version , status |
version . update_attribute :status , status
end
2009-12-13 06:37:21 +03:00
# Relations after in case issues related each other
project . issues . each do | issue |
new_issue = issues_map [ issue . id ]
2010-12-10 20:37:24 +03:00
unless new_issue
# Issue was not copied
next
end
2011-08-21 12:46:33 +04:00
2009-12-13 06:37:21 +03:00
# Relations
issue . relations_from . each do | source_relation |
new_issue_relation = IssueRelation . new
new_issue_relation . attributes = source_relation . attributes . dup . except ( " id " , " issue_from_id " , " issue_to_id " )
new_issue_relation . issue_to = issues_map [ source_relation . issue_to_id ]
if new_issue_relation . issue_to . nil? && Setting . cross_project_issue_relations?
new_issue_relation . issue_to = source_relation . issue_to
end
new_issue . relations_from << new_issue_relation
end
2011-08-21 12:46:33 +04:00
2009-12-13 06:37:21 +03:00
issue . relations_to . each do | source_relation |
new_issue_relation = IssueRelation . new
new_issue_relation . attributes = source_relation . attributes . dup . except ( " id " , " issue_from_id " , " issue_to_id " )
new_issue_relation . issue_from = issues_map [ source_relation . issue_from_id ]
if new_issue_relation . issue_from . nil? && Setting . cross_project_issue_relations?
new_issue_relation . issue_from = source_relation . issue_from
end
new_issue . relations_to << new_issue_relation
end
2009-10-24 17:30:23 +04:00
end
end
# Copies members from +project+
def copy_members ( project )
2011-01-02 14:38:35 +03:00
# Copy users first, then groups to handle members with inherited and given roles
members_to_copy = [ ]
members_to_copy += project . memberships . select { | m | m . principal . is_a? ( User ) }
members_to_copy += project . memberships . select { | m | ! m . principal . is_a? ( User ) }
2011-08-21 12:46:33 +04:00
2011-01-02 14:38:35 +03:00
members_to_copy . each do | member |
2009-10-24 17:30:23 +04:00
new_member = Member . new
2009-10-25 13:44:03 +03:00
new_member . attributes = member . attributes . dup . except ( " id " , " project_id " , " created_on " )
2009-12-26 19:14:55 +03:00
# only copy non inherited roles
# inherited roles will be added when copying the group membership
role_ids = member . member_roles . reject ( & :inherited? ) . collect ( & :role_id )
next if role_ids . empty?
new_member . role_ids = role_ids
2009-10-24 17:30:23 +04:00
new_member . project = self
self . members << new_member
end
end
# Copies queries from +project+
def copy_queries ( project )
project . queries . each do | query |
2012-12-09 18:10:49 +04:00
new_query = IssueQuery . new
2009-10-25 13:35:59 +03:00
new_query . attributes = query . attributes . dup . except ( " id " , " project_id " , " sort_criteria " )
2009-10-24 17:30:23 +04:00
new_query . sort_criteria = query . sort_criteria if query . sort_criteria
new_query . project = self
2011-11-11 21:33:02 +04:00
new_query . user_id = query . user_id
2009-10-24 17:30:23 +04:00
self . queries << new_query
end
end
2009-10-25 14:23:46 +03:00
# Copies boards from +project+
def copy_boards ( project )
project . boards . each do | board |
new_board = Board . new
new_board . attributes = board . attributes . dup . except ( " id " , " project_id " , " topics_count " , " messages_count " , " last_message_id " )
new_board . project = self
self . boards << new_board
end
end
2011-08-21 12:46:33 +04:00
2007-09-14 15:34:08 +04:00
def allowed_permissions
@allowed_permissions || = begin
2013-10-20 20:04:04 +04:00
module_names = enabled_modules . loaded? ? enabled_modules . map ( & :name ) : enabled_modules . pluck ( :name )
2007-09-14 15:34:08 +04:00
Redmine :: AccessControl . modules_permissions ( module_names ) . collect { | p | p . name }
end
end
def allowed_actions
@actions_allowed || = allowed_permissions . inject ( [ ] ) { | actions , permission | actions += Redmine :: AccessControl . allowed_actions ( permission ) } . flatten
end
2009-10-22 02:34:39 +04:00
2009-10-22 02:34:45 +04:00
# Returns all the active Systemwide and project specific activities
def active_activities
2009-12-24 21:25:49 +03:00
overridden_activity_ids = self . time_entry_activities . collect ( & :parent_id )
2011-08-21 12:46:33 +04:00
2009-10-22 02:34:45 +04:00
if overridden_activity_ids . empty?
2009-11-21 15:00:49 +03:00
return TimeEntryActivity . shared . active
2009-10-22 02:34:45 +04:00
else
return system_activities_and_project_overrides
end
end
# Returns all the Systemwide and project specific activities
# (inactive and active)
def all_activities
overridden_activity_ids = self . time_entry_activities . collect ( & :parent_id )
if overridden_activity_ids . empty?
2009-11-21 15:00:49 +03:00
return TimeEntryActivity . shared
2009-10-22 02:34:45 +04:00
else
return system_activities_and_project_overrides ( true )
end
end
# Returns the systemwide active activities merged with the project specific overrides
def system_activities_and_project_overrides ( include_inactive = false )
if include_inactive
2009-11-21 15:00:49 +03:00
return TimeEntryActivity . shared .
2012-12-03 00:23:48 +04:00
where ( " id NOT IN (?) " , self . time_entry_activities . collect ( & :parent_id ) ) . all +
2009-10-22 02:34:45 +04:00
self . time_entry_activities
else
2009-11-21 15:00:49 +03:00
return TimeEntryActivity . shared . active .
2012-12-03 00:23:48 +04:00
where ( " id NOT IN (?) " , self . time_entry_activities . collect ( & :parent_id ) ) . all +
2009-10-22 02:34:45 +04:00
self . time_entry_activities . active
end
2009-10-22 02:34:39 +04:00
end
2011-08-21 12:46:33 +04:00
2009-12-06 13:28:20 +03:00
# Archives subprojects recursively
def archive!
children . each do | subproject |
subproject . send :archive!
end
update_attribute :status , STATUS_ARCHIVED
end
2012-08-10 20:50:15 +04:00
def update_position_under_parent
set_or_update_position_under ( parent )
end
2014-01-21 10:29:48 +04:00
public
2012-08-10 20:50:15 +04:00
# Inserts/moves the project so that target's children or root projects stay alphabetically sorted
def set_or_update_position_under ( target_parent )
2013-02-02 16:50:45 +04:00
parent_was = parent
2012-08-10 20:50:15 +04:00
sibs = ( target_parent . nil? ? self . class . roots : target_parent . children )
to_be_inserted_before = sibs . sort_by { | c | c . name . to_s . downcase } . detect { | c | c . name . to_s . downcase > name . to_s . downcase }
if to_be_inserted_before
move_to_left_of ( to_be_inserted_before )
elsif target_parent . nil?
if sibs . empty?
# move_to_root adds the project in first (ie. left) position
move_to_root
else
move_to_right_of ( sibs . last ) unless self == sibs . last
end
else
# move_to_child_of adds the project in last (ie.right) position
move_to_child_of ( target_parent )
end
2013-02-02 16:50:45 +04:00
if parent_was != target_parent
after_parent_changed ( parent_was )
end
2012-08-10 20:50:15 +04:00
end
2006-06-28 22:11:03 +04:00
end