Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Testing and fixing #parent_id=, #parent, and #root. Using except(:whe…

…re) to be cautious but unscoped may be fine as well
  • Loading branch information...
commit 39e22117655b3ac3eb6030ae45ac8744ed06ad64 1 parent e30a4ce
Brian Underwood authored
Showing with 7 additions and 3 deletions.
  1. +3 −3 lib/ancestry/instance_methods.rb
  2. +4 −0 test/has_ancestry_test.rb
View
6 lib/ancestry/instance_methods.rb
@@ -103,7 +103,7 @@ def parent= parent
end
def parent_id= parent_id
- self.parent = if parent_id.blank? then nil else self.base_class.find(parent_id) end
+ self.parent = if parent_id.blank? then nil else self.base_class.scoped.except(:where).find(parent_id) end
end
def parent_id
@@ -111,7 +111,7 @@ def parent_id
end
def parent
- if parent_id.blank? then nil else self.base_class.find(parent_id) end
+ if parent_id.blank? then nil else self.base_class.scoped.except(:where).find(parent_id) end
end
# Root
@@ -120,7 +120,7 @@ def root_id
end
def root
- if root_id == id then self else self.base_class.find(root_id) end
+ if root_id == id then self else self.base_class.scoped.except(:where).find(root_id) end
end
def is_root?
View
4 test/has_ancestry_test.rb
@@ -53,6 +53,8 @@ def test_setting_invalid_orphan_strategy
def test_scoping_in_callbacks
AncestryTestDatabase.with_model do |model|
+ $random_object = model.create
+
model.instance_eval do
after_create :after_create_callback
end
@@ -60,6 +62,8 @@ def test_scoping_in_callbacks
def after_create_callback
# We don't want to be in the #children scope here when creating the child
self.parent
+ self.parent_id = $random_object.id if $random_object
+ self.root
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.