Commit 7fefda6ae0b3a0f21577762c047afa22a5cad114
1 parent
7e2cd9cf
Exists in
master
and in
29 other branches
Destroy children of container block when it was destroyed
Showing
2 changed files
with
18 additions
and
0 deletions
Show diff stats
plugins/container_block/lib/container_block.rb
1 | class ContainerBlock < Block | 1 | class ContainerBlock < Block |
2 | 2 | ||
3 | after_create :create_box | 3 | after_create :create_box |
4 | + after_destroy :destroy_children | ||
4 | 5 | ||
5 | settings_items :container_box_id, :type => Integer, :default => nil | 6 | settings_items :container_box_id, :type => Integer, :default => nil |
6 | settings_items :children_settings, :type => Hash, :default => {} | 7 | settings_items :children_settings, :type => Hash, :default => {} |
@@ -17,6 +18,10 @@ class ContainerBlock < Block | @@ -17,6 +18,10 @@ class ContainerBlock < Block | ||
17 | nil | 18 | nil |
18 | end | 19 | end |
19 | 20 | ||
21 | + def destroy_children | ||
22 | + blocks.destroy_all | ||
23 | + end | ||
24 | + | ||
20 | def create_box | 25 | def create_box |
21 | box = Box.create!(:owner => self) | 26 | box = Box.create!(:owner => self) |
22 | settings[:container_box_id] = box.id | 27 | settings[:container_box_id] = box.id |
plugins/container_block/test/unit/container_block_test.rb
@@ -69,4 +69,17 @@ class ContainerBlockTest < ActiveSupport::TestCase | @@ -69,4 +69,17 @@ class ContainerBlockTest < ActiveSupport::TestCase | ||
69 | assert_equal nil, @block.layout_template | 69 | assert_equal nil, @block.layout_template |
70 | end | 70 | end |
71 | 71 | ||
72 | + should 'return children blocks that have container box as box' do | ||
73 | + @block.save! | ||
74 | + child = Block.create!(:box_id => @block.container_box.id) | ||
75 | + assert_equal [child], @block.blocks | ||
76 | + end | ||
77 | + | ||
78 | + should 'destroy chilrend when container is removed' do | ||
79 | + @block.save! | ||
80 | + child = Block.create!(:box_id => @block.container_box.id) | ||
81 | + @block.destroy | ||
82 | + assert !Block.exists?(child.id) | ||
83 | + end | ||
84 | + | ||
72 | end | 85 | end |