From 600df031f5c53428528bffdb97d3304a7199ddc1 Mon Sep 17 00:00:00 2001 From: Pavel Chipiga Date: Tue, 21 Feb 2012 23:27:11 +0200 Subject: [PATCH] Refactor platform clone controller and routes. Refactor and fix clone process for platform, repository, product. Place XML RPC clone request to DJ. Refs #207 --- app/controllers/platforms_controller.rb | 26 +++++++++++++------------ app/models/platform.rb | 12 +++++------- app/models/product.rb | 6 +++--- app/models/repository.rb | 6 +++--- app/views/platforms/clone.html.haml | 2 +- config/routes.rb | 2 +- 6 files changed, 27 insertions(+), 27 deletions(-) diff --git a/app/controllers/platforms_controller.rb b/app/controllers/platforms_controller.rb index 060fe99e0..06eb87c45 100644 --- a/app/controllers/platforms_controller.rb +++ b/app/controllers/platforms_controller.rb @@ -109,19 +109,21 @@ class PlatformsController < ApplicationController end def clone - if request.post? - @cloned = @platform.make_clone(:name => params[:platform]['name'], :description => params[:platform]['description'], - :owner_id => current_user.id, :owner_type => current_user.class.to_s) - if @cloned.persisted? - flash[:notice] = I18n.t("flash.platform.clone_success") - redirect_to @cloned - else - flash[:error] = @cloned.errors.full_messages.join('. ') - end + @cloned = Platform.new + @cloned.name = @platform.name + "_clone" + @cloned.description = @platform.description + "_clone" + end + + def make_clone + @cloned = @platform.make_clone(:name => params[:platform]['name'], :description => params[:platform]['description'], + :owner_id => current_user.id, :owner_type => current_user.class.to_s) + if @cloned.persisted? # valid? + flash[:notice] = I18n.t("flash.platform.clone_success") + redirect_to @cloned else - @cloned = Platform.new - @cloned.name = @platform.name + "_clone" - @cloned.description = @platform.description + "_clone" + raise @cloned.repositories.first.inspect + flash[:error] = @cloned.errors.full_messages.join('. ') + render 'clone' end end diff --git a/app/models/platform.rb b/app/models/platform.rb index e7b552ba2..773aa3111 100644 --- a/app/models/platform.rb +++ b/app/models/platform.rb @@ -98,23 +98,21 @@ class Platform < ActiveRecord::Base platform_type == 'personal' end - def full_clone(attrs) # :description, :name, :owner + def full_clone(attrs = {}) # :description, :name, :owner clone.tap do |c| - c.attributes = attrs + c.attributes = attrs # do not set protected c.updated_at = nil; c.created_at = nil # :id = nil c.parent = self - new_attrs = {:platform_id => nil} - c.repositories = repositories.map{|r| r.full_clone(new_attrs.merge(:owner_id => attrs[:owner_id], :owner_type => attrs[:owner_type]))} - c.products = products.map{|p| p.full_clone(new_attrs)} + c.repositories = repositories.map(&:full_clone) + c.products = products.map(&:full_clone) end end - # TODO * make it Delayed Job * def make_clone(attrs) p = full_clone(attrs) begin Thread.current[:skip] = true - p.save and xml_rpc_clone(attrs[:name]) + p.save and self.delay.xml_rpc_clone(attrs[:name]) ensure Thread.current[:skip] = false end diff --git a/app/models/product.rb b/app/models/product.rb index 94e355d20..14cd10549 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -59,10 +59,10 @@ class Product < ActiveRecord::Base EOF end - def full_clone(attrs) # owner + def full_clone(attrs = {}) clone.tap do |c| # dup - c.attributes = attrs - c.updated_at = nil; c.created_at = nil # :id = nil + c.attributes = attrs # do not set protected + c.platform_id = nil; c.updated_at = nil; c.created_at = nil # :id = nil end end diff --git a/app/models/repository.rb b/app/models/repository.rb index f5c7d4c13..79e49a345 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -15,10 +15,10 @@ class Repository < ActiveRecord::Base attr_accessible :description, :name - def full_clone(attrs) # owner + def full_clone(attrs = {}) clone.tap do |c| # dup - c.attributes = attrs - c.updated_at = nil; c.created_at = nil # :id = nil + c.attributes = attrs # do not set protected + c.platform_id = nil; c.updated_at = nil; c.created_at = nil # :id = nil c.projects = projects end end diff --git a/app/views/platforms/clone.html.haml b/app/views/platforms/clone.html.haml index 045e2956a..01eb9b7be 100644 --- a/app/views/platforms/clone.html.haml +++ b/app/views/platforms/clone.html.haml @@ -8,7 +8,7 @@ %h2.title = t("layout.platforms.clone_header") .inner - = form_for @cloned, :url => clone_platform_path(@platform), :html => { :class => :form } do |f| + = form_for @cloned, :url => make_clone_platform_path(@platform), :html => { :class => :form } do |f| .group = f.label :name, :class => :label = f.text_field :name, :class => 'text_field' diff --git a/config/routes.rb b/config/routes.rb index e8c872689..b3ef45c46 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -71,7 +71,7 @@ Rosa::Application.routes.draw do post 'freeze' post 'unfreeze' get 'clone' - post 'clone' + post 'make_clone' post 'build_all' end