diff --git a/app/controllers/box_organizer_controller.rb b/app/controllers/box_organizer_controller.rb index ab17ee8..fb37c42 100644 --- a/app/controllers/box_organizer_controller.rb +++ b/app/controllers/box_organizer_controller.rb @@ -70,7 +70,7 @@ class BoxOrganizerController < ApplicationController else @center_block_types = (Box.acceptable_center_blocks & available_blocks) + plugins.dispatch(:extra_blocks, :type => boxes_holder.class, :position => 1) @side_block_types = (Box.acceptable_side_blocks & available_blocks) + plugins.dispatch(:extra_blocks, :type => boxes_holder.class, :position => [2,3]) - @boxes = boxes_holder.boxes + @boxes = boxes_holder.boxes.with_position render :action => 'add_block', :layout => false end end diff --git a/app/helpers/boxes_helper.rb b/app/helpers/boxes_helper.rb index cf069d9..78a57c7 100644 --- a/app/helpers/boxes_helper.rb +++ b/app/helpers/boxes_helper.rb @@ -39,7 +39,7 @@ module BoxesHelper end def display_boxes(holder, main_content) - boxes = holder.boxes.first(holder.boxes_limit) + boxes = holder.boxes.with_position.first(holder.boxes_limit) content = boxes.reverse.map { |item| display_box(item, main_content) }.join("\n") content = main_content if (content.blank?) diff --git a/app/models/box.rb b/app/models/box.rb index 36b31d1..05226f7 100644 --- a/app/models/box.rb +++ b/app/models/box.rb @@ -5,6 +5,8 @@ class Box < ActiveRecord::Base include Noosfero::Plugin::HotSpot + named_scope :with_position, :conditions => ['boxes.position > 0'] + def environment owner ? (owner.kind_of?(Environment) ? owner : owner.environment) : nil end diff --git a/plugins/container_block/lib/container_block_plugin/container_block.rb b/plugins/container_block/lib/container_block_plugin/container_block.rb index 87b4007..0d74762 100644 --- a/plugins/container_block/lib/container_block_plugin/container_block.rb +++ b/plugins/container_block/lib/container_block_plugin/container_block.rb @@ -24,8 +24,9 @@ class ContainerBlockPlugin::ContainerBlock < Block end def create_box - box = Box.create!(:owner => owner) - settings[:container_box_id] = box.id + container_box = Box.create!(:owner => owner) + container_box.update_attribute(:position, nil) + settings[:container_box_id] = container_box.id save! end diff --git a/plugins/container_block/test/unit/container_block_plugin/container_block_test.rb b/plugins/container_block/test/unit/container_block_plugin/container_block_test.rb index 62e1fa3..c364f9b 100644 --- a/plugins/container_block/test/unit/container_block_plugin/container_block_test.rb +++ b/plugins/container_block/test/unit/container_block_plugin/container_block_test.rb @@ -20,6 +20,11 @@ class ContainerBlockPlugin::ContainerBlockTest < ActiveSupport::TestCase assert @block.container_box_id end + should 'created box should have nil as position' do + @block.save! + assert_equal nil, @block.container_box.position + end + should 'return created box' do @block.save! assert @block.container_box @@ -89,4 +94,14 @@ class ContainerBlockPlugin::ContainerBlockTest < ActiveSupport::TestCase end end + should 'not mess up with boxes positions when destroyed' do + env = fast_create(Environment) + box1 = fast_create(Box, :owner_id => env.id, :owner_type => 'Environment', :position => 1) + box2 = fast_create(Box, :owner_id => env.id, :owner_type => 'Environment', :position => 2) + box3 = fast_create(Box, :owner_id => env.id, :owner_type => 'Environment', :position => 3) + block = ContainerBlockPlugin::ContainerBlock.create!(:box => box1) + block.destroy + assert_equal [1, 2, 3], [box1.reload.position, box2.reload.position, box3.reload.position] + end + end diff --git a/test/unit/box_test.rb b/test/unit/box_test.rb index 7a1d7d8..408b6bc 100644 --- a/test/unit/box_test.rb +++ b/test/unit/box_test.rb @@ -119,4 +119,11 @@ class BoxTest < ActiveSupport::TestCase assert blocks.include?('plugin-block') end + should 'list only boxes with a postion greater than zero' do + profile = fast_create(Profile) + box = fast_create(Box, :owner_id => profile.id, :owner_type => 'Profile', :position => 0) + box2 = fast_create(Box, :owner_id => profile.id, :owner_type => 'Profile', :position => 1) + assert_equal [box2], profile.boxes.with_position + end + end diff --git a/test/unit/boxes_helper_test.rb b/test/unit/boxes_helper_test.rb index 9998e1e..624facf 100644 --- a/test/unit/boxes_helper_test.rb +++ b/test/unit/boxes_helper_test.rb @@ -13,7 +13,8 @@ class BoxesHelperTest < ActiveSupport::TestCase should 'include profile-specific header' do holder = mock - holder.stubs(:boxes).returns([]) + holder.stubs(:boxes).returns(boxes = []) + boxes.stubs(:with_position).returns([]) holder.stubs(:boxes_limit).returns(0) holder.stubs(:custom_header_expanded).returns('my custom header') @controller.stubs(:boxes_holder).returns(holder) @@ -23,7 +24,8 @@ class BoxesHelperTest < ActiveSupport::TestCase should 'include profile-specific footer' do holder = mock - holder.stubs(:boxes).returns([]) + holder.stubs(:boxes).returns(boxes = []) + boxes.stubs(:with_position).returns([]) holder.stubs(:boxes_limit).returns(0) holder.stubs(:custom_footer_expanded).returns('my custom footer') @controller.stubs(:boxes_holder).returns(holder) -- libgit2 0.21.2