diff --git a/app/models/block.rb b/app/models/block.rb index 36b173c..4921cb3 100644 --- a/app/models/block.rb +++ b/app/models/block.rb @@ -234,4 +234,9 @@ class Block < ActiveRecord::Base duplicated_block end + def copy_from(block) + self.settings = block.settings + self.position = block.position + end + end diff --git a/app/models/profile.rb b/app/models/profile.rb index 3482a52..0879322 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -346,16 +346,17 @@ class Profile < ActiveRecord::Base end def copy_blocks_from(profile) + template_boxes = profile.boxes.select{|box| box.position} self.boxes.destroy_all - profile.boxes.each do |box| - new_box = Box.new + self.boxes = template_boxes.size.times.map { Box.new } + + template_boxes.each_with_index do |box, i| + new_box = self.boxes[i] new_box.position = box.position - self.boxes << new_box box.blocks.each do |block| new_block = block.class.new(:title => block[:title]) - new_block.settings = block.settings - new_block.position = block.position - self.boxes[-1].blocks << new_block + new_block.copy_from(block) + new_box.blocks << new_block end end end diff --git a/plugins/container_block/lib/container_block_plugin/container_block.rb b/plugins/container_block/lib/container_block_plugin/container_block.rb index 32a9ef3..0cb4609 100644 --- a/plugins/container_block/lib/container_block_plugin/container_block.rb +++ b/plugins/container_block/lib/container_block_plugin/container_block.rb @@ -38,9 +38,11 @@ class ContainerBlockPlugin::ContainerBlock < Block end def create_box - container_box = Box.create!(:owner => owner) - container_box.update_attribute(:position, nil) + container_box = Box.new(:owner => owner) + container_box.save! settings[:container_box_id] = container_box.id + copy_blocks unless @blocks_to_copy.blank? + container_box.update_attribute(:position, nil) save! end @@ -71,4 +73,23 @@ class ContainerBlockPlugin::ContainerBlock < Block end end + def copy_from_with_container(block) + copy_from_without_container(block) + children_settings = block.children_settings + @blocks_to_copy = block.blocks + end + + alias_method_chain :copy_from, :container + + def copy_blocks + new_children_settings = {} + @blocks_to_copy.map do |child| + new_block = child.class.new(:title => child[:title]) + new_block.copy_from(child) + container_box.blocks << new_block + new_children_settings[new_block.id] = children_settings[child.id] if children_settings[child.id] + end + settings[:children_settings] = new_children_settings + end + end diff --git a/plugins/container_block/test/unit/profile_test.rb b/plugins/container_block/test/unit/profile_test.rb new file mode 100644 index 0000000..f0a6af8 --- /dev/null +++ b/plugins/container_block/test/unit/profile_test.rb @@ -0,0 +1,47 @@ +require 'test_helper' + +class ProfileTest < ActiveSupport::TestCase + + should 'not lose position values for boxes when the template has a container block' do + template = fast_create(Profile) + template.boxes = [Box.new, Box.new] + template.boxes[0].blocks << ContainerBlockPlugin::ContainerBlock.new + template.boxes[1].blocks << Block.new + template.is_template = true + template.save! + + p = Profile.new + p.identifier = 'profile-with-template' + p.name = p.identifier + p.template = template.reload + p.save! + + assert_equivalent p.reload.boxes.map(&:position), template.reload.boxes.map(&:position) + end + + should 'copy contents of a container block that belongs to the template' do + template = fast_create(Profile) + template.boxes = [Box.new] + template.boxes[0].blocks << ContainerBlockPlugin::ContainerBlock.new + template.is_template = true + template.save! + + container = template.blocks.first + container.container_box.blocks << Block.new + container.container_box.blocks << Block.new + container.settings[:children_settings] = {} + container.settings[:children_settings][container.blocks.last.id] = 999 + container.save! + + p = Profile.new + p.identifier = 'another-profile-with-template' + p.name = p.identifier + p.template = template.reload + p.save! + + container_copy = p.blocks.first + assert_equal({container_copy.blocks.last.id => 999}, container_copy.children_settings) + assert_equal container.blocks.size, p.blocks.first.blocks.size + end + +end -- libgit2 0.21.2