diff options
author | Eric Davis <edavis@littlestreamsoftware.com> | 2009-05-03 21:25:37 +0000 |
---|---|---|
committer | Eric Davis <edavis@littlestreamsoftware.com> | 2009-05-03 21:25:37 +0000 |
commit | fa7bd1c71dca1e9c74e6d83277336321393dac9f (patch) | |
tree | 4f1adac8a535ae7424887ea21f6171a22eb698ea /app | |
parent | 29c0dae1518ecb8a86d10da8e05caf70f731d746 (diff) | |
download | redmine-fa7bd1c71dca1e9c74e6d83277336321393dac9f.tar.gz redmine-fa7bd1c71dca1e9c74e6d83277336321393dac9f.zip |
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
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/projects_controller.rb | 30 | ||||
-rw-r--r-- | app/models/project.rb | 60 | ||||
-rw-r--r-- | app/views/admin/projects.rhtml | 4 | ||||
-rw-r--r-- | app/views/projects/copy.rhtml | 16 |
4 files changed, 107 insertions, 3 deletions
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 @@ <th><%=l(:field_created_on)%></th> <th></th> <th></th> + <th></th> </tr></thead> <tbody> <% for project in @projects %> @@ -38,6 +39,9 @@ </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> </tr> 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 @@ +<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 %> |