Commit cf94bc1e5bd51d0e6f978d96a6e9d1c5005fc8e9
1 parent
ca1dc3cd
Exists in
master
and in
29 other branches
container_block: do not show its boxes at add_block view
Showing
7 changed files
with
33 additions
and
6 deletions
Show diff stats
app/controllers/box_organizer_controller.rb
| @@ -70,7 +70,7 @@ class BoxOrganizerController < ApplicationController | @@ -70,7 +70,7 @@ class BoxOrganizerController < ApplicationController | ||
| 70 | else | 70 | else |
| 71 | @center_block_types = (Box.acceptable_center_blocks & available_blocks) + plugins.dispatch(:extra_blocks, :type => boxes_holder.class, :position => 1) | 71 | @center_block_types = (Box.acceptable_center_blocks & available_blocks) + plugins.dispatch(:extra_blocks, :type => boxes_holder.class, :position => 1) |
| 72 | @side_block_types = (Box.acceptable_side_blocks & available_blocks) + plugins.dispatch(:extra_blocks, :type => boxes_holder.class, :position => [2,3]) | 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 | render :action => 'add_block', :layout => false | 74 | render :action => 'add_block', :layout => false |
| 75 | end | 75 | end |
| 76 | end | 76 | end |
app/helpers/boxes_helper.rb
| @@ -39,7 +39,7 @@ module BoxesHelper | @@ -39,7 +39,7 @@ module BoxesHelper | ||
| 39 | end | 39 | end |
| 40 | 40 | ||
| 41 | def display_boxes(holder, main_content) | 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 | content = boxes.reverse.map { |item| display_box(item, main_content) }.join("\n") | 43 | content = boxes.reverse.map { |item| display_box(item, main_content) }.join("\n") |
| 44 | content = main_content if (content.blank?) | 44 | content = main_content if (content.blank?) |
| 45 | 45 |
app/models/box.rb
| @@ -5,6 +5,8 @@ class Box < ActiveRecord::Base | @@ -5,6 +5,8 @@ class Box < ActiveRecord::Base | ||
| 5 | 5 | ||
| 6 | include Noosfero::Plugin::HotSpot | 6 | include Noosfero::Plugin::HotSpot |
| 7 | 7 | ||
| 8 | + named_scope :with_position, :conditions => ['boxes.position > 0'] | ||
| 9 | + | ||
| 8 | def environment | 10 | def environment |
| 9 | owner ? (owner.kind_of?(Environment) ? owner : owner.environment) : nil | 11 | owner ? (owner.kind_of?(Environment) ? owner : owner.environment) : nil |
| 10 | end | 12 | end |
plugins/container_block/lib/container_block_plugin/container_block.rb
| @@ -24,8 +24,9 @@ class ContainerBlockPlugin::ContainerBlock < Block | @@ -24,8 +24,9 @@ class ContainerBlockPlugin::ContainerBlock < Block | ||
| 24 | end | 24 | end |
| 25 | 25 | ||
| 26 | def create_box | 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 | save! | 30 | save! |
| 30 | end | 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,6 +20,11 @@ class ContainerBlockPlugin::ContainerBlockTest < ActiveSupport::TestCase | ||
| 20 | assert @block.container_box_id | 20 | assert @block.container_box_id |
| 21 | end | 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 | should 'return created box' do | 28 | should 'return created box' do |
| 24 | @block.save! | 29 | @block.save! |
| 25 | assert @block.container_box | 30 | assert @block.container_box |
| @@ -89,4 +94,14 @@ class ContainerBlockPlugin::ContainerBlockTest < ActiveSupport::TestCase | @@ -89,4 +94,14 @@ class ContainerBlockPlugin::ContainerBlockTest < ActiveSupport::TestCase | ||
| 89 | end | 94 | end |
| 90 | end | 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 | end | 107 | end |
test/unit/box_test.rb
| @@ -119,4 +119,11 @@ class BoxTest < ActiveSupport::TestCase | @@ -119,4 +119,11 @@ class BoxTest < ActiveSupport::TestCase | ||
| 119 | assert blocks.include?('plugin-block') | 119 | assert blocks.include?('plugin-block') |
| 120 | end | 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 | end | 129 | end |
test/unit/boxes_helper_test.rb
| @@ -13,7 +13,8 @@ class BoxesHelperTest < ActiveSupport::TestCase | @@ -13,7 +13,8 @@ class BoxesHelperTest < ActiveSupport::TestCase | ||
| 13 | 13 | ||
| 14 | should 'include profile-specific header' do | 14 | should 'include profile-specific header' do |
| 15 | holder = mock | 15 | holder = mock |
| 16 | - holder.stubs(:boxes).returns([]) | 16 | + holder.stubs(:boxes).returns(boxes = []) |
| 17 | + boxes.stubs(:with_position).returns([]) | ||
| 17 | holder.stubs(:boxes_limit).returns(0) | 18 | holder.stubs(:boxes_limit).returns(0) |
| 18 | holder.stubs(:custom_header_expanded).returns('my custom header') | 19 | holder.stubs(:custom_header_expanded).returns('my custom header') |
| 19 | @controller.stubs(:boxes_holder).returns(holder) | 20 | @controller.stubs(:boxes_holder).returns(holder) |
| @@ -23,7 +24,8 @@ class BoxesHelperTest < ActiveSupport::TestCase | @@ -23,7 +24,8 @@ class BoxesHelperTest < ActiveSupport::TestCase | ||
| 23 | 24 | ||
| 24 | should 'include profile-specific footer' do | 25 | should 'include profile-specific footer' do |
| 25 | holder = mock | 26 | holder = mock |
| 26 | - holder.stubs(:boxes).returns([]) | 27 | + holder.stubs(:boxes).returns(boxes = []) |
| 28 | + boxes.stubs(:with_position).returns([]) | ||
| 27 | holder.stubs(:boxes_limit).returns(0) | 29 | holder.stubs(:boxes_limit).returns(0) |
| 28 | holder.stubs(:custom_footer_expanded).returns('my custom footer') | 30 | holder.stubs(:custom_footer_expanded).returns('my custom footer') |
| 29 | @controller.stubs(:boxes_holder).returns(holder) | 31 | @controller.stubs(:boxes_holder).returns(holder) |