require 'spec_helper' describe Customize::Inherited do with_model :product do table do |t| t.string :name end model do include Customize::Inherited has_many :product_objects end end with_model :product_object do table do |t| t.references :product end model do belongs_to :product end end before :each do @parent = Product.create! @child = Product.create! @child.inherit @parent @parent.reload end it "cannot inherit different class" do expect { @child.inherit Object.new }.to raise_error end it "have method ascent_ids" do @child.ascent_ids.should_not be_nil end it "child should have 1 ascent" do @child.ascent_ids.should have(1).items end it "parent should have 1 descent" do @parent.descent_ids.should have(1).items end it "should support 2 levels inherit" do c2 = Product.create! c2.inherit @child c2.ascent_ids.should have(2).items @child.ascent_ids.should have(1).items end it "root has 1 item" do Product.root.should have(1).items end it "child should have ascents" do @child.ascents.should have(1).items end it "child is leaf" do @child.leaf?.should be_true end it "parent is not leaf" do @parent.leaf?.should_not be_true end it "convert type_tree" do tree = Product.type_tree tree.first[:id].should == @parent.id tree.first[:children].should have(1).items end it "should not inherit self" do expect { @parent.inherit @parent }.to raise_error end context "new node" do subject { Product.new } it "should have [] ascent_ids" do subject.ascent_ids.should == [] end it "should have [] descent_ids" do subject.descent_ids.should == [] end end context "destroy node" do before :each do Product.create.inherit @parent @child.destroy end it "parent (right - left) should eq all descent size * 2 + 1" do @parent.reload node = @parent.inherit_node (node.right - node.left).should == @parent.descent_ids.size * 2 + 1 end it "destroy a node with chilldren is not allowed" do c1 = Product.create c2 = Product.create c1.inherit @parent c2.inherit c1 expect { c1.destroy }.to raise_error end end context "associations" do subject { p1 = Product.create p2 = Product.create p2.inherit p1 p1.product_objects.create p2.product_objects.create p2 } it "should have 2 inherit_association :product_objects" do pending "cannot pass with with_model" subject.inherit_association(:product_objects,:include=>true).should have(2).times end end end