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