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 %> diff --git a/config/locales/en.yml b/config/locales/en.yml index cdc505a53..f3d815167 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -351,6 +351,7 @@ en: label_user_new: New user label_project: Project label_project_new: New project + label_project_copy: Copy project label_project_plural: Projects label_x_projects: zero: no projects diff --git a/test/fixtures/issues.yml b/test/fixtures/issues.yml index 921ba40c4..856f80289 100644 --- a/test/fixtures/issues.yml +++ b/test/fixtures/issues.yml @@ -58,7 +58,7 @@ issues_004: category_id: description: Issue on project 2 tracker_id: 1 - assigned_to_id: + assigned_to_id: 2 author_id: 2 status_id: 1 issues_005: @@ -125,4 +125,4 @@ issues_008: start_date: due_date: lock_version: 0 - \ No newline at end of file + diff --git a/test/fixtures/queries.yml b/test/fixtures/queries.yml index a1bb08eff..563bf583a 100644 --- a/test/fixtures/queries.yml +++ b/test/fixtures/queries.yml @@ -106,4 +106,32 @@ queries_006: --- - - priority - desc - \ No newline at end of file +queries_007: + id: 7 + project_id: 2 + is_public: true + name: Public query for project 2 + filters: | + --- + tracker_id: + :values: + - "3" + :operator: "=" + + user_id: 2 + column_names: +queries_008: + id: 8 + project_id: 2 + is_public: false + name: Private query for project 2 + filters: | + --- + tracker_id: + :values: + - "3" + :operator: "=" + + user_id: 2 + column_names: + diff --git a/test/functional/projects_controller_test.rb b/test/functional/projects_controller_test.rb index 1aded6429..4393ac075 100644 --- a/test/functional/projects_controller_test.rb +++ b/test/functional/projects_controller_test.rb @@ -453,7 +453,6 @@ class ProjectsControllerTest < Test::Unit::TestCase 6.times do |i| p = Project.create!(:name => "Breadcrumbs #{i}", :identifier => "breadcrumbs-#{i}") p.set_parent!(parent) - get :show, :id => p assert_tag :h1, :parent => { :attributes => {:id => 'header'}}, :children => { :count => [i, 3].min, @@ -462,7 +461,24 @@ class ProjectsControllerTest < Test::Unit::TestCase parent = p end end - + + def test_copy_with_project + @request.session[:user_id] = 1 # admin + get :copy, :id => 1 + assert_response :success + assert_template 'copy' + assert assigns(:project) + assert_equal Project.find(1).description, assigns(:project).description + assert_nil assigns(:project).id + end + + def test_copy_without_project + @request.session[:user_id] = 1 # admin + get :copy + assert_response :redirect + assert_redirected_to :controller => 'admin', :action => 'projects' + end + def test_jump_should_redirect_to_active_tab get :show, :id => 1, :jump => 'issues' assert_redirected_to 'projects/ecookbook/issues' diff --git a/test/unit/project_test.rb b/test/unit/project_test.rb index f579e14ff..f9a17e2ec 100644 --- a/test/unit/project_test.rb +++ b/test/unit/project_test.rb @@ -20,7 +20,8 @@ require File.dirname(__FILE__) + '/../test_helper' class ProjectTest < Test::Unit::TestCase fixtures :projects, :enabled_modules, :issues, :issue_statuses, :journals, :journal_details, - :users, :members, :roles, :projects_trackers, :trackers, :boards + :users, :members, :roles, :projects_trackers, :trackers, :boards, + :queries def setup @ecookbook = Project.find(1) @@ -221,6 +222,7 @@ class ProjectTest < Test::Unit::TestCase assert_nil Project.next_identifier end + def test_enabled_module_names_should_not_recreate_enabled_modules project = Project.find(1) # Remove one module @@ -233,4 +235,86 @@ class ProjectTest < Test::Unit::TestCase # Ids should be preserved assert_equal project.enabled_module_ids.sort, modules.collect(&:id).sort end + + def test_copy_from_existing_project + source_project = Project.find(1) + copied_project = Project.copy_from(1) + + assert copied_project + # Cleared attributes + assert copied_project.id.blank? + assert copied_project.name.blank? + assert copied_project.identifier.blank? + + # Duplicated attributes + assert_equal source_project.description, copied_project.description + assert_equal source_project.enabled_modules, copied_project.enabled_modules + assert_equal source_project.trackers, copied_project.trackers + + # Default attributes + assert_equal 1, copied_project.status + end + + # Context: Project#copy + def test_copy_should_copy_issues + # Setup + ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests + source_project = Project.find(2) + Project.destroy_all :identifier => "copy-test" + project = Project.new(:name => 'Copy Test', :identifier => 'copy-test') + project.trackers = source_project.trackers + assert project.valid? + + assert project.issues.empty? + assert project.copy(source_project) + + # Tests + assert_equal source_project.issues.size, project.issues.size + project.issues.each do |issue| + assert issue.valid? + assert ! issue.assigned_to.blank? + assert_equal project, issue.project + end + end + + def test_copy_should_copy_members + # Setup + ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests + source_project = Project.find(2) + project = Project.new(:name => 'Copy Test', :identifier => 'copy-test') + project.trackers = source_project.trackers + project.enabled_modules = source_project.enabled_modules + assert project.valid? + + assert project.members.empty? + assert project.copy(source_project) + + # Tests + assert_equal source_project.members.size, project.members.size + project.members.each do |member| + assert member + assert_equal project, member.project + end + end + + def test_copy_should_copy_project_level_queries + # Setup + ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests + source_project = Project.find(2) + project = Project.new(:name => 'Copy Test', :identifier => 'copy-test') + project.trackers = source_project.trackers + project.enabled_modules = source_project.enabled_modules + assert project.valid? + + assert project.queries.empty? + assert project.copy(source_project) + + # Tests + assert_equal source_project.queries.size, project.queries.size + project.queries.each do |query| + assert query + assert_equal project, query.project + end + end + end