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