Added the ability to copy a project in the Project Administration panel.
* Added Copy project button. * Added Project#copy_from to duplicate a project to be modified and saved by the user * Added a ProjectsController#copy based off the add method ** Used Project#copy_from to create a duplicate project in memory * Implemented Project#copy to copy data for a project from another and save it. ** Members ** Project level queries ** Project custom fields * Added a plugin hook for Project#copy. #1125 #1556 #886 #309 git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2704 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
@@ -318,6 +318,66 @@ class Project < ActiveRecord::Base
|
||||
p.nil? ? nil : p.identifier.to_s.succ
|
||||
end
|
||||
|
||||
# Copies and saves the Project instance based on the +project+.
|
||||
# Will duplicate the source project's:
|
||||
# * Issues
|
||||
# * Members
|
||||
# * Queries
|
||||
def copy(project)
|
||||
project = project.is_a?(Project) ? project : Project.find(project)
|
||||
|
||||
Project.transaction do
|
||||
# Issues
|
||||
project.issues.each do |issue|
|
||||
new_issue = Issue.new
|
||||
new_issue.copy_from(issue)
|
||||
self.issues << new_issue
|
||||
end
|
||||
|
||||
# Members
|
||||
project.members.each do |member|
|
||||
new_member = Member.new
|
||||
new_member.attributes = member.attributes.dup.except("project_id")
|
||||
new_member.project = self
|
||||
self.members << new_member
|
||||
end
|
||||
|
||||
# Queries
|
||||
project.queries.each do |query|
|
||||
new_query = Query.new
|
||||
new_query.attributes = query.attributes.dup.except("project_id", "sort_criteria")
|
||||
new_query.sort_criteria = query.sort_criteria if query.sort_criteria
|
||||
new_query.project = self
|
||||
self.queries << new_query
|
||||
end
|
||||
|
||||
Redmine::Hook.call_hook(:model_project_copy_before_save, :source_project => project, :destination_project => self)
|
||||
self.save
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# Copies +project+ and returns the new instance. This will not save
|
||||
# the copy
|
||||
def self.copy_from(project)
|
||||
begin
|
||||
project = project.is_a?(Project) ? project : Project.find(project)
|
||||
if project
|
||||
# clear unique attributes
|
||||
attributes = project.attributes.dup.except('name', 'identifier', 'id', 'status')
|
||||
copy = Project.new(attributes)
|
||||
copy.enabled_modules = project.enabled_modules
|
||||
copy.trackers = project.trackers
|
||||
copy.custom_values = project.custom_values.collect {|v| v.clone}
|
||||
return copy
|
||||
else
|
||||
return nil
|
||||
end
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
def validate
|
||||
errors.add(:identifier, :invalid) if !identifier.blank? && identifier.match(/^\d*$/)
|
||||
|
||||
Reference in New Issue
Block a user