Commit c0e020285f7d9e547feb1e4876acfa977ac1a5fb

Authored by Victor Costa
1 parent 7ab8c9ac

container_block: avoid cyclical reference

(ActionItem2943)
plugins/container_block/lib/container_block_plugin/container_block.rb
... ... @@ -7,6 +7,16 @@ class ContainerBlockPlugin::ContainerBlock < Block
7 7 settings_items :container_box_id, :type => Integer, :default => nil
8 8 settings_items :children_settings, :type => Hash, :default => {}
9 9  
  10 + validate :no_cyclical_reference, :if => 'container_box_id.present?'
  11 +
  12 + def no_cyclical_reference
  13 + errors.add(:box_id, _('cyclical reference is not allowed.')) if box_id == container_box_id
  14 + end
  15 +
  16 + before_save do |b|
  17 + raise "cyclical reference is not allowed" if b.box_id == b.container_box_id && !b.container_box_id.blank?
  18 + end
  19 +
10 20 def self.description
11 21 _('Container')
12 22 end
... ...
plugins/container_block/test/unit/container_block_plugin/container_block_test.rb
... ... @@ -104,4 +104,17 @@ class ContainerBlockPlugin::ContainerBlockTest < ActiveSupport::TestCase
104 104 assert_equal [1, 2, 3], [box1.reload.position, box2.reload.position, box3.reload.position]
105 105 end
106 106  
  107 + should 'be able to change box' do
  108 + @block.save!
  109 + @block.box = Box.new(:owner => Environment.default)
  110 + @block.save!
  111 + end
  112 +
  113 + should 'not able to change box to be the same as container_box' do
  114 + @block.save!
  115 + @block.box = @block.container_box
  116 + @block.save
  117 + assert @block.errors.invalid?(:box_id)
  118 + end
  119 +
107 120 end
... ...