require 'spec_helper' describe BrickLayer::Route do before(:each) do BrickLayer::Route.destroy_all @root = BrickLayer::Route.create(:slug => "") end it "should allow the creation of a route" do route = @root.children.create(:slug => "test1") route.should be_valid end it "should not allow a route with the same slug to be entered on the same parent" do route1 = @root.children.create(:slug => "route1") route2 = route1.children.create(:slug => "route2") route2_again = route1.children.create(:slug => "route2") route2_again.should_not be_valid route3 = route2.children.create(:slug => "route3") route2_again_valid = route3.children.create(:slug => "route2") route2_again_valid.should be_valid end it "it should give it's full path in this/path/format" do route1 = @root.children.create(:slug => "route1") route2 = route1.children.create(:slug => "route2") route3 = route2.children.create(:slug => "route3") route3.full_path.should == "/route1/route2/route3" end it "should allow you to change it's parent" do route1 = @root.children.create(:slug => "route1") route2 = route1.children.create(:slug => "route2") route3 = route2.children.create(:slug => "route3") route3.full_path.should == "/route1/route2/route3" route3.change_parent_to(route1) route3.save route3.full_path.should == "/route1/route3" route2.full_path.should == "/route1/route2" route2.change_parent_to(route3) route2.save route2.full_path.should == "/route1/route3/route2" end it "should remove children routes if they exist" do route1 = @root.children.create(:slug => "route1") route2 = route1.children.create(:slug => "route2") route3 = route2.children.create(:slug => "route3") lambda { BrickLayer::Route.find(route3.id) }.should_not raise_error route2.destroy lambda { BrickLayer::Route.find(route3.id) }.should raise_error(Mongoid::Errors::DocumentNotFound) end it "should properly display cached path" do route1 = @root.children.create(:slug => "route1") route2 = route1.children.create(:slug => "route2") route3 = route2.children.create(:slug => "route3") route3.set_full_path_cache.should == "/route1/route2/route3" route3.change_parent_to(route1) route3.save route3.set_full_path_cache.should == "/route1/route3" route2.set_full_path_cache.should == "/route1/route2" route2.change_parent_to(route3) route2.save route2.set_full_path_cache.should == "/route1/route3/route2" end end