Commit a770c8f376086a96153b9dbee7366a8a4a29678a

Authored by Victor Costa
1 parent 0e4d4a79

rails3: fix container_block plugin

plugins/container_block/lib/container_block_plugin/container_block.rb
... ... @@ -38,7 +38,7 @@ class ContainerBlockPlugin::ContainerBlock < Block
38 38 end
39 39  
40 40 def block_classes=(classes)
41   - classes.each { |c| block = c.constantize.create!(:box => container_box) } if classes
  41 + classes.each { |c| block = c.constantize.create!(:box_id => container_box.id) } if classes
42 42 end
43 43  
44 44 def blocks
... ... @@ -51,8 +51,8 @@ class ContainerBlockPlugin::ContainerBlock < Block
51 51  
52 52 def content(args={})
53 53 block = self
54   - lambda do
55   - render :file => 'blocks/container.rhtml', :locals => {:block => block}
  54 + proc do
  55 + render :file => 'blocks/container', :locals => {:block => block}
56 56 end
57 57 end
58 58  
... ...
plugins/container_block/test/functional/container_block_environment_design_controller_test.rb
... ... @@ -20,7 +20,7 @@ class EnvironmentDesignControllerTest < ActionController::TestCase
20 20 @environment.add_admin(user.person)
21 21 login_as(user.login)
22 22  
23   - @block = ContainerBlockPlugin::ContainerBlock.create!(:box => @environment.boxes.first)
  23 + @block = create(ContainerBlockPlugin::ContainerBlock, :box => @environment.boxes.first)
24 24 end
25 25  
26 26 should 'be able to edit ContainerBlock' do
... ... @@ -36,46 +36,46 @@ class EnvironmentDesignControllerTest < ActionController::TestCase
36 36 end
37 37  
38 38 should 'display container children' do
39   - c1 = RawHTMLBlock.create!(:box => @block.container_box, :html => 'child1 content')
  39 + c1 = RawHTMLBlock.create!(:box_id => @block.container_box.id, :html => 'child1 content')
40 40 get :index
41 41 assert_tag :div, :attributes => { :id => "block-#{c1.id}" }
42 42 end
43 43  
44 44 should 'display hidden children of container block' do
45   - c1 = RawHTMLBlock.create!(:box => @block.container_box, :html => 'child1 content', :display => 'never')
  45 + c1 = RawHTMLBlock.create!(:box_id => @block.container_box.id, :html => 'child1 content', :display => 'never')
46 46 get :index
47 47 assert_tag :div, :attributes => { :id => "block-#{c1.id}", :class => 'block raw-html-block invisible-block' }
48 48 end
49 49  
50 50 should 'display button to save widths of container children' do
51   - c1 = RawHTMLBlock.create!(:box => @block.container_box, :html => 'child1 content')
  51 + c1 = RawHTMLBlock.create!(:box_id => @block.container_box.id, :html => 'child1 content')
52 52 get :index
53 53 assert_tag :a, :attributes => { :class => "button icon-save container_block_save" }
54 54 end
55 55  
56 56 should 'move child of container block to another box' do
57   - c1 = RawHTMLBlock.create!(:box => @block.container_box, :html => 'child1 content')
  57 + c1 = RawHTMLBlock.create!(:box_id => @block.container_box.id, :html => 'child1 content')
58 58 get :move_block, :id => c1.id, :target => "end-of-box-#{@environment.boxes.last.id}"
59 59 assert_equal @environment.boxes.last, c1.reload.box
60 60 end
61 61  
62 62 should 'move block to inside of a container block' do
63   - c1 = RawHTMLBlock.create!(:box => @block.container_box, :html => 'child1 content')
64   - c2 = RawHTMLBlock.create!(:box => @environment.boxes.last, :html => 'child2 content')
  63 + c1 = RawHTMLBlock.create!(:box_id => @block.container_box.id, :html => 'child1 content')
  64 + c2 = RawHTMLBlock.create!(:box_id => @environment.boxes.last.id, :html => 'child2 content')
65 65 get :move_block, :id => c2.id, :target => "before-block-#{c1.id}"
66 66 assert_equal @block.container_box, c2.reload.box
67 67 end
68 68  
69 69 should 'move down a container block child' do
70   - c1 = RawHTMLBlock.create!(:box => @block.container_box, :html => 'child1 content')
71   - c2 = RawHTMLBlock.create!(:box => @block.container_box, :html => 'child2 content')
  70 + c1 = RawHTMLBlock.create!(:box_id => @block.container_box.id, :html => 'child1 content')
  71 + c2 = RawHTMLBlock.create!(:box_id => @block.container_box.id, :html => 'child2 content')
72 72 get :move_block_down, :id => c1.id
73 73 assert_equal [c2, c1], @block.blocks
74 74 end
75 75  
76 76 should 'move up a container block child' do
77   - c1 = RawHTMLBlock.create!(:box => @block.container_box, :html => 'child1 content')
78   - c2 = RawHTMLBlock.create!(:box => @block.container_box, :html => 'child2 content')
  77 + c1 = RawHTMLBlock.create!(:box_id => @block.container_box.id, :html => 'child1 content')
  78 + c2 = RawHTMLBlock.create!(:box_id => @block.container_box.id, :html => 'child2 content')
79 79 get :move_block_up, :id => c2.id
80 80 assert_equal [c2, c1], @block.blocks
81 81 end
... ...
plugins/container_block/test/functional/container_block_home_controller_test.rb
... ... @@ -20,8 +20,8 @@ class HomeControllerTest < ActionController::TestCase
20 20 @environment.add_admin(user.person)
21 21 login_as(user.login)
22 22  
23   - box = Box.create!(:owner => @environment)
24   - @block = ContainerBlockPlugin::ContainerBlock.create!(:box => box)
  23 + box = create(Box, :owner => @environment)
  24 + @block = create(ContainerBlockPlugin::ContainerBlock, :box => box)
25 25  
26 26 @environment.boxes = [box]
27 27 end
... ... @@ -32,15 +32,15 @@ class HomeControllerTest < ActionController::TestCase
32 32 end
33 33  
34 34 should 'display container children' do
35   - c1 = RawHTMLBlock.create!(:box => @block.container_box, :html => 'child1 content')
36   - c2 = RawHTMLBlock.create!(:box => @block.container_box, :html => 'child2 content')
  35 + c1 = RawHTMLBlock.create!(:box_id => @block.container_box.id, :html => 'child1 content')
  36 + c2 = RawHTMLBlock.create!(:box_id => @block.container_box.id, :html => 'child2 content')
37 37 get :index
38 38 assert_tag :div, :attributes => { :id => "block-#{c1.id}" }
39 39 assert_tag :div, :attributes => { :id => "block-#{c2.id}" }
40 40 end
41 41  
42 42 should 'display style tags for container children' do
43   - c1 = RawHTMLBlock.create!(:box => @block.container_box, :html => 'child1 content')
  43 + c1 = RawHTMLBlock.create!(:box_id => @block.container_box.id, :html => 'child1 content')
44 44 @block.children_settings = { c1.id => {:width => "123"} }
45 45 @block.save!
46 46 get :index
... ... @@ -48,7 +48,7 @@ class HomeControllerTest < ActionController::TestCase
48 48 end
49 49  
50 50 should 'do not display hidden children of container' do
51   - c1 = RawHTMLBlock.create!(:box => @block.container_box, :html => 'child1 content', :display => 'never')
  51 + c1 = RawHTMLBlock.create!(:box_id => @block.container_box.id, :html => 'child1 content', :display => 'never')
52 52 get :index
53 53 assert_no_tag :div, :attributes => { :id => "block-#{c1.id}" }
54 54 end
... ...
plugins/container_block/test/functional/container_block_plugin_admin_controller_test.rb
... ... @@ -12,9 +12,9 @@ class ContainerBlockPluginAdminControllerTest < ActionController::TestCase
12 12 @environment.add_admin(user.person)
13 13 login_as(user.login)
14 14  
15   - @block = ContainerBlockPlugin::ContainerBlock.create!(:box => @environment.boxes.first)
16   - @child1 = Block.create!(:box => @block.container_box)
17   - @child2 = Block.create!(:box => @block.container_box)
  15 + @block = ContainerBlockPlugin::ContainerBlock.create!(:box_id => @environment.boxes.first.id)
  16 + @child1 = Block.create!(:box_id => @block.container_box.id)
  17 + @child2 = Block.create!(:box_id => @block.container_box.id)
18 18 end
19 19  
20 20 should 'save widths of container block children' do
... ...
plugins/container_block/test/functional/container_block_plugin_controller_test.rb
... ... @@ -14,9 +14,9 @@ class ContainerBlockPluginControllerTest < ActionController::TestCase
14 14 @environment.add_admin(user.person)
15 15 login_as(user.login)
16 16  
17   - @block = ContainerBlockPlugin::ContainerBlock.create!(:box => @environment.boxes.first)
18   - @child1 = Block.create!(:box => @block.container_box)
19   - @child2 = Block.create!(:box => @block.container_box)
  17 + @block = ContainerBlockPlugin::ContainerBlock.create!(:box_id => @environment.boxes.first.id)
  18 + @child1 = Block.create!(:box_id => @block.container_box.id)
  19 + @child2 = Block.create!(:box_id => @block.container_box.id)
20 20 @environment = Environment.find(@environment.id)
21 21 stubs(:boxes_holder).returns(@environment)
22 22 @params = {}
... ...
plugins/container_block/test/functional/container_block_plugin_myprofile_controller_test.rb
... ... @@ -8,11 +8,11 @@ class ContainerBlockPluginMyprofileControllerTest < ActionController::TestCase
8 8  
9 9 @profile = fast_create(Community)
10 10 @profile.add_admin(user.person)
11   - @box = Box.new(:owner => @profile)
  11 + @box = Box.create!(:owner => @profile)
12 12  
13   - @block = ContainerBlockPlugin::ContainerBlock.create!(:box => @box)
14   - @child1 = Block.create!(:box => @block.container_box)
15   - @child2 = Block.create!(:box => @block.container_box)
  13 + @block = ContainerBlockPlugin::ContainerBlock.create!(:box_id => @box.id)
  14 + @child1 = Block.create!(:box_id => @block.container_box.id)
  15 + @child2 = Block.create!(:box_id => @block.container_box.id)
16 16 end
17 17  
18 18 should 'save widths of container block children' do
... ...
plugins/container_block/test/unit/block_test.rb
... ... @@ -5,26 +5,26 @@ class BlockTest < ActiveSupport::TestCase
5 5 def setup
6 6 @environment = fast_create(Environment)
7 7 @box = Box.create!(:owner => @environment)
8   - @container = ContainerBlockPlugin::ContainerBlock.create!(:box => @box)
  8 + @container = ContainerBlockPlugin::ContainerBlock.create!(:box_id => @box.id)
9 9 end
10 10  
11 11 should 'return environment box if block owner is not a ContainerBlock' do
12   - block = Block.create!(:box => @box)
  12 + block = Block.create!(:box_id => @box.id)
13 13 assert_equal @box, block.box
14 14 end
15 15  
16 16 should 'return container box if block owner is a ContainerBlock' do
17   - block = Block.create!(:box => @container.container_box)
  17 + block = Block.create!(:box_id => @container.container_box.id)
18 18 assert_equal @container.container_box, block.box
19 19 end
20 20  
21 21 should 'return block owner if block onwer is not a ContainerBlock' do
22   - block = Block.create!(:box => @box)
  22 + block = Block.create!(:box_id => @box.id)
23 23 assert_equal @environment, block.owner
24 24 end
25 25  
26 26 should 'return environment as owner if block onwer is a ContainerBlock' do
27   - block = Block.create!(:box => @container.container_box)
  27 + block = Block.create!(:box_id => @container.container_box.id)
28 28 assert_equal @environment, block.owner
29 29 end
30 30  
... ...
plugins/container_block/test/unit/container_block_plugin/container_block_test.rb
... ... @@ -27,7 +27,7 @@ class ContainerBlockPlugin::ContainerBlockTest < ActiveSupport::TestCase
27 27  
28 28 should 'create new blocks when receive block classes' do
29 29 @block.save!
30   - assert_difference Block, :count, 1 do
  30 + assert_difference 'Block.count', 1 do
31 31 @block.block_classes = ['Block']
32 32 end
33 33 assert_equal Block, Block.last.class
... ... @@ -35,14 +35,14 @@ class ContainerBlockPlugin::ContainerBlockTest < ActiveSupport::TestCase
35 35  
36 36 should 'do not create blocks when nothing is passed as block classes' do
37 37 @block.save!
38   - assert_no_difference Block, :count do
  38 + assert_no_difference 'Block.count' do
39 39 @block.block_classes = []
40 40 end
41 41 end
42 42  
43 43 should 'do not create blocks when nil is passed as block classes' do
44 44 @block.save!
45   - assert_no_difference Block, :count do
  45 + assert_no_difference 'Block.count' do
46 46 @block.block_classes = nil
47 47 end
48 48 end
... ... @@ -84,7 +84,7 @@ class ContainerBlockPlugin::ContainerBlockTest < ActiveSupport::TestCase
84 84  
85 85 should 'destroy box when container is removed' do
86 86 @block.save!
87   - assert_difference Box, :count, -1 do
  87 + assert_difference 'Box.count', -1 do
88 88 @block.destroy
89 89 end
90 90 end
... ...
plugins/container_block/test/unit/environment_test.rb
... ... @@ -5,10 +5,10 @@ class EnvironmentTest < ActiveSupport::TestCase
5 5 def setup
6 6 @environment = fast_create(Environment)
7 7  
8   - @box = Box.create!(:owner => @environment)
9   - @block = Block.create!(:box => @box)
  8 + @box = create(Box, :owner => @environment)
  9 + @block = create(Block, :box => @box)
10 10  
11   - @container = ContainerBlockPlugin::ContainerBlock.create!(:box => @box)
  11 + @container = create(ContainerBlockPlugin::ContainerBlock, :box => @box)
12 12 end
13 13  
14 14 should 'return blocks as usual' do
... ... @@ -16,7 +16,7 @@ class EnvironmentTest < ActiveSupport::TestCase
16 16 end
17 17  
18 18 should 'return blocks with container children' do
19   - child = Block.create!(:box => @container.container_box)
  19 + child = Block.create!(:box_id => @container.container_box.id)
20 20 assert_equal [@block, @container, child], @environment.blocks
21 21 end
22 22  
... ... @@ -25,7 +25,7 @@ class EnvironmentTest < ActiveSupport::TestCase
25 25 end
26 26  
27 27 should 'return child block with id at find method' do
28   - child = Block.create!(:box => @container.container_box)
  28 + child = Block.create!(:box_id => @container.container_box.id)
29 29 assert_equal child, @environment.blocks.find(child.id)
30 30 end
31 31  
... ...
plugins/container_block/test/unit/profile_test.rb
... ... @@ -5,10 +5,10 @@ class ProfileTest < ActiveSupport::TestCase
5 5 def setup
6 6 @profile = fast_create(Profile)
7 7  
8   - @box = Box.create!(:owner => @profile)
9   - @block = Block.create!(:box => @box)
  8 + @box = create(Box, :owner => @profile)
  9 + @block = create(Block, :box => @box)
10 10  
11   - @container = ContainerBlockPlugin::ContainerBlock.create!(:box => @box)
  11 + @container = create(ContainerBlockPlugin::ContainerBlock, :box => @box)
12 12 end
13 13  
14 14 should 'return blocks as usual' do
... ... @@ -16,7 +16,7 @@ class ProfileTest < ActiveSupport::TestCase
16 16 end
17 17  
18 18 should 'return blocks with container children' do
19   - child = Block.create!(:box => @container.container_box)
  19 + child = Block.create!(:box_id => @container.container_box.id)
20 20 assert_equal [@block, @container, child], @profile.blocks
21 21 end
22 22  
... ... @@ -25,7 +25,7 @@ class ProfileTest < ActiveSupport::TestCase
25 25 end
26 26  
27 27 should 'return child block with id at find method' do
28   - child = Block.create!(:box => @container.container_box)
  28 + child = Block.create!(:box_id => @container.container_box.id)
29 29 assert_equal child, @profile.blocks.find(child.id)
30 30 end
31 31  
... ...
plugins/container_block/views/blocks/container.html.erb 0 → 100644
... ... @@ -0,0 +1,60 @@
  1 +<% edit_mode = controller.send(:boxes_editor?) && controller.send(:uses_design_blocks?) %>
  2 +<% box_decorator = edit_mode ? self : BoxesHelper::DontMoveBlocks %>
  3 +
  4 +
  5 +<div class="box" id="box-<%= block.container_box.id %>">
  6 + <%= display_box_content(block.container_box, nil) %>
  7 + <div class="clear"></div>
  8 +</div>
  9 +<div class="clear"></div>
  10 +
  11 +<style>
  12 + <% box_decorator.select_blocks(block.blocks, { :article => @page, :request_path => request.path, :locale => locale }).each do |child| %>
  13 + #block-<%=block.id%> #block-<%=child.id%> { width: <%= block.child_width(child.id) %>px; }
  14 + <% end %>
  15 +</style>
  16 +
  17 +<% if edit_mode %>
  18 + <div class="button-bar">
  19 + <a href="#" onclick="toggleMoveContainerChildren(<%= block.id %>, <%= block.container_box.id %>); return false;" class="button icon-resize" title=<%= _('Resize blocks').to_json %>></a>
  20 + <%= link_to_remote '', :url => { :controller => controller.boxes_holder.kind_of?(Environment) ? 'container_block_plugin_admin' : 'container_block_plugin_myprofile', :action => 'saveWidths', :id => block.id },
  21 + :with => "containerChildrenWidth(#{block.id}, #{block.container_box.id})",
  22 + :html => {:class => "button icon-save container_block_save", :id => "container_block_save_#{block.id}", :title => _('Save') },
  23 + :loading => "open_loading(DEFAULT_LOADING_MESSAGE);",
  24 + :loaded => "close_loading();",
  25 + :complete => "display_notice(request.responseText);"%>
  26 + </div>
  27 +
  28 + <script>
  29 + function toggleMoveContainerChildren(container, box) {
  30 + var div = jQuery('#box-'+box+' > .block-outer > .block');
  31 + var targetDiv = jQuery('#box-'+box+' .block-outer .block-target');
  32 + if(div.is('.ui-resizable')) {
  33 + targetDiv.show();
  34 + div.find("a").die("click");
  35 + div.resizable('destroy');
  36 + } else {
  37 + targetDiv.hide();
  38 + div.find("a").live("click", function(e) {
  39 + e.preventDefault();
  40 + });
  41 + div.resizable({
  42 + handles: 'e, w',
  43 + containment: '#block-'+container+' .block-inner-2',
  44 + resize: function( event, ui ) {
  45 + ui.element.height('auto');
  46 + }
  47 + });
  48 + }
  49 + }
  50 +
  51 + function containerChildrenWidth(container, box) {
  52 + widths = "";
  53 + jQuery('#box-'+box+' > .block-outer > .block').each(function(i) {
  54 + childId = jQuery(this).attr('id').match(/block-(\d+)/)[1];
  55 + widths+=childId+","+jQuery(this).width()+"|";
  56 + });
  57 + return "widths="+widths;
  58 + }
  59 + </script>
  60 +<% end %>
... ...
plugins/container_block/views/blocks/container.rhtml
... ... @@ -1,60 +0,0 @@
1   -<% edit_mode = @controller.send(:boxes_editor?) && @controller.send(:uses_design_blocks?) %>
2   -<% box_decorator = edit_mode ? self : BoxesHelper::DontMoveBlocks %>
3   -
4   -
5   -<div class="box" id="box-<%= block.container_box.id %>">
6   - <%= display_box_content(block.container_box, nil) %>
7   - <div class="clear"></div>
8   -</div>
9   -<div class="clear"></div>
10   -
11   -<style>
12   - <% box_decorator.select_blocks(block.blocks, { :article => @page, :request_path => request.path, :locale => locale }).each do |child| %>
13   - #block-<%=block.id%> #block-<%=child.id%> { width: <%= block.child_width(child.id) %>px; }
14   - <% end %>
15   -</style>
16   -
17   -<% if edit_mode %>
18   - <div class="button-bar">
19   - <a href="#" onclick="toggleMoveContainerChildren(<%= block.id %>, <%= block.container_box.id %>); return false;" class="button icon-resize" title=<%= _('Resize blocks').to_json %>></a>
20   - <%= link_to_remote '', :url => { :controller => @controller.boxes_holder.kind_of?(Environment) ? 'container_block_plugin_admin' : 'container_block_plugin_myprofile', :action => 'saveWidths', :id => block.id },
21   - :with => "containerChildrenWidth(#{block.id}, #{block.container_box.id})",
22   - :html => {:class => "button icon-save container_block_save", :id => "container_block_save_#{block.id}", :title => _('Save') },
23   - :loading => "open_loading(DEFAULT_LOADING_MESSAGE);",
24   - :loaded => "close_loading();",
25   - :complete => "display_notice(request.responseText);"%>
26   - </div>
27   -
28   - <script>
29   - function toggleMoveContainerChildren(container, box) {
30   - var div = jQuery('#box-'+box+' > .block-outer > .block');
31   - var targetDiv = jQuery('#box-'+box+' .block-outer .block-target');
32   - if(div.is('.ui-resizable')) {
33   - targetDiv.show();
34   - div.find("a").die("click");
35   - div.resizable('destroy');
36   - } else {
37   - targetDiv.hide();
38   - div.find("a").live("click", function(e) {
39   - e.preventDefault();
40   - });
41   - div.resizable({
42   - handles: 'e, w',
43   - containment: '#block-'+container+' .block-inner-2',
44   - resize: function( event, ui ) {
45   - ui.element.height('auto');
46   - }
47   - });
48   - }
49   - }
50   -
51   - function containerChildrenWidth(container, box) {
52   - widths = "";
53   - jQuery('#box-'+box+' > .block-outer > .block').each(function(i) {
54   - childId = jQuery(this).attr('id').match(/block-(\d+)/)[1];
55   - widths+=childId+","+jQuery(this).width()+"|";
56   - });
57   - return "widths="+widths;
58   - }
59   - </script>
60   -<% end %>