Commit e1fa81e8d1460e819cdf811172ae5e2bebdbb6c6
Exists in
staging
and in
4 other branches
Merge branch 'fix_template_with_container_block' into rails3_stable
Showing
4 changed files
with
82 additions
and
8 deletions
Show diff stats
app/models/block.rb
@@ -234,4 +234,9 @@ class Block < ActiveRecord::Base | @@ -234,4 +234,9 @@ class Block < ActiveRecord::Base | ||
234 | duplicated_block | 234 | duplicated_block |
235 | end | 235 | end |
236 | 236 | ||
237 | + def copy_from(block) | ||
238 | + self.settings = block.settings | ||
239 | + self.position = block.position | ||
240 | + end | ||
241 | + | ||
237 | end | 242 | end |
app/models/profile.rb
@@ -380,16 +380,17 @@ class Profile < ActiveRecord::Base | @@ -380,16 +380,17 @@ class Profile < ActiveRecord::Base | ||
380 | end | 380 | end |
381 | 381 | ||
382 | def copy_blocks_from(profile) | 382 | def copy_blocks_from(profile) |
383 | + template_boxes = profile.boxes.select{|box| box.position} | ||
383 | self.boxes.destroy_all | 384 | self.boxes.destroy_all |
384 | - profile.boxes.each do |box| | ||
385 | - new_box = Box.new | 385 | + self.boxes = template_boxes.size.times.map { Box.new } |
386 | + | ||
387 | + template_boxes.each_with_index do |box, i| | ||
388 | + new_box = self.boxes[i] | ||
386 | new_box.position = box.position | 389 | new_box.position = box.position |
387 | - self.boxes << new_box | ||
388 | box.blocks.each do |block| | 390 | box.blocks.each do |block| |
389 | new_block = block.class.new(:title => block[:title]) | 391 | new_block = block.class.new(:title => block[:title]) |
390 | - new_block.settings = block.settings | ||
391 | - new_block.position = block.position | ||
392 | - self.boxes[-1].blocks << new_block | 392 | + new_block.copy_from(block) |
393 | + new_box.blocks << new_block | ||
393 | end | 394 | end |
394 | end | 395 | end |
395 | end | 396 | end |
plugins/container_block/lib/container_block_plugin/container_block.rb
@@ -38,9 +38,11 @@ class ContainerBlockPlugin::ContainerBlock < Block | @@ -38,9 +38,11 @@ class ContainerBlockPlugin::ContainerBlock < Block | ||
38 | end | 38 | end |
39 | 39 | ||
40 | def create_box | 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 | settings[:container_box_id] = container_box.id | 43 | settings[:container_box_id] = container_box.id |
44 | + copy_blocks unless @blocks_to_copy.blank? | ||
45 | + container_box.update_attribute(:position, nil) | ||
44 | save! | 46 | save! |
45 | end | 47 | end |
46 | 48 | ||
@@ -71,4 +73,23 @@ class ContainerBlockPlugin::ContainerBlock < Block | @@ -71,4 +73,23 @@ class ContainerBlockPlugin::ContainerBlock < Block | ||
71 | end | 73 | end |
72 | end | 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 | end | 95 | end |
@@ -0,0 +1,47 @@ | @@ -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 |