Skip to content

Instantly share code, notes, and snippets.

@nmk
Created September 28, 2011 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmk/1248123 to your computer and use it in GitHub Desktop.
Save nmk/1248123 to your computer and use it in GitHub Desktop.
If a cyclic model is namespaced, root and parent relationships are lost
require 'mongoid'
require 'minitest/autorun'
Mongoid.configure do |config|
config.master = Mongo::Connection.new.db('nodes_test')
end
module Tree
class Node
include Mongoid::Document
recursively_embeds_many
field :name
def is_root?
parent_node.nil?
end
end
end
class NodeTest < MiniTest::Unit::TestCase
include Tree
def setup
@root = Node.new(name: 'root')
@first_child = @root.child_nodes.build(name: 'first_child')
@second_child = @root.child_nodes.build(name: 'second_child')
@root.save!
end
def test_a_node_knows_if_it_is_a_root
assert @root.is_root?
end
def test_a_node_knows_if_it_not_a_root
refute @root.child_nodes.first.is_root?
end
def test_a_node_knows_its_parent
assert_equal @root, @root.child_nodes.first.parent_node
end
end
require 'mongoid'
require 'minitest/autorun'
Mongoid.configure do |config|
config.master = Mongo::Connection.new.db('nodes_test')
end
class Node
include Mongoid::Document
recursively_embeds_many
field :name
def is_root?
parent_node.nil?
end
end
class NodeTest < MiniTest::Unit::TestCase
def setup
@root = Node.new(name: 'root')
@first_child = @root.child_nodes.build(name: 'first_child')
@second_child = @root.child_nodes.build(name: 'second_child')
@root.save!
end
def test_a_node_knows_if_it_is_a_root
assert @root.is_root?
end
def test_a_node_knows_if_it_not_a_root
refute @root.child_nodes.first.is_root?
end
def test_a_node_knows_its_parent
assert_equal @root, @root.child_nodes.first.parent_node
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment