Skip to content

Commit

Permalink
Fixed is_or_is_ancestor_of? and is_or_is_descendant_of? which were no…
Browse files Browse the repository at this point in the history
…t using scope. Added is_same_scope? which is used by both.

git-svn-id: https://source.collectiveidea.com/public/rails/plugins/awesome_nested_set@288 2c0bb46f-6917-0410-8ab9-f4bf1162979e
  • Loading branch information
danielmorrison committed Jan 18, 2008
1 parent 47cccca commit 6a5b751
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/awesome_nested_set.rb
Expand Up @@ -401,11 +401,15 @@ def children(multiplicity = :all, *args)
end

def is_or_is_descendant_of?(other)
other.left <= self.left && self.left < other.right
other.left <= self.left && self.left < other.right && is_same_scope?(other)
end

def is_or_is_ancestor_of?(other)
self.left <= other.left && other.left < self.right
self.left <= other.left && other.left < self.right && is_same_scope?(other)
end

def is_same_scope?(other)
!acts_as_nested_set_options[:scope] || self.send(scope_column_name) == other.send(scope_column_name)
end

# Find the first sibling to the right
Expand Down
26 changes: 25 additions & 1 deletion test/awesome_nested_set_test.rb
Expand Up @@ -156,7 +156,15 @@ def test_is_or_is_ancestor_of?
assert !categories(:child_2_1).is_or_is_ancestor_of?(categories(:child_2))
assert !categories(:child_1).is_or_is_ancestor_of?(categories(:child_2))
end


def test_is_or_is_ancestor_of_with_scope
root = Scoped.root
child = root.children.first
assert root.is_or_is_ancestor_of?(child)
child.update_attribute :organization_id, 'different'
assert !root.is_or_is_ancestor_of?(child)
end

def test_is_or_is_descendant_of?
assert categories(:child_1).is_or_is_descendant_of?(categories(:top_level))
assert categories(:child_2_1).is_or_is_descendant_of?(categories(:top_level))
Expand All @@ -165,6 +173,22 @@ def test_is_or_is_descendant_of?
assert !categories(:child_2).is_or_is_descendant_of?(categories(:child_1))
end

def test_is_or_is_descendant_of_with_scope
root = Scoped.root
child = root.children.first
assert child.is_or_is_descendant_of?(root)
child.update_attribute :organization_id, 'different'
assert !child.is_or_is_descendant_of?(root)
end

def test_is_same_scope?
root = Scoped.root
child = root.children.first
assert child.is_same_scope?(root)
child.update_attribute :organization_id, 'different'
assert !child.is_same_scope?(root)
end

def test_left_sibling
assert_equal categories(:child_1), categories(:child_2).left_sibling
assert_equal categories(:child_2), categories(:child_3).left_sibling
Expand Down
1 change: 1 addition & 0 deletions test/db/schema.rb
Expand Up @@ -5,6 +5,7 @@
t.column :parent_id, :integer
t.column :lft, :integer
t.column :rgt, :integer
t.column :organization_id, :integer
end

create_table :departments, :force => true do |t|
Expand Down

0 comments on commit 6a5b751

Please sign in to comment.