Skip to content

Commit

Permalink
always discard scope when looking up root & parent
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Henzen committed Jun 27, 2012
1 parent e552e10 commit cd268b6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/ancestry/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ 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
if ancestor_ids.empty? then nil else ancestor_ids.last end
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
Expand All @@ -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?
Expand Down Expand Up @@ -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
Expand All @@ -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
4 changes: 4 additions & 0 deletions test/has_ancestry_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,17 @@ 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
model.class_eval do
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

Expand Down

0 comments on commit cd268b6

Please sign in to comment.