Commit 097aa929503bf39829edf02f86b269fd51860a61
1 parent
f323c51d
Exists in
master
and in
29 other branches
container_block: fix the use of container blocks with templates
fixes #7
Showing
4 changed files
with
82 additions
and
8 deletions
Show diff stats
app/models/block.rb
app/models/profile.rb
... | ... | @@ -346,16 +346,17 @@ class Profile < ActiveRecord::Base |
346 | 346 | end |
347 | 347 | |
348 | 348 | def copy_blocks_from(profile) |
349 | + template_boxes = profile.boxes.select{|box| box.position} | |
349 | 350 | self.boxes.destroy_all |
350 | - profile.boxes.each do |box| | |
351 | - new_box = Box.new | |
351 | + self.boxes = template_boxes.size.times.map { Box.new } | |
352 | + | |
353 | + template_boxes.each_with_index do |box, i| | |
354 | + new_box = self.boxes[i] | |
352 | 355 | new_box.position = box.position |
353 | - self.boxes << new_box | |
354 | 356 | box.blocks.each do |block| |
355 | 357 | new_block = block.class.new(:title => block[:title]) |
356 | - new_block.settings = block.settings | |
357 | - new_block.position = block.position | |
358 | - self.boxes[-1].blocks << new_block | |
358 | + new_block.copy_from(block) | |
359 | + new_box.blocks << new_block | |
359 | 360 | end |
360 | 361 | end |
361 | 362 | end | ... | ... |
plugins/container_block/lib/container_block_plugin/container_block.rb
... | ... | @@ -38,9 +38,11 @@ class ContainerBlockPlugin::ContainerBlock < Block |
38 | 38 | end |
39 | 39 | |
40 | 40 | def create_box |
41 | - container_box = Box.create!(:owner => owner) | |
42 | - container_box.update_attribute(:position, nil) | |
41 | + container_box = Box.new(:owner => owner) | |
42 | + container_box.save! | |
43 | 43 | settings[:container_box_id] = container_box.id |
44 | + copy_blocks unless @blocks_to_copy.blank? | |
45 | + container_box.update_attribute(:position, nil) | |
44 | 46 | save! |
45 | 47 | end |
46 | 48 | |
... | ... | @@ -71,4 +73,23 @@ class ContainerBlockPlugin::ContainerBlock < Block |
71 | 73 | end |
72 | 74 | end |
73 | 75 | |
76 | + def copy_from_with_container(block) | |
77 | + copy_from_without_container(block) | |
78 | + children_settings = block.children_settings | |
79 | + @blocks_to_copy = block.blocks | |
80 | + end | |
81 | + | |
82 | + alias_method_chain :copy_from, :container | |
83 | + | |
84 | + def copy_blocks | |
85 | + new_children_settings = {} | |
86 | + @blocks_to_copy.map do |child| | |
87 | + new_block = child.class.new(:title => child[:title]) | |
88 | + new_block.copy_from(child) | |
89 | + container_box.blocks << new_block | |
90 | + new_children_settings[new_block.id] = children_settings[child.id] if children_settings[child.id] | |
91 | + end | |
92 | + settings[:children_settings] = new_children_settings | |
93 | + end | |
94 | + | |
74 | 95 | end | ... | ... |
... | ... | @@ -0,0 +1,47 @@ |
1 | +require 'test_helper' | |
2 | + | |
3 | +class ProfileTest < ActiveSupport::TestCase | |
4 | + | |
5 | + should 'not lose position values for boxes when the template has a container block' do | |
6 | + template = fast_create(Profile) | |
7 | + template.boxes = [Box.new, Box.new] | |
8 | + template.boxes[0].blocks << ContainerBlockPlugin::ContainerBlock.new | |
9 | + template.boxes[1].blocks << Block.new | |
10 | + template.is_template = true | |
11 | + template.save! | |
12 | + | |
13 | + p = Profile.new | |
14 | + p.identifier = 'profile-with-template' | |
15 | + p.name = p.identifier | |
16 | + p.template = template.reload | |
17 | + p.save! | |
18 | + | |
19 | + assert_equivalent p.reload.boxes.map(&:position), template.reload.boxes.map(&:position) | |
20 | + end | |
21 | + | |
22 | + should 'copy contents of a container block that belongs to the template' do | |
23 | + template = fast_create(Profile) | |
24 | + template.boxes = [Box.new] | |
25 | + template.boxes[0].blocks << ContainerBlockPlugin::ContainerBlock.new | |
26 | + template.is_template = true | |
27 | + template.save! | |
28 | + | |
29 | + container = template.blocks.first | |
30 | + container.container_box.blocks << Block.new | |
31 | + container.container_box.blocks << Block.new | |
32 | + container.settings[:children_settings] = {} | |
33 | + container.settings[:children_settings][container.blocks.last.id] = 999 | |
34 | + container.save! | |
35 | + | |
36 | + p = Profile.new | |
37 | + p.identifier = 'another-profile-with-template' | |
38 | + p.name = p.identifier | |
39 | + p.template = template.reload | |
40 | + p.save! | |
41 | + | |
42 | + container_copy = p.blocks.first | |
43 | + assert_equal({container_copy.blocks.last.id => 999}, container_copy.children_settings) | |
44 | + assert_equal container.blocks.size, p.blocks.first.blocks.size | |
45 | + end | |
46 | + | |
47 | +end | ... | ... |