Commit cf94bc1e5bd51d0e6f978d96a6e9d1c5005fc8e9

Authored by Victor Costa
1 parent ca1dc3cd

container_block: do not show its boxes at add_block view

app/controllers/box_organizer_controller.rb
... ... @@ -70,7 +70,7 @@ class BoxOrganizerController < ApplicationController
70 70 else
71 71 @center_block_types = (Box.acceptable_center_blocks & available_blocks) + plugins.dispatch(:extra_blocks, :type => boxes_holder.class, :position => 1)
72 72 @side_block_types = (Box.acceptable_side_blocks & available_blocks) + plugins.dispatch(:extra_blocks, :type => boxes_holder.class, :position => [2,3])
73   - @boxes = boxes_holder.boxes
  73 + @boxes = boxes_holder.boxes.with_position
74 74 render :action => 'add_block', :layout => false
75 75 end
76 76 end
... ...
app/helpers/boxes_helper.rb
... ... @@ -39,7 +39,7 @@ module BoxesHelper
39 39 end
40 40  
41 41 def display_boxes(holder, main_content)
42   - boxes = holder.boxes.first(holder.boxes_limit)
  42 + boxes = holder.boxes.with_position.first(holder.boxes_limit)
43 43 content = boxes.reverse.map { |item| display_box(item, main_content) }.join("\n")
44 44 content = main_content if (content.blank?)
45 45  
... ...
app/models/box.rb
... ... @@ -5,6 +5,8 @@ class Box < ActiveRecord::Base
5 5  
6 6 include Noosfero::Plugin::HotSpot
7 7  
  8 + named_scope :with_position, :conditions => ['boxes.position > 0']
  9 +
8 10 def environment
9 11 owner ? (owner.kind_of?(Environment) ? owner : owner.environment) : nil
10 12 end
... ...
plugins/container_block/lib/container_block_plugin/container_block.rb
... ... @@ -24,8 +24,9 @@ class ContainerBlockPlugin::ContainerBlock < Block
24 24 end
25 25  
26 26 def create_box
27   - box = Box.create!(:owner => owner)
28   - settings[:container_box_id] = box.id
  27 + container_box = Box.create!(:owner => owner)
  28 + container_box.update_attribute(:position, nil)
  29 + settings[:container_box_id] = container_box.id
29 30 save!
30 31 end
31 32  
... ...
plugins/container_block/test/unit/container_block_plugin/container_block_test.rb
... ... @@ -20,6 +20,11 @@ class ContainerBlockPlugin::ContainerBlockTest < ActiveSupport::TestCase
20 20 assert @block.container_box_id
21 21 end
22 22  
  23 + should 'created box should have nil as position' do
  24 + @block.save!
  25 + assert_equal nil, @block.container_box.position
  26 + end
  27 +
23 28 should 'return created box' do
24 29 @block.save!
25 30 assert @block.container_box
... ... @@ -89,4 +94,14 @@ class ContainerBlockPlugin::ContainerBlockTest < ActiveSupport::TestCase
89 94 end
90 95 end
91 96  
  97 + should 'not mess up with boxes positions when destroyed' do
  98 + env = fast_create(Environment)
  99 + box1 = fast_create(Box, :owner_id => env.id, :owner_type => 'Environment', :position => 1)
  100 + box2 = fast_create(Box, :owner_id => env.id, :owner_type => 'Environment', :position => 2)
  101 + box3 = fast_create(Box, :owner_id => env.id, :owner_type => 'Environment', :position => 3)
  102 + block = ContainerBlockPlugin::ContainerBlock.create!(:box => box1)
  103 + block.destroy
  104 + assert_equal [1, 2, 3], [box1.reload.position, box2.reload.position, box3.reload.position]
  105 + end
  106 +
92 107 end
... ...
test/unit/box_test.rb
... ... @@ -119,4 +119,11 @@ class BoxTest < ActiveSupport::TestCase
119 119 assert blocks.include?('plugin-block')
120 120 end
121 121  
  122 + should 'list only boxes with a postion greater than zero' do
  123 + profile = fast_create(Profile)
  124 + box = fast_create(Box, :owner_id => profile.id, :owner_type => 'Profile', :position => 0)
  125 + box2 = fast_create(Box, :owner_id => profile.id, :owner_type => 'Profile', :position => 1)
  126 + assert_equal [box2], profile.boxes.with_position
  127 + end
  128 +
122 129 end
... ...
test/unit/boxes_helper_test.rb
... ... @@ -13,7 +13,8 @@ class BoxesHelperTest < ActiveSupport::TestCase
13 13  
14 14 should 'include profile-specific header' do
15 15 holder = mock
16   - holder.stubs(:boxes).returns([])
  16 + holder.stubs(:boxes).returns(boxes = [])
  17 + boxes.stubs(:with_position).returns([])
17 18 holder.stubs(:boxes_limit).returns(0)
18 19 holder.stubs(:custom_header_expanded).returns('my custom header')
19 20 @controller.stubs(:boxes_holder).returns(holder)
... ... @@ -23,7 +24,8 @@ class BoxesHelperTest < ActiveSupport::TestCase
23 24  
24 25 should 'include profile-specific footer' do
25 26 holder = mock
26   - holder.stubs(:boxes).returns([])
  27 + holder.stubs(:boxes).returns(boxes = [])
  28 + boxes.stubs(:with_position).returns([])
27 29 holder.stubs(:boxes_limit).returns(0)
28 30 holder.stubs(:custom_footer_expanded).returns('my custom footer')
29 31 @controller.stubs(:boxes_holder).returns(holder)
... ...