Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

always discard scope when looking up root & parent

  • Loading branch information...
commit cd268b6ebb7940ad5e98fb765aba5a923d298b08 1 parent e552e10
Stefan Henzen authored
Showing with 12 additions and 4 deletions.
  1. +8 −4 lib/ancestry/instance_methods.rb
  2. +4 −0 test/has_ancestry_test.rb
View
12 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 unscoped_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 unscoped_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 unscoped_find(root_id) end
end
def is_root?
@@ -221,7 +221,7 @@ def primary_key_type
end
def unscoped_descendants
- self.base_class.unscoped do
+ self.base_class.unscoped do
self.base_class.all(:conditions => descendant_conditions)
end
end
@@ -231,5 +231,9 @@ def unscoped_descendants
def sane_ancestry?
ancestry.nil? || (ancestry.to_s =~ Ancestry::ANCESTRY_PATTERN && !ancestor_ids.include?(self.id))
end
+
+ def unscoped_find id
+ self.base_class.unscoped { self.base_class.find(id) }
+ end
end
end
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.