Skip to content

New self_and_ancestors_ids method to get self id with ancestor_ids #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions lib/closure_tree/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}+
Expand Down
2 changes: 2 additions & 0 deletions spec/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ def assert_mid_and_leaf_remain
u = User.find_or_create_by_path(%w([email protected] [email protected] [email protected]))
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]
Expand Down