Changeset 125

Show
Ignore:
Timestamp:
08/03/08 17:24:58 (4 months ago)
Author:
jweiss
Message:

Ability to clone projects. close #85

Location:
trunk
Files:
1 added
8 modified

Legend:

Unmodified
Added
Removed
  • trunk/CHANGELOG.txt

    r124 r125  
    11 
    22SVN 
     3 
     4* Allow to clone a project. Stages and configuration is cloned for now. Closes #85 
    35 
    46* Update to Capistrano 2.4.3 
  • trunk/app/controllers/projects_controller.rb

    r60 r125  
    3939  def new 
    4040    @project = Project.new 
     41    if load_clone_original 
     42      @project.prepare_cloning(@original) 
     43      render :action => 'clone' and return 
     44    end 
    4145  end 
    4246 
     
    5054  def create 
    5155    @project = Project.new(params[:project]) 
    52  
     56     
     57    if load_clone_original 
     58      action_to_render = 'clone'   
     59    else 
     60      action_to_render = 'new'   
     61    end 
     62       
    5363    respond_to do |format| 
    5464      if @project.save 
     65         
     66        @project.clone(@original) if load_clone_original 
     67         
    5568        flash[:notice] = 'Project was successfully created.' 
    5669        format.html { redirect_to project_url(@project) } 
    5770        format.xml  { head :created, :location => project_url(@project) } 
    5871      else 
    59         format.html { render :action => "new" } 
     72        format.html { render :action => action_to_render } 
    6073        format.xml  { render :xml => @project.errors.to_xml } 
    6174      end 
     
    103116    end 
    104117  end 
     118   
     119  def load_clone_original 
     120    if params[:clone] 
     121      @original = Project.find(params[:clone]) 
     122    else 
     123      false 
     124    end 
     125  end 
    105126end 
  • trunk/app/helpers/projects_helper.rb

    r1 r125  
    11module ProjectsHelper 
     2   
     3  def clone_form_path(project) 
     4    "#{new_project_path}?clone=#{project.id}" 
     5  end 
     6   
     7  def clone_path(project) 
     8    "#{projects_path}?clone=#{project.id}" 
     9  end 
     10   
    211end 
  • trunk/app/models/project.rb

    r114 r125  
    3636    self.name.underscore.gsub(/[^a-zA-Z0-9\-\_]/, '_') 
    3737  end 
     38   
     39  def prepare_cloning(other) 
     40    self.name = "Clone of #{other.name}" 
     41    self.description = "Clone of #{other.name}: #{other.description}" 
     42    self.template = other.template 
     43  end 
     44   
     45  def clone(other)   
     46    self.stages.destroy_all 
     47    self.configuration_parameters.destroy_all 
     48       
     49    other.configuration_parameters.each do |conf| 
     50      self.configuration_parameters << ProjectConfiguration.new(:name => conf.name, :value => conf.value, :prompt_on_deploy => conf.prompt_on_deploy) 
     51    end 
     52     
     53    other.stages.each do |stage| 
     54      new_stage = Stage.new 
     55      new_stage.project = self 
     56      new_stage.name = stage.name 
     57      new_stage.alert_emails = stage.alert_emails 
     58      new_stage.save! 
     59       
     60      stage.configuration_parameters.each do |conf| 
     61        new_stage.configuration_parameters << StageConfiguration.new(:name => conf.name, :value => conf.value, :prompt_on_deploy => conf.prompt_on_deploy) 
     62      end 
     63       
     64      stage.recipes.each do |recipe| 
     65        new_stage.recipes << recipe 
     66      end 
     67    end 
     68     
     69    self.reload 
     70  end 
    3871     
    3972end 
  • trunk/app/views/projects/show.html.erb

    r73 r125  
    3535                </p><br /> 
    3636                <%= link_to 'Edit Project', edit_project_path(@project), :class => 'arrow_link' %> |  
     37                <%= link_to 'Clone Project', clone_form_path(@project), :class => 'arrow_link' %> |  
    3738                <%= link_to 'Delete', project_path(@project), :confirm => 'Are you sure?', :method => :delete, :class => 'arrow_link' %> 
    3839        </div>   
  • trunk/test/functional/projects_controller_test.rb

    r56 r125  
    121121  end 
    122122   
     123  def test_clone 
     124    @user = admin_login 
     125    @project.template = "mod_rails" 
     126    @project.save! 
     127    assert_difference "Project.count", 1 do 
     128      get :new, :clone => @project.id 
     129      assert_response :success 
     130      assert_select "h2", "Clone project" 
     131      post :new, :clone => @project.id, :project => { :name => 'MyClone' } 
     132    end 
     133    assert_equal "mod_rails". Project.last.template 
     134  end 
     135   
    123136end 
  • trunk/test/test_helper.rb

    r90 r125  
    4343    options = { 
    4444      :name => random_string, 
     45      :description => random_string, 
    4546      :template => 'rails' 
    4647    }.update(options) 
     
    4849    p = Project.new 
    4950    p.name = options[:name] 
     51    p.description = options[:description] 
    5052    p.template = options[:template] 
    5153    p.save! 
  • trunk/test/unit/project_test.rb

    r1 r125  
    8484  end 
    8585   
     86  def test_prepare_cloning 
     87    original = create_new_project(:name => 'Some Project', :template => 'mod_rails', :description => "Dr. Foo") 
     88    my = create_new_project(:template => 'mongrel_rails') 
     89     
     90    my.prepare_cloning(original) 
     91    assert_equal "Clone of Some Project", my.name 
     92    assert_equal "Clone of Some Project: Dr. Foo", my.description 
     93    assert_equal original.template, my.template 
     94  end 
     95   
     96  def test_clone 
     97    # setup 
     98    original = create_new_project(:name => 'Some Project', :template => 'mod_rails') 
     99    3.times do |i| 
     100      create_new_project_configuration(:project => original, :name => "#{i}-project-conf", :value => "value-#{i}") 
     101    end 
     102    stage_1 = create_new_stage(:project => original, :name => 'test') 
     103      create_new_stage_configuration(:stage => stage_1, :name => "stage1-conf", :value => "stage1-value") 
     104    stage_2 = create_new_stage(:project => original, :name => 'prod') 
     105      create_new_stage_configuration(:stage => stage_2, :name => "stage2-conf", :value => "stage2-value") 
     106    recipe = create_new_recipe 
     107    stage_1.recipes << recipe 
     108       
     109    new_project = create_new_project 
     110    new_project.clone(original) 
     111     
     112    # check project configuration 
     113    assert_equal original.configuration_parameters.count, new_project.configuration_parameters.count 
     114    new_project.configuration_parameters.each_with_index do |conf, i| 
     115      orig = original.configuration_parameters.find_by_name(conf.name) 
     116       
     117      if conf.name == 'application' 
     118        assert_match 'some_project',conf.value 
     119      else 
     120        assert_equal orig.name, conf.name 
     121        assert_equal orig.value, conf.value 
     122        assert_equal orig.prompt_on_deploy, conf.prompt_on_deploy 
     123      end 
     124    end 
     125 
     126    # check stages 
     127    assert_equal 2, new_project.stages.count 
     128    cloned_stage_1 = new_project.stages.find_by_name("test") 
     129    cloned_stage_2 = new_project.stages.find_by_name("prod") 
     130    assert_equal stage_1.configuration_parameters.first.name, cloned_stage_1.configuration_parameters.first.name  
     131    assert_equal stage_1.configuration_parameters.first.value, cloned_stage_1.configuration_parameters.first.value 
     132    assert_equal stage_2.configuration_parameters.first.name, cloned_stage_2.configuration_parameters.first.name  
     133    assert_equal stage_2.configuration_parameters.first.value, cloned_stage_2.configuration_parameters.first.value 
     134    assert_equal [recipe], cloned_stage_1.recipes 
     135  end 
     136   
    86137end