From 51b63ca678908d778f784107a4e4943d1dba03ac Mon Sep 17 00:00:00 2001 From: Kir Shatrov Date: Wed, 5 Feb 2014 15:00:25 +0400 Subject: [PATCH 1/2] New `self_and_ancestors_ids` method to get self id with ancestor_ids --- lib/closure_tree/model.rb | 4 ++++ spec/user_spec.rb | 2 ++ 2 files changed, 6 insertions(+) diff --git a/lib/closure_tree/model.rb b/lib/closure_tree/model.rb index 5d8e2126..9ac7ce4a 100644 --- a/lib/closure_tree/model.rb +++ b/lib/closure_tree/model.rb @@ -86,6 +86,10 @@ def ancestor_ids _ct.ids_from(ancestors) end + def self_and_ancestors_ids + _ct.ids_from(ancestors) + [id] + end + # Returns an array, root first, of self_and_ancestors' values of the +to_s_column+, which defaults # to the +name_column+. # (so child.ancestry_path == +%w{grandparent parent child}+ diff --git a/spec/user_spec.rb b/spec/user_spec.rb index 2fef830f..55b9c2d6 100644 --- a/spec/user_spec.rb +++ b/spec/user_spec.rb @@ -84,8 +84,10 @@ def assert_mid_and_leaf_remain u = User.find_or_create_by_path(%w(a@t.co b@t.co c@t.co)) u.descendant_ids.should == [] u.ancestor_ids.should == [u.parent.id, u.root.id] + u.self_and_ancestor_ids.should == [u.parent.id, u.root.id] + [u.id] u.root.descendant_ids.should == [u.parent.id, u.id] u.root.ancestor_ids.should == [] + u.root.self_and_ancestor_ids.should == [u.root.id] c1 = u.contracts.create! c2 = u.parent.contracts.create! u.root.indirect_contracts.to_a.should =~ [c1, c2] From 009aca745e1e6e8f720b43506926a7dc2bcd4e7e Mon Sep 17 00:00:00 2001 From: Kir Shatrov Date: Wed, 5 Feb 2014 15:01:31 +0400 Subject: [PATCH 2/2] Mention `self_and_ancestors_ids` in README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6dfe0acc..8b6dc7a3 100644 --- a/README.md +++ b/README.md @@ -280,6 +280,7 @@ When you include ```acts_as_tree``` in your model, you can provide a hash to ove * ```tag.ancestors``` is a ordered scope of [ parent, grandparent, great grandparent, … ]. Note that the size of this array will always equal ```tag.depth```. * ```tag.ancestor_ids``` is an array of the IDs of the ancestors. * ```tag.self_and_ancestors``` returns a scope containing self, parent, grandparent, great grandparent, etc. +* ```tag.self_and_ancestors_ids``` returns IDS containing self, parent, grandparent, great grandparent, etc. * ```tag.siblings``` returns a scope containing all nodes with the same parent as ```tag```, excluding self. * ```tag.sibling_ids``` returns an array of the IDs of the siblings. * ```tag.self_and_siblings``` returns a scope containing all nodes with the same parent as ```tag```, including self.