Chris@1115: require 'spec_helper' Chris@1115: Chris@1115: describe "AwesomeNestedSet" do Chris@1115: before(:all) do Chris@1115: self.class.fixtures :categories, :departments, :notes, :things, :brokens Chris@1115: end Chris@1115: Chris@1115: describe "defaults" do Chris@1115: it "should have left_column_default" do Chris@1115: Default.acts_as_nested_set_options[:left_column].should == 'lft' Chris@1115: end Chris@1115: Chris@1115: it "should have right_column_default" do Chris@1115: Default.acts_as_nested_set_options[:right_column].should == 'rgt' Chris@1115: end Chris@1115: Chris@1115: it "should have parent_column_default" do Chris@1115: Default.acts_as_nested_set_options[:parent_column].should == 'parent_id' Chris@1115: end Chris@1115: Chris@1115: it "should have scope_default" do Chris@1115: Default.acts_as_nested_set_options[:scope].should be_nil Chris@1115: end Chris@1115: Chris@1115: it "should have left_column_name" do Chris@1115: Default.left_column_name.should == 'lft' Chris@1115: Default.new.left_column_name.should == 'lft' Chris@1115: RenamedColumns.left_column_name.should == 'red' Chris@1115: RenamedColumns.new.left_column_name.should == 'red' Chris@1115: end Chris@1115: Chris@1115: it "should have right_column_name" do Chris@1115: Default.right_column_name.should == 'rgt' Chris@1115: Default.new.right_column_name.should == 'rgt' Chris@1115: RenamedColumns.right_column_name.should == 'black' Chris@1115: RenamedColumns.new.right_column_name.should == 'black' Chris@1115: end Chris@1115: Chris@1115: it "should have parent_column_name" do Chris@1115: Default.parent_column_name.should == 'parent_id' Chris@1115: Default.new.parent_column_name.should == 'parent_id' Chris@1115: RenamedColumns.parent_column_name.should == 'mother_id' Chris@1115: RenamedColumns.new.parent_column_name.should == 'mother_id' Chris@1115: end Chris@1115: end Chris@1115: Chris@1115: it "creation_with_altered_column_names" do Chris@1115: lambda { Chris@1115: RenamedColumns.create!() Chris@1115: }.should_not raise_exception Chris@1115: end Chris@1115: Chris@1115: it "creation when existing record has nil left column" do Chris@1115: assert_nothing_raised do Chris@1115: Broken.create! Chris@1115: end Chris@1115: end Chris@1115: Chris@1115: it "quoted_left_column_name" do Chris@1115: quoted = Default.connection.quote_column_name('lft') Chris@1115: Default.quoted_left_column_name.should == quoted Chris@1115: Default.new.quoted_left_column_name.should == quoted Chris@1115: end Chris@1115: Chris@1115: it "quoted_right_column_name" do Chris@1115: quoted = Default.connection.quote_column_name('rgt') Chris@1115: Default.quoted_right_column_name.should == quoted Chris@1115: Default.new.quoted_right_column_name.should == quoted Chris@1115: end Chris@1115: Chris@1115: it "left_column_protected_from_assignment" do Chris@1115: lambda { Chris@1115: Category.new.lft = 1 Chris@1115: }.should raise_exception(ActiveRecord::ActiveRecordError) Chris@1115: end Chris@1115: Chris@1115: it "right_column_protected_from_assignment" do Chris@1115: lambda { Chris@1115: Category.new.rgt = 1 Chris@1115: }.should raise_exception(ActiveRecord::ActiveRecordError) Chris@1115: end Chris@1115: Chris@1115: it "scoped_appends_id" do Chris@1115: ScopedCategory.acts_as_nested_set_options[:scope].should == :organization_id Chris@1115: end Chris@1115: Chris@1115: it "roots_class_method" do Chris@1115: Category.roots.should == Category.find_all_by_parent_id(nil) Chris@1115: end Chris@1115: Chris@1115: it "root_class_method" do Chris@1115: Category.root.should == categories(:top_level) Chris@1115: end Chris@1115: Chris@1115: it "root" do Chris@1115: categories(:child_3).root.should == categories(:top_level) Chris@1115: end Chris@1115: Chris@1115: it "root?" do Chris@1115: categories(:top_level).root?.should be_true Chris@1115: categories(:top_level_2).root?.should be_true Chris@1115: end Chris@1115: Chris@1115: it "leaves_class_method" do Chris@1115: Category.find(:all, :conditions => "#{Category.right_column_name} - #{Category.left_column_name} = 1").should == Category.leaves Chris@1115: Category.leaves.count.should == 4 Chris@1115: Category.leaves.should include(categories(:child_1)) Chris@1115: Category.leaves.should include(categories(:child_2_1)) Chris@1115: Category.leaves.should include(categories(:child_3)) Chris@1115: Category.leaves.should include(categories(:top_level_2)) Chris@1115: end Chris@1115: Chris@1115: it "leaf" do Chris@1115: categories(:child_1).leaf?.should be_true Chris@1115: categories(:child_2_1).leaf?.should be_true Chris@1115: categories(:child_3).leaf?.should be_true Chris@1115: categories(:top_level_2).leaf?.should be_true Chris@1115: Chris@1115: categories(:top_level).leaf?.should be_false Chris@1115: categories(:child_2).leaf?.should be_false Chris@1115: Category.new.leaf?.should be_false Chris@1115: end Chris@1115: Chris@1115: Chris@1115: it "parent" do Chris@1115: categories(:child_2_1).parent.should == categories(:child_2) Chris@1115: end Chris@1115: Chris@1115: it "self_and_ancestors" do Chris@1115: child = categories(:child_2_1) Chris@1115: self_and_ancestors = [categories(:top_level), categories(:child_2), child] Chris@1115: self_and_ancestors.should == child.self_and_ancestors Chris@1115: end Chris@1115: Chris@1115: it "ancestors" do Chris@1115: child = categories(:child_2_1) Chris@1115: ancestors = [categories(:top_level), categories(:child_2)] Chris@1115: ancestors.should == child.ancestors Chris@1115: end Chris@1115: Chris@1115: it "self_and_siblings" do Chris@1115: child = categories(:child_2) Chris@1115: self_and_siblings = [categories(:child_1), child, categories(:child_3)] Chris@1115: self_and_siblings.should == child.self_and_siblings Chris@1115: lambda do Chris@1115: tops = [categories(:top_level), categories(:top_level_2)] Chris@1115: assert_equal tops, categories(:top_level).self_and_siblings Chris@1115: end.should_not raise_exception Chris@1115: end Chris@1115: Chris@1115: it "siblings" do Chris@1115: child = categories(:child_2) Chris@1115: siblings = [categories(:child_1), categories(:child_3)] Chris@1115: siblings.should == child.siblings Chris@1115: end Chris@1115: Chris@1115: it "leaves" do Chris@1115: leaves = [categories(:child_1), categories(:child_2_1), categories(:child_3)] Chris@1115: categories(:top_level).leaves.should == leaves Chris@1115: end Chris@1115: Chris@1115: it "level" do Chris@1115: categories(:top_level).level.should == 0 Chris@1115: categories(:child_1).level.should == 1 Chris@1115: categories(:child_2_1).level.should == 2 Chris@1115: end Chris@1115: Chris@1115: it "has_children?" do Chris@1115: categories(:child_2_1).children.empty?.should be_true Chris@1115: categories(:child_2).children.empty?.should be_false Chris@1115: categories(:top_level).children.empty?.should be_false Chris@1115: end Chris@1115: Chris@1115: it "self_and_descendents" do Chris@1115: parent = categories(:top_level) Chris@1115: self_and_descendants = [parent, categories(:child_1), categories(:child_2), Chris@1115: categories(:child_2_1), categories(:child_3)] Chris@1115: self_and_descendants.should == parent.self_and_descendants Chris@1115: self_and_descendants.count.should == parent.self_and_descendants.count Chris@1115: end Chris@1115: Chris@1115: it "descendents" do Chris@1115: lawyers = Category.create!(:name => "lawyers") Chris@1115: us = Category.create!(:name => "United States") Chris@1115: us.move_to_child_of(lawyers) Chris@1115: patent = Category.create!(:name => "Patent Law") Chris@1115: patent.move_to_child_of(us) Chris@1115: lawyers.reload Chris@1115: Chris@1115: lawyers.children.size.should == 1 Chris@1115: us.children.size.should == 1 Chris@1115: lawyers.descendants.size.should == 2 Chris@1115: end Chris@1115: Chris@1115: it "self_and_descendents" do Chris@1115: parent = categories(:top_level) Chris@1115: descendants = [categories(:child_1), categories(:child_2), Chris@1115: categories(:child_2_1), categories(:child_3)] Chris@1115: descendants.should == parent.descendants Chris@1115: end Chris@1115: Chris@1115: it "children" do Chris@1115: category = categories(:top_level) Chris@1115: category.children.each {|c| category.id.should == c.parent_id } Chris@1115: end Chris@1115: Chris@1115: it "order_of_children" do Chris@1115: categories(:child_2).move_left Chris@1115: categories(:child_2).should == categories(:top_level).children[0] Chris@1115: categories(:child_1).should == categories(:top_level).children[1] Chris@1115: categories(:child_3).should == categories(:top_level).children[2] Chris@1115: end Chris@1115: Chris@1115: it "is_or_is_ancestor_of?" do Chris@1115: categories(:top_level).is_or_is_ancestor_of?(categories(:child_1)).should be_true Chris@1115: categories(:top_level).is_or_is_ancestor_of?(categories(:child_2_1)).should be_true Chris@1115: categories(:child_2).is_or_is_ancestor_of?(categories(:child_2_1)).should be_true Chris@1115: categories(:child_2_1).is_or_is_ancestor_of?(categories(:child_2)).should be_false Chris@1115: categories(:child_1).is_or_is_ancestor_of?(categories(:child_2)).should be_false Chris@1115: categories(:child_1).is_or_is_ancestor_of?(categories(:child_1)).should be_true Chris@1115: end Chris@1115: Chris@1115: it "is_ancestor_of?" do Chris@1115: categories(:top_level).is_ancestor_of?(categories(:child_1)).should be_true Chris@1115: categories(:top_level).is_ancestor_of?(categories(:child_2_1)).should be_true Chris@1115: categories(:child_2).is_ancestor_of?(categories(:child_2_1)).should be_true Chris@1115: categories(:child_2_1).is_ancestor_of?(categories(:child_2)).should be_false Chris@1115: categories(:child_1).is_ancestor_of?(categories(:child_2)).should be_false Chris@1115: categories(:child_1).is_ancestor_of?(categories(:child_1)).should be_false Chris@1115: end Chris@1115: Chris@1115: it "is_or_is_ancestor_of_with_scope" do Chris@1115: root = ScopedCategory.root Chris@1115: child = root.children.first Chris@1115: root.is_or_is_ancestor_of?(child).should be_true Chris@1115: child.update_attribute :organization_id, 'different' Chris@1115: root.is_or_is_ancestor_of?(child).should be_false Chris@1115: end Chris@1115: Chris@1115: it "is_or_is_descendant_of?" do Chris@1115: categories(:child_1).is_or_is_descendant_of?(categories(:top_level)).should be_true Chris@1115: categories(:child_2_1).is_or_is_descendant_of?(categories(:top_level)).should be_true Chris@1115: categories(:child_2_1).is_or_is_descendant_of?(categories(:child_2)).should be_true Chris@1115: categories(:child_2).is_or_is_descendant_of?(categories(:child_2_1)).should be_false Chris@1115: categories(:child_2).is_or_is_descendant_of?(categories(:child_1)).should be_false Chris@1115: categories(:child_1).is_or_is_descendant_of?(categories(:child_1)).should be_true Chris@1115: end Chris@1115: Chris@1115: it "is_descendant_of?" do Chris@1115: categories(:child_1).is_descendant_of?(categories(:top_level)).should be_true Chris@1115: categories(:child_2_1).is_descendant_of?(categories(:top_level)).should be_true Chris@1115: categories(:child_2_1).is_descendant_of?(categories(:child_2)).should be_true Chris@1115: categories(:child_2).is_descendant_of?(categories(:child_2_1)).should be_false Chris@1115: categories(:child_2).is_descendant_of?(categories(:child_1)).should be_false Chris@1115: categories(:child_1).is_descendant_of?(categories(:child_1)).should be_false Chris@1115: end Chris@1115: Chris@1115: it "is_or_is_descendant_of_with_scope" do Chris@1115: root = ScopedCategory.root Chris@1115: child = root.children.first Chris@1115: child.is_or_is_descendant_of?(root).should be_true Chris@1115: child.update_attribute :organization_id, 'different' Chris@1115: child.is_or_is_descendant_of?(root).should be_false Chris@1115: end Chris@1115: Chris@1115: it "same_scope?" do Chris@1115: root = ScopedCategory.root Chris@1115: child = root.children.first Chris@1115: child.same_scope?(root).should be_true Chris@1115: child.update_attribute :organization_id, 'different' Chris@1115: child.same_scope?(root).should be_false Chris@1115: end Chris@1115: Chris@1115: it "left_sibling" do Chris@1115: categories(:child_1).should == categories(:child_2).left_sibling Chris@1115: categories(:child_2).should == categories(:child_3).left_sibling Chris@1115: end Chris@1115: Chris@1115: it "left_sibling_of_root" do Chris@1115: categories(:top_level).left_sibling.should be_nil Chris@1115: end Chris@1115: Chris@1115: it "left_sibling_without_siblings" do Chris@1115: categories(:child_2_1).left_sibling.should be_nil Chris@1115: end Chris@1115: Chris@1115: it "left_sibling_of_leftmost_node" do Chris@1115: categories(:child_1).left_sibling.should be_nil Chris@1115: end Chris@1115: Chris@1115: it "right_sibling" do Chris@1115: categories(:child_3).should == categories(:child_2).right_sibling Chris@1115: categories(:child_2).should == categories(:child_1).right_sibling Chris@1115: end Chris@1115: Chris@1115: it "right_sibling_of_root" do Chris@1115: categories(:top_level_2).should == categories(:top_level).right_sibling Chris@1115: categories(:top_level_2).right_sibling.should be_nil Chris@1115: end Chris@1115: Chris@1115: it "right_sibling_without_siblings" do Chris@1115: categories(:child_2_1).right_sibling.should be_nil Chris@1115: end Chris@1115: Chris@1115: it "right_sibling_of_rightmost_node" do Chris@1115: categories(:child_3).right_sibling.should be_nil Chris@1115: end Chris@1115: Chris@1115: it "move_left" do Chris@1115: categories(:child_2).move_left Chris@1115: categories(:child_2).left_sibling.should be_nil Chris@1115: categories(:child_1).should == categories(:child_2).right_sibling Chris@1115: Category.valid?.should be_true Chris@1115: end Chris@1115: Chris@1115: it "move_right" do Chris@1115: categories(:child_2).move_right Chris@1115: categories(:child_2).right_sibling.should be_nil Chris@1115: categories(:child_3).should == categories(:child_2).left_sibling Chris@1115: Category.valid?.should be_true Chris@1115: end Chris@1115: Chris@1115: it "move_to_left_of" do Chris@1115: categories(:child_3).move_to_left_of(categories(:child_1)) Chris@1115: categories(:child_3).left_sibling.should be_nil Chris@1115: categories(:child_1).should == categories(:child_3).right_sibling Chris@1115: Category.valid?.should be_true Chris@1115: end Chris@1115: Chris@1115: it "move_to_right_of" do Chris@1115: categories(:child_1).move_to_right_of(categories(:child_3)) Chris@1115: categories(:child_1).right_sibling.should be_nil Chris@1115: categories(:child_3).should == categories(:child_1).left_sibling Chris@1115: Category.valid?.should be_true Chris@1115: end Chris@1115: Chris@1115: it "move_to_root" do Chris@1115: categories(:child_2).move_to_root Chris@1115: categories(:child_2).parent.should be_nil Chris@1115: categories(:child_2).level.should == 0 Chris@1115: categories(:child_2_1).level.should == 1 Chris@1115: categories(:child_2).left.should == 1 Chris@1115: categories(:child_2).right.should == 4 Chris@1115: Category.valid?.should be_true Chris@1115: end Chris@1115: Chris@1115: it "move_to_child_of" do Chris@1115: categories(:child_1).move_to_child_of(categories(:child_3)) Chris@1115: categories(:child_3).id.should == categories(:child_1).parent_id Chris@1115: Category.valid?.should be_true Chris@1115: end Chris@1115: Chris@1115: it "move_to_child_of_appends_to_end" do Chris@1115: child = Category.create! :name => 'New Child' Chris@1115: child.move_to_child_of categories(:top_level) Chris@1115: child.should == categories(:top_level).children.last Chris@1115: end Chris@1115: Chris@1115: it "subtree_move_to_child_of" do Chris@1115: categories(:child_2).left.should == 4 Chris@1115: categories(:child_2).right.should == 7 Chris@1115: Chris@1115: categories(:child_1).left.should == 2 Chris@1115: categories(:child_1).right.should == 3 Chris@1115: Chris@1115: categories(:child_2).move_to_child_of(categories(:child_1)) Chris@1115: Category.valid?.should be_true Chris@1115: categories(:child_1).id.should == categories(:child_2).parent_id Chris@1115: Chris@1115: categories(:child_2).left.should == 3 Chris@1115: categories(:child_2).right.should == 6 Chris@1115: categories(:child_1).left.should == 2 Chris@1115: categories(:child_1).right.should == 7 Chris@1115: end Chris@1115: Chris@1115: it "slightly_difficult_move_to_child_of" do Chris@1115: categories(:top_level_2).left.should == 11 Chris@1115: categories(:top_level_2).right.should == 12 Chris@1115: Chris@1115: # create a new top-level node and move single-node top-level tree inside it. Chris@1115: new_top = Category.create(:name => 'New Top') Chris@1115: new_top.left.should == 13 Chris@1115: new_top.right.should == 14 Chris@1115: Chris@1115: categories(:top_level_2).move_to_child_of(new_top) Chris@1115: Chris@1115: Category.valid?.should be_true Chris@1115: new_top.id.should == categories(:top_level_2).parent_id Chris@1115: Chris@1115: categories(:top_level_2).left.should == 12 Chris@1115: categories(:top_level_2).right.should == 13 Chris@1115: new_top.left.should == 11 Chris@1115: new_top.right.should == 14 Chris@1115: end Chris@1115: Chris@1115: it "difficult_move_to_child_of" do Chris@1115: categories(:top_level).left.should == 1 Chris@1115: categories(:top_level).right.should == 10 Chris@1115: categories(:child_2_1).left.should == 5 Chris@1115: categories(:child_2_1).right.should == 6 Chris@1115: Chris@1115: # create a new top-level node and move an entire top-level tree inside it. Chris@1115: new_top = Category.create(:name => 'New Top') Chris@1115: categories(:top_level).move_to_child_of(new_top) Chris@1115: categories(:child_2_1).reload Chris@1115: Category.valid?.should be_true Chris@1115: new_top.id.should == categories(:top_level).parent_id Chris@1115: Chris@1115: categories(:top_level).left.should == 4 Chris@1115: categories(:top_level).right.should == 13 Chris@1115: categories(:child_2_1).left.should == 8 Chris@1115: categories(:child_2_1).right.should == 9 Chris@1115: end Chris@1115: Chris@1115: #rebuild swaps the position of the 2 children when added using move_to_child twice onto same parent Chris@1115: it "move_to_child_more_than_once_per_parent_rebuild" do Chris@1115: root1 = Category.create(:name => 'Root1') Chris@1115: root2 = Category.create(:name => 'Root2') Chris@1115: root3 = Category.create(:name => 'Root3') Chris@1115: Chris@1115: root2.move_to_child_of root1 Chris@1115: root3.move_to_child_of root1 Chris@1115: Chris@1115: output = Category.roots.last.to_text Chris@1115: Category.update_all('lft = null, rgt = null') Chris@1115: Category.rebuild! Chris@1115: Chris@1115: Category.roots.last.to_text.should == output Chris@1115: end Chris@1115: Chris@1115: # doing move_to_child twice onto same parent from the furthest right first Chris@1115: it "move_to_child_more_than_once_per_parent_outside_in" do Chris@1115: node1 = Category.create(:name => 'Node-1') Chris@1115: node2 = Category.create(:name => 'Node-2') Chris@1115: node3 = Category.create(:name => 'Node-3') Chris@1115: Chris@1115: node2.move_to_child_of node1 Chris@1115: node3.move_to_child_of node1 Chris@1115: Chris@1115: output = Category.roots.last.to_text Chris@1115: Category.update_all('lft = null, rgt = null') Chris@1115: Category.rebuild! Chris@1115: Chris@1115: Category.roots.last.to_text.should == output Chris@1115: end Chris@1115: Chris@1115: it "should be able to rebuild without validating each record" do Chris@1115: root1 = Category.create(:name => 'Root1') Chris@1115: root2 = Category.create(:name => 'Root2') Chris@1115: root3 = Category.create(:name => 'Root3') Chris@1115: Chris@1115: root2.move_to_child_of root1 Chris@1115: root3.move_to_child_of root1 Chris@1115: Chris@1115: root2.name = nil Chris@1115: root2.save!(:validate => false) Chris@1115: Chris@1115: output = Category.roots.last.to_text Chris@1115: Category.update_all('lft = null, rgt = null') Chris@1115: Category.rebuild!(false) Chris@1115: Chris@1115: Category.roots.last.to_text.should == output Chris@1115: end Chris@1115: Chris@1115: it "valid_with_null_lefts" do Chris@1115: Category.valid?.should be_true Chris@1115: Category.update_all('lft = null') Chris@1115: Category.valid?.should be_false Chris@1115: end Chris@1115: Chris@1115: it "valid_with_null_rights" do Chris@1115: Category.valid?.should be_true Chris@1115: Category.update_all('rgt = null') Chris@1115: Category.valid?.should be_false Chris@1115: end Chris@1115: Chris@1115: it "valid_with_missing_intermediate_node" do Chris@1115: # Even though child_2_1 will still exist, it is a sign of a sloppy delete, not an invalid tree. Chris@1115: Category.valid?.should be_true Chris@1115: Category.delete(categories(:child_2).id) Chris@1115: Category.valid?.should be_true Chris@1115: end Chris@1115: Chris@1115: it "valid_with_overlapping_and_rights" do Chris@1115: Category.valid?.should be_true Chris@1115: categories(:top_level_2)['lft'] = 0 Chris@1115: categories(:top_level_2).save Chris@1115: Category.valid?.should be_false Chris@1115: end Chris@1115: Chris@1115: it "rebuild" do Chris@1115: Category.valid?.should be_true Chris@1115: before_text = Category.root.to_text Chris@1115: Category.update_all('lft = null, rgt = null') Chris@1115: Category.rebuild! Chris@1115: Category.valid?.should be_true Chris@1115: before_text.should == Category.root.to_text Chris@1115: end Chris@1115: Chris@1115: it "move_possible_for_sibling" do Chris@1115: categories(:child_2).move_possible?(categories(:child_1)).should be_true Chris@1115: end Chris@1115: Chris@1115: it "move_not_possible_to_self" do Chris@1115: categories(:top_level).move_possible?(categories(:top_level)).should be_false Chris@1115: end Chris@1115: Chris@1115: it "move_not_possible_to_parent" do Chris@1115: categories(:top_level).descendants.each do |descendant| Chris@1115: categories(:top_level).move_possible?(descendant).should be_false Chris@1115: descendant.move_possible?(categories(:top_level)).should be_true Chris@1115: end Chris@1115: end Chris@1115: Chris@1115: it "is_or_is_ancestor_of?" do Chris@1115: [:child_1, :child_2, :child_2_1, :child_3].each do |c| Chris@1115: categories(:top_level).is_or_is_ancestor_of?(categories(c)).should be_true Chris@1115: end Chris@1115: categories(:top_level).is_or_is_ancestor_of?(categories(:top_level_2)).should be_false Chris@1115: end Chris@1115: Chris@1115: it "left_and_rights_valid_with_blank_left" do Chris@1115: Category.left_and_rights_valid?.should be_true Chris@1115: categories(:child_2)[:lft] = nil Chris@1115: categories(:child_2).save(:validate => false) Chris@1115: Category.left_and_rights_valid?.should be_false Chris@1115: end Chris@1115: Chris@1115: it "left_and_rights_valid_with_blank_right" do Chris@1115: Category.left_and_rights_valid?.should be_true Chris@1115: categories(:child_2)[:rgt] = nil Chris@1115: categories(:child_2).save(:validate => false) Chris@1115: Category.left_and_rights_valid?.should be_false Chris@1115: end Chris@1115: Chris@1115: it "left_and_rights_valid_with_equal" do Chris@1115: Category.left_and_rights_valid?.should be_true Chris@1115: categories(:top_level_2)[:lft] = categories(:top_level_2)[:rgt] Chris@1115: categories(:top_level_2).save(:validate => false) Chris@1115: Category.left_and_rights_valid?.should be_false Chris@1115: end Chris@1115: Chris@1115: it "left_and_rights_valid_with_left_equal_to_parent" do Chris@1115: Category.left_and_rights_valid?.should be_true Chris@1115: categories(:child_2)[:lft] = categories(:top_level)[:lft] Chris@1115: categories(:child_2).save(:validate => false) Chris@1115: Category.left_and_rights_valid?.should be_false Chris@1115: end Chris@1115: Chris@1115: it "left_and_rights_valid_with_right_equal_to_parent" do Chris@1115: Category.left_and_rights_valid?.should be_true Chris@1115: categories(:child_2)[:rgt] = categories(:top_level)[:rgt] Chris@1115: categories(:child_2).save(:validate => false) Chris@1115: Category.left_and_rights_valid?.should be_false Chris@1115: end Chris@1115: Chris@1115: it "moving_dirty_objects_doesnt_invalidate_tree" do Chris@1115: r1 = Category.create :name => "Test 1" Chris@1115: r2 = Category.create :name => "Test 2" Chris@1115: r3 = Category.create :name => "Test 3" Chris@1115: r4 = Category.create :name => "Test 4" Chris@1115: nodes = [r1, r2, r3, r4] Chris@1115: Chris@1115: r2.move_to_child_of(r1) Chris@1115: Category.valid?.should be_true Chris@1115: Chris@1115: r3.move_to_child_of(r1) Chris@1115: Category.valid?.should be_true Chris@1115: Chris@1115: r4.move_to_child_of(r2) Chris@1115: Category.valid?.should be_true Chris@1115: end Chris@1115: Chris@1115: it "multi_scoped_no_duplicates_for_columns?" do Chris@1115: lambda { Chris@1115: Note.no_duplicates_for_columns? Chris@1115: }.should_not raise_exception Chris@1115: end Chris@1115: Chris@1115: it "multi_scoped_all_roots_valid?" do Chris@1115: lambda { Chris@1115: Note.all_roots_valid? Chris@1115: }.should_not raise_exception Chris@1115: end Chris@1115: Chris@1115: it "multi_scoped" do Chris@1115: note1 = Note.create!(:body => "A", :notable_id => 2, :notable_type => 'Category') Chris@1115: note2 = Note.create!(:body => "B", :notable_id => 2, :notable_type => 'Category') Chris@1115: note3 = Note.create!(:body => "C", :notable_id => 2, :notable_type => 'Default') Chris@1115: Chris@1115: [note1, note2].should == note1.self_and_siblings Chris@1115: [note3].should == note3.self_and_siblings Chris@1115: end Chris@1115: Chris@1115: it "multi_scoped_rebuild" do Chris@1115: root = Note.create!(:body => "A", :notable_id => 3, :notable_type => 'Category') Chris@1115: child1 = Note.create!(:body => "B", :notable_id => 3, :notable_type => 'Category') Chris@1115: child2 = Note.create!(:body => "C", :notable_id => 3, :notable_type => 'Category') Chris@1115: Chris@1115: child1.move_to_child_of root Chris@1115: child2.move_to_child_of root Chris@1115: Chris@1115: Note.update_all('lft = null, rgt = null') Chris@1115: Note.rebuild! Chris@1115: Chris@1115: Note.roots.find_by_body('A').should == root Chris@1115: [child1, child2].should == Note.roots.find_by_body('A').children Chris@1115: end Chris@1115: Chris@1115: it "same_scope_with_multi_scopes" do Chris@1115: lambda { Chris@1115: notes(:scope1).same_scope?(notes(:child_1)) Chris@1115: }.should_not raise_exception Chris@1115: notes(:scope1).same_scope?(notes(:child_1)).should be_true Chris@1115: notes(:child_1).same_scope?(notes(:scope1)).should be_true Chris@1115: notes(:scope1).same_scope?(notes(:scope2)).should be_false Chris@1115: end Chris@1115: Chris@1115: it "quoting_of_multi_scope_column_names" do Chris@1115: ["\"notable_id\"", "\"notable_type\""].should == Note.quoted_scope_column_names Chris@1115: end Chris@1115: Chris@1115: it "equal_in_same_scope" do Chris@1115: notes(:scope1).should == notes(:scope1) Chris@1115: notes(:scope1).should_not == notes(:child_1) Chris@1115: end Chris@1115: Chris@1115: it "equal_in_different_scopes" do Chris@1115: notes(:scope1).should_not == notes(:scope2) Chris@1115: end Chris@1115: Chris@1115: it "delete_does_not_invalidate" do Chris@1115: Category.acts_as_nested_set_options[:dependent] = :delete Chris@1115: categories(:child_2).destroy Chris@1115: Category.valid?.should be_true Chris@1115: end Chris@1115: Chris@1115: it "destroy_does_not_invalidate" do Chris@1115: Category.acts_as_nested_set_options[:dependent] = :destroy Chris@1115: categories(:child_2).destroy Chris@1115: Category.valid?.should be_true Chris@1115: end Chris@1115: Chris@1115: it "destroy_multiple_times_does_not_invalidate" do Chris@1115: Category.acts_as_nested_set_options[:dependent] = :destroy Chris@1115: categories(:child_2).destroy Chris@1115: categories(:child_2).destroy Chris@1115: Category.valid?.should be_true Chris@1115: end Chris@1115: Chris@1115: it "assigning_parent_id_on_create" do Chris@1115: category = Category.create!(:name => "Child", :parent_id => categories(:child_2).id) Chris@1115: categories(:child_2).should == category.parent Chris@1115: categories(:child_2).id.should == category.parent_id Chris@1115: category.left.should_not be_nil Chris@1115: category.right.should_not be_nil Chris@1115: Category.valid?.should be_true Chris@1115: end Chris@1115: Chris@1115: it "assigning_parent_on_create" do Chris@1115: category = Category.create!(:name => "Child", :parent => categories(:child_2)) Chris@1115: categories(:child_2).should == category.parent Chris@1115: categories(:child_2).id.should == category.parent_id Chris@1115: category.left.should_not be_nil Chris@1115: category.right.should_not be_nil Chris@1115: Category.valid?.should be_true Chris@1115: end Chris@1115: Chris@1115: it "assigning_parent_id_to_nil_on_create" do Chris@1115: category = Category.create!(:name => "New Root", :parent_id => nil) Chris@1115: category.parent.should be_nil Chris@1115: category.parent_id.should be_nil Chris@1115: category.left.should_not be_nil Chris@1115: category.right.should_not be_nil Chris@1115: Category.valid?.should be_true Chris@1115: end Chris@1115: Chris@1115: it "assigning_parent_id_on_update" do Chris@1115: category = categories(:child_2_1) Chris@1115: category.parent_id = categories(:child_3).id Chris@1115: category.save Chris@1115: category.reload Chris@1115: categories(:child_3).reload Chris@1115: categories(:child_3).should == category.parent Chris@1115: categories(:child_3).id.should == category.parent_id Chris@1115: Category.valid?.should be_true Chris@1115: end Chris@1115: Chris@1115: it "assigning_parent_on_update" do Chris@1115: category = categories(:child_2_1) Chris@1115: category.parent = categories(:child_3) Chris@1115: category.save Chris@1115: category.reload Chris@1115: categories(:child_3).reload Chris@1115: categories(:child_3).should == category.parent Chris@1115: categories(:child_3).id.should == category.parent_id Chris@1115: Category.valid?.should be_true Chris@1115: end Chris@1115: Chris@1115: it "assigning_parent_id_to_nil_on_update" do Chris@1115: category = categories(:child_2_1) Chris@1115: category.parent_id = nil Chris@1115: category.save Chris@1115: category.parent.should be_nil Chris@1115: category.parent_id.should be_nil Chris@1115: Category.valid?.should be_true Chris@1115: end Chris@1115: Chris@1115: it "creating_child_from_parent" do Chris@1115: category = categories(:child_2).children.create!(:name => "Child") Chris@1115: categories(:child_2).should == category.parent Chris@1115: categories(:child_2).id.should == category.parent_id Chris@1115: category.left.should_not be_nil Chris@1115: category.right.should_not be_nil Chris@1115: Category.valid?.should be_true Chris@1115: end Chris@1115: Chris@1115: def check_structure(entries, structure) Chris@1115: structure = structure.dup Chris@1115: Category.each_with_level(entries) do |category, level| Chris@1115: expected_level, expected_name = structure.shift Chris@1115: expected_name.should == category.name Chris@1115: expected_level.should == level Chris@1115: end Chris@1115: end Chris@1115: Chris@1115: it "each_with_level" do Chris@1115: levels = [ Chris@1115: [0, "Top Level"], Chris@1115: [1, "Child 1"], Chris@1115: [1, "Child 2"], Chris@1115: [2, "Child 2.1"], Chris@1115: [1, "Child 3" ]] Chris@1115: Chris@1115: check_structure(Category.root.self_and_descendants, levels) Chris@1115: Chris@1115: # test some deeper structures Chris@1115: category = Category.find_by_name("Child 1") Chris@1115: c1 = Category.new(:name => "Child 1.1") Chris@1115: c2 = Category.new(:name => "Child 1.1.1") Chris@1115: c3 = Category.new(:name => "Child 1.1.1.1") Chris@1115: c4 = Category.new(:name => "Child 1.2") Chris@1115: [c1, c2, c3, c4].each(&:save!) Chris@1115: Chris@1115: c1.move_to_child_of(category) Chris@1115: c2.move_to_child_of(c1) Chris@1115: c3.move_to_child_of(c2) Chris@1115: c4.move_to_child_of(category) Chris@1115: Chris@1115: levels = [ Chris@1115: [0, "Top Level"], Chris@1115: [1, "Child 1"], Chris@1115: [2, "Child 1.1"], Chris@1115: [3, "Child 1.1.1"], Chris@1115: [4, "Child 1.1.1.1"], Chris@1115: [2, "Child 1.2"], Chris@1115: [1, "Child 2"], Chris@1115: [2, "Child 2.1"], Chris@1115: [1, "Child 3" ]] Chris@1115: Chris@1115: check_structure(Category.root.self_and_descendants, levels) Chris@1115: end Chris@1115: Chris@1115: it "should not error on a model with attr_accessible" do Chris@1115: model = Class.new(ActiveRecord::Base) Chris@1115: model.table_name = 'categories' Chris@1115: model.attr_accessible :name Chris@1115: lambda { Chris@1115: model.acts_as_nested_set Chris@1115: model.new(:name => 'foo') Chris@1115: }.should_not raise_exception Chris@1115: end Chris@1115: Chris@1115: describe "before_move_callback" do Chris@1115: it "should fire the callback" do Chris@1115: categories(:child_2).should_receive(:custom_before_move) Chris@1115: categories(:child_2).move_to_root Chris@1115: end Chris@1115: Chris@1115: it "should stop move when callback returns false" do Chris@1115: Category.test_allows_move = false Chris@1115: categories(:child_3).move_to_root.should be_false Chris@1115: categories(:child_3).root?.should be_false Chris@1115: end Chris@1115: Chris@1115: it "should not halt save actions" do Chris@1115: Category.test_allows_move = false Chris@1115: categories(:child_3).parent_id = nil Chris@1115: categories(:child_3).save.should be_true Chris@1115: end Chris@1115: end Chris@1115: Chris@1115: describe "counter_cache" do Chris@1115: Chris@1115: it "should allow use of a counter cache for children" do Chris@1115: note1 = things(:parent1) Chris@1115: note1.children.count.should == 2 Chris@1115: end Chris@1115: Chris@1115: it "should increment the counter cache on create" do Chris@1115: note1 = things(:parent1) Chris@1115: note1.children.count.should == 2 Chris@1115: note1[:children_count].should == 2 Chris@1115: note1.children.create :body => 'Child 3' Chris@1115: note1.children.count.should == 3 Chris@1115: note1.reload Chris@1115: note1[:children_count].should == 3 Chris@1115: end Chris@1115: Chris@1115: it "should decrement the counter cache on destroy" do Chris@1115: note1 = things(:parent1) Chris@1115: note1.children.count.should == 2 Chris@1115: note1[:children_count].should == 2 Chris@1115: note1.children.last.destroy Chris@1115: note1.children.count.should == 1 Chris@1115: note1.reload Chris@1115: note1[:children_count].should == 1 Chris@1115: end Chris@1115: end Chris@1115: Chris@1115: describe "association callbacks on children" do Chris@1115: it "should call the appropriate callbacks on the children :has_many association " do Chris@1115: root = DefaultWithCallbacks.create Chris@1115: root.should_not be_new_record Chris@1115: Chris@1115: child = root.children.build Chris@1115: Chris@1115: root.before_add.should == child Chris@1115: root.after_add.should == child Chris@1115: Chris@1115: root.before_remove.should_not == child Chris@1115: root.after_remove.should_not == child Chris@1115: Chris@1115: child.save.should be_true Chris@1115: root.children.delete(child).should be_true Chris@1115: Chris@1115: root.before_remove.should == child Chris@1115: root.after_remove.should == child Chris@1115: end Chris@1115: end Chris@1115: end