Skip to content

Instantly share code, notes, and snippets.

@saks
Created December 14, 2010 23:22
Show Gist options
  • Save saks/741321 to your computer and use it in GitHub Desktop.
Save saks/741321 to your computer and use it in GitHub Desktop.
this should work
require 'rubygems'
require 'mongoid'
require 'mongoid_acts_as_tree'
Mongoid.configure do |config|
name = "control_development"
host = "localhost"
config.master = Mongo::Connection.new.db(name)
config.persist_in_safe_mode = false
end
class Msg
include Mongoid::Document
include Mongoid::Acts::Tree
acts_as_tree
field :name
end
if __FILE__ == $PROGRAM_NAME
require "test/unit"
class TestMsg < Test::Unit::TestCase
def test_move_children_with_children
Msg.destroy_all
parent1 = Msg.create! :name => 'name1'
parent2 = Msg.create! :name => 'name2'
fl_child = Msg.create! :name => 'test1', :parent_id => parent1.id
sl_child = Msg.create! :name => 'test2', :parent_id => fl_child.id
assert_equal ['test1', 'test2'], parent1.descendants.map {|o| o.name}
assert_equal [], parent2.descendants.map {|o| o.name}
a = Msg.find(:first, :conditions => {"name" => "test1"})
a.parent_id = parent2.id
a.save!
assert_equal [], parent1.descendants.map {|o| o.name}
assert_equal ['test1', 'test2'], parent2.descendants.map {|o| o.name}
assert_equal ['test2'], a.children.map {|o| o.name}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment