Skip to content
Browse files

Add: inverse_of option

  • Loading branch information...
1 parent e813892 commit 2c59dcaf52b9ef20126aff33732351b80288a0e9 Paul Gillard committed Mar 22, 2012
Showing with 19 additions and 2 deletions.
  1. +2 −2 lib/acts_as_tree/active_record/acts/tree.rb
  2. +17 −0 test/test_acts_as_list.rb
View
4 lib/acts_as_tree/active_record/acts/tree.rb
@@ -43,8 +43,8 @@ def acts_as_tree(options = {})
configuration = { :foreign_key => "parent_id", :order => nil, :counter_cache => nil }
configuration.update(options) if options.is_a?(Hash)
- belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key], :counter_cache => configuration[:counter_cache]
- has_many :children, :class_name => name, :foreign_key => configuration[:foreign_key], :order => configuration[:order], :dependent => :destroy
+ belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key], :counter_cache => configuration[:counter_cache], :inverse_of => :children
+ has_many :children, :class_name => name, :foreign_key => configuration[:foreign_key], :order => configuration[:order], :dependent => :destroy, :inverse_of => :parent
class_eval <<-EOV
include ActiveRecord::Acts::Tree::InstanceMethods
View
17 test/test_acts_as_list.rb
@@ -222,3 +222,20 @@ def test_roots
assert_equal [], [@root1, @root2] - TreeMixinWithoutOrder.roots
end
end
+
+class UnsavedTreeTest < Test::Unit::TestCase
+ def setup
+ setup_db
+ @root = TreeMixin.new
+ @root_child = @root.children.build
+ end
+
+ def teardown
+ teardown_db
+ end
+
+ def test_inverse_of
+ # We want children to be aware of their parent before saving either
+ assert_equal @root, @root_child.parent
+ end
+end

0 comments on commit 2c59dca

Please sign in to comment.
Something went wrong with that request. Please try again.