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:
@@ -23,10 +23,10 @@ class ProjectsController < ApplicationController
|
||||
menu_item :settings, :only => :settings
|
||||
menu_item :issues, :only => [:changelog]
|
||||
|
||||
before_filter :find_project, :except => [ :index, :list, :add, :activity ]
|
||||
before_filter :find_project, :except => [ :index, :list, :add, :copy, :activity ]
|
||||
before_filter :find_optional_project, :only => :activity
|
||||
before_filter :authorize, :except => [ :index, :list, :add, :archive, :unarchive, :destroy, :activity ]
|
||||
before_filter :require_admin, :only => [ :add, :archive, :unarchive, :destroy ]
|
||||
before_filter :authorize, :except => [ :index, :list, :add, :copy, :archive, :unarchive, :destroy, :activity ]
|
||||
before_filter :require_admin, :only => [ :add, :copy, :archive, :unarchive, :destroy ]
|
||||
accept_key_auth :activity
|
||||
|
||||
after_filter :only => [:add, :edit, :archive, :unarchive, :destroy] do |controller|
|
||||
@@ -80,6 +80,30 @@ class ProjectsController < ApplicationController
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def copy
|
||||
@issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
|
||||
@trackers = Tracker.all
|
||||
@root_projects = Project.find(:all,
|
||||
:conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}",
|
||||
:order => 'name')
|
||||
if request.get?
|
||||
@project = Project.copy_from(params[:id])
|
||||
if @project
|
||||
@project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
|
||||
else
|
||||
redirect_to :controller => 'admin', :action => 'projects'
|
||||
end
|
||||
else
|
||||
@project = Project.new(params[:project])
|
||||
@project.enabled_module_names = params[:enabled_modules]
|
||||
if @project.copy(params[:id])
|
||||
flash[:notice] = l(:notice_successful_create)
|
||||
redirect_to :controller => 'admin', :action => 'projects'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# Show @project
|
||||
def show
|
||||
|
||||
@@ -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*$/)
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
<th><%=l(:field_created_on)%></th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
</tr></thead>
|
||||
<tbody>
|
||||
<% for project in @projects %>
|
||||
@@ -37,6 +38,9 @@
|
||||
<%= link_to(l(:button_unarchive), { :controller => 'projects', :action => 'unarchive', :id => project }, :method => :post, :class => 'icon icon-unlock') if !project.active? && (project.parent.nil? || project.parent.active?) %>
|
||||
</small>
|
||||
</td>
|
||||
<td align="center" style="width:10%">
|
||||
<%= link_to(l(:button_copy), { :controller => 'projects', :action => 'copy', :id => project }, :class => 'icon icon-copy') %>
|
||||
</td>
|
||||
<td align="center" style="width:10%">
|
||||
<small><%= link_to(l(:button_delete), { :controller => 'projects', :action => 'destroy', :id => project }, :class => 'icon icon-del') %></small>
|
||||
</td>
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
<h2><%=l(:label_project_copy)%></h2>
|
||||
|
||||
<% labelled_tabular_form_for :project, @project, :url => { :action => "copy" } do |f| %>
|
||||
<%= render :partial => 'form', :locals => { :f => f } %>
|
||||
|
||||
<fieldset class="box"><legend><%= l(:label_module_plural) %></legend>
|
||||
<% Redmine::AccessControl.available_project_modules.each do |m| %>
|
||||
<label class="floating">
|
||||
<%= check_box_tag 'enabled_modules[]', m, @project.module_enabled?(m) %>
|
||||
<%= l_or_humanize(m, :prefix => "project_module_") %>
|
||||
</label>
|
||||
<% end %>
|
||||
</fieldset>
|
||||
|
||||
<%= submit_tag l(:button_copy) %>
|
||||
<% end %>
|
||||
Reference in New Issue
Block a user