Skip to content
This repository has been archived by the owner on Sep 9, 2021. It is now read-only.

Commit

Permalink
extra save removed
Browse files Browse the repository at this point in the history
  • Loading branch information
saks committed Jul 5, 2010
1 parent 249840a commit 7e2a125
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
8 changes: 5 additions & 3 deletions lib/mongoid_acts_as_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def acts_as_tree(options = {})
self.class_eval do
define_method "#{parent_id_field}=" do | new_parent_id |
new_parent = self.class.find new_parent_id
new_parent.children << self
new_parent.children.push self, false
end
end

Expand Down Expand Up @@ -184,20 +184,22 @@ def initialize(owner)
end

#Add new child to list of object children
def <<(object)
def <<(object, will_save=true)
if object.descendants.include? @parent
object.instance_variable_set :@_cyclic, true
else
object.write_attribute object.parent_id_field, @parent._id
object[object.path_field] = @parent[@parent.path_field] + [@parent._id]
object[object.depth_field] = @parent[@parent.depth_field] + 1
object.instance_variable_set :@_will_move, true
object.save
object.save if will_save
end

super(object)
end

alias push <<

#Deletes object only from children list.
#To delete object use <tt>object.destroy</tt>.
def delete(object_or_id)
Expand Down
2 changes: 1 addition & 1 deletion mongoid_acts_as_tree.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Gem::Specification.new do |s|
s.name = %q{mongoid_acts_as_tree}
s.version = "0.1.4"
s.version = "0.1.5"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Jakob Vidmar, Aliaksandr Rahalevich"]
Expand Down
10 changes: 8 additions & 2 deletions test/test_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,14 @@ class TestMongoidActsAsTree < Test::Unit::TestCase
assert_equal 1, child.depth
assert_equal [parent.id], child.path

more_deep_child = Category.create :name => 'more deep child'
more_deep_child.parent_id = child.id
more_deep_child = Category.new(
:name => 'more deep child',
:parent_id => child.id
)

assert more_deep_child.new_record?
more_deep_child.save
assert !more_deep_child.new_record?

assert_equal child.children.first.id, more_deep_child.id
assert_equal child.id, more_deep_child.parent_id
Expand Down

0 comments on commit 7e2a125

Please sign in to comment.