diff --git a/app/controllers/my_profile/profile_design_controller.rb b/app/controllers/my_profile/profile_design_controller.rb index 69b7a62..771c737 100644 --- a/app/controllers/my_profile/profile_design_controller.rb +++ b/app/controllers/my_profile/profile_design_controller.rb @@ -54,4 +54,10 @@ class ProfileDesignController < BoxOrganizerController blocks end + def clone + block = Block.find(params[:id]) + block.duplicate + redirect_to :action => 'index' + end + end diff --git a/app/helpers/boxes_helper.rb b/app/helpers/boxes_helper.rb index db9dee7..e645a68 100644 --- a/app/helpers/boxes_helper.rb +++ b/app/helpers/boxes_helper.rb @@ -210,6 +210,7 @@ module BoxesHelper if !block.main? buttons << icon_button(:delete, _('Remove block'), { :action => 'remove', :id => block.id }, { :method => 'post', :confirm => _('Are you sure you want to remove this block?')}) + buttons << icon_button(:clone, _('Clone'), { :action => 'clone', :id => block.id }, { :method => 'post' }) end if block.respond_to?(:help) diff --git a/app/models/block.rb b/app/models/block.rb index 9fab33b..115b199 100644 --- a/app/models/block.rb +++ b/app/models/block.rb @@ -138,4 +138,14 @@ class Block < ActiveRecord::Base 4.hours end + def duplicate + duplicated_block = self.clone + duplicated_block.enabled = false + duplicated_block.created_at = nil + duplicated_block.updated_at = nil + duplicated_block.save! + duplicated_block.insert_at(self.position + 1) + duplicated_block + end + end diff --git a/public/designs/icons/tango/style.css b/public/designs/icons/tango/style.css index 1881f0c..e0c2806 100644 --- a/public/designs/icons/tango/style.css +++ b/public/designs/icons/tango/style.css @@ -100,6 +100,7 @@ .icon-user-removed { background-image: url(Tango/16x16/actions/gtk-cancel.png) } .icon-user-unknown { background-image: url(Tango/16x16/status/dialog-error.png) } .icon-alert { background-image: url(Tango/16x16/status/dialog-warning.png) } +.icon-clone { background-image: url(Tango/16x16/actions/edit-copy.png) } /******************LARGE ICONS********************/ .image-gallery-item .folder { background-image: url(mod/96x96/places/folder.png) } diff --git a/test/functional/profile_design_controller_test.rb b/test/functional/profile_design_controller_test.rb index b79ee94..ab9f3ba 100644 --- a/test/functional/profile_design_controller_test.rb +++ b/test/functional/profile_design_controller_test.rb @@ -714,4 +714,12 @@ class ProfileDesignControllerTest < ActionController::TestCase assert !@controller.available_blocks.include?(CustomBlock1) end + should 'clone a block' do + block = ProfileImageBlock.create!(:box => profile.boxes.first) + assert_difference ProfileImageBlock, :count, 1 do + post :clone, :id => block.id, :profile => profile.identifier + assert_response :redirect + end + end + end diff --git a/test/unit/block_test.rb b/test/unit/block_test.rb index 5406049..b684b44 100644 --- a/test/unit/block_test.rb +++ b/test/unit/block_test.rb @@ -2,6 +2,8 @@ require File.dirname(__FILE__) + '/../test_helper' class BlockTest < ActiveSupport::TestCase + DUPLICATABLE_FIELDS = [:settings, :title, :box_id, :type] + should 'describe itself' do assert_kind_of String, Block.description end @@ -150,10 +152,49 @@ class BlockTest < ActiveSupport::TestCase should 'delegate environment to box' do box = fast_create(Box, :owner_id => fast_create(Profile).id) - block = Block.new(:box => box) + block = Block.new(:bo => box) box.stubs(:environment).returns(Environment.default) assert_equal box.environment, block.environment end + should 'create a clone block' do + block = fast_create(Block, :title => 'test 1', :position => 1) + assert_difference Block, :count, 1 do + block.duplicate + end + end + + should 'clone and keep some fields' do + box = fast_create(Box, :owner_id => fast_create(Profile).id) + block = TagsBlock.create!(:title => 'test 1', :box_id => box.id, :settings => {:test => 'test'}) + duplicated = block.duplicate + DUPLICATABLE_FIELDS.each do |f| + assert_equal duplicated.send(f), block.send(f) + end + end + + should 'clone block and set fields' do + box = fast_create(Box, :owner_id => fast_create(Profile).id) + block = TagsBlock.create!(:title => 'test 1', :box_id => box.id, :settings => {:test => 'test'}, :position => 1) + block2 = TagsBlock.create!(:title => 'test 2', :box_id => box.id, :settings => {:test => 'test'}, :position => 2) + duplicated = block.duplicate + block2.reload + block.reload + assert_equal false, duplicated.enabled + assert_equal 1, block.position + assert_equal 2, duplicated.position + assert_equal 3, block2.position + end + + should 'not clone date creation and update attributes' do + box = fast_create(Box, :owner_id => fast_create(Profile).id) + block = TagsBlock.create!(:title => 'test 1', :box_id => box.id, :settings => {:test => 'test'}, :position => 1) + duplicated = block.duplicate + + assert_not_equal block.created_at, duplicated.created_at + assert_not_equal block.updated_at, duplicated.updated_at + end + + end -- libgit2 0.21.2