From fa7bd1c71dca1e9c74e6d83277336321393dac9f Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Sun, 3 May 2009 21:25:37 +0000 Subject: 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 --- app/controllers/projects_controller.rb | 30 +++++++++++++++-- app/models/project.rb | 60 ++++++++++++++++++++++++++++++++++ app/views/admin/projects.rhtml | 4 +++ app/views/projects/copy.rhtml | 16 +++++++++ 4 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 app/views/projects/copy.rhtml (limited to 'app') diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index f9c537cfc..b663291de 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -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 diff --git a/app/models/project.rb b/app/models/project.rb index bddd66362..261844e8e 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -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*$/) diff --git a/app/views/admin/projects.rhtml b/app/views/admin/projects.rhtml index 40177a63b..1acef091f 100644 --- a/app/views/admin/projects.rhtml +++ b/app/views/admin/projects.rhtml @@ -23,6 +23,7 @@ <%=l(:field_created_on)%> + <% 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?) %> + + <%= link_to(l(:button_copy), { :controller => 'projects', :action => 'copy', :id => project }, :class => 'icon icon-copy') %> + <%= link_to(l(:button_delete), { :controller => 'projects', :action => 'destroy', :id => project }, :class => 'icon icon-del') %> diff --git a/app/views/projects/copy.rhtml b/app/views/projects/copy.rhtml new file mode 100644 index 000000000..a5c4e140f --- /dev/null +++ b/app/views/projects/copy.rhtml @@ -0,0 +1,16 @@ +

<%=l(:label_project_copy)%>

+ +<% labelled_tabular_form_for :project, @project, :url => { :action => "copy" } do |f| %> +<%= render :partial => 'form', :locals => { :f => f } %> + +
<%= l(:label_module_plural) %> +<% Redmine::AccessControl.available_project_modules.each do |m| %> + +<% end %> +
+ +<%= submit_tag l(:button_copy) %> +<% end %> -- cgit v1.2.3