Skip to content
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

Rename parent_ids to ancestor_ids #60

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 20 additions & 20 deletions lib/mongoid/tree.rb
Expand Up @@ -89,8 +89,8 @@ module Tree

belongs_to :parent, :class_name => self.name, :inverse_of => :children, :index => true, :validate => false

field :parent_ids, :type => Array, :default => []
index :parent_ids => 1
field :ancestor_ids, :type => Array, :default => []
index :ancestor_ids => 1

field :depth, :type => Integer
index :depth => 1
Expand Down Expand Up @@ -220,8 +220,8 @@ def leaves
# @param [Mongoid::Tree] document

##
# @!method parent_ids
# Returns a list of the document's parent_ids, starting with the root node.
# @!method ancestor_ids
# Returns a list of the document's ancestor_ids, starting with the root node.
#
# @note Generated by Mongoid
#
Expand All @@ -236,7 +236,7 @@ def leaves
#
# @return [Fixnum] Depth of this document
def depth
super || parent_ids.count
super || ancestor_ids.count
end

##
Expand Down Expand Up @@ -265,8 +265,8 @@ def leaf?
#
# @return [Mongoid::Document] The documents root node
def root
if parent_ids.present?
base_class.find(parent_ids.first)
if ancestor_ids.present?
base_class.find(ancestor_ids.first)
else
self.root? ? self : self.parent.root
end
Expand All @@ -277,7 +277,7 @@ def root
#
# @return [Mongoid::Criteria] Mongoid criteria to retrieve the documents ancestors
def ancestors
base_class.where(:_id.in => parent_ids).order(:depth => :asc)
base_class.where(:_id.in => ancestor_ids).order(:depth => :asc)
end

##
Expand All @@ -295,15 +295,15 @@ def ancestors_and_self
#
# @return [Boolean] The document is an ancestor of the other document
def ancestor_of?(other)
other.parent_ids.include?(self.id)
other.ancestor_ids.include?(self.id)
end

##
# Returns a chainable criteria for this document's descendants
#
# @return [Mongoid::Criteria] Mongoid criteria to retrieve the document's descendants
def descendants
base_class.where(:parent_ids => self.id)
base_class.where(:ancestor_ids => self.id)
end

##
Expand All @@ -321,7 +321,7 @@ def descendants_and_self
#
# @return [Boolean] The document is a descendant of the other document
def descendant_of?(other)
self.parent_ids.include?(other.id)
self.ancestor_ids.include?(other.id)
end

##
Expand Down Expand Up @@ -355,7 +355,7 @@ def sibling_of?(other)
#
# @return [Mongoid::Criteria] Mongoid criteria to retrieve the document's leaves
def leaves
base_class.where(:_id.nin => base_class.only(:parent_id).collect(&:parent_id)).and(:parent_ids => self.id)
base_class.where(:_id.nin => base_class.only(:parent_id).collect(&:parent_id)).and(:ancestor_ids => self.id)
end

##
Expand Down Expand Up @@ -401,7 +401,7 @@ def move_children_to_parent
#
# @return [undefined]
def delete_descendants
base_class.delete_all(:conditions => { :parent_ids => self.id })
base_class.delete_all(:conditions => { :ancestor_ids => self.id })
end

##
Expand All @@ -415,21 +415,21 @@ def destroy_children
private

##
# Updates the parent_ids and marks the children for
# rearrangement when the parent_ids changed
# Updates the ancestor_ids and marks the children for
# rearrangement when the ancestor_ids changed
#
# @private
# @return [undefined]
def rearrange
if self.parent_id
self.parent_ids = parent.parent_ids + [self.parent_id]
self.ancestor_ids = parent.ancestor_ids + [self.parent_id]
else
self.parent_ids = []
self.ancestor_ids = []
end

self.depth = parent_ids.size
self.depth = ancestor_ids.size

rearrange_children! if self.parent_ids_changed?
rearrange_children! if self.ancestor_ids_changed?
end

def rearrange_children
Expand All @@ -438,7 +438,7 @@ def rearrange_children
end

def position_in_tree
errors.add(:parent_id, :invalid) if self.parent_ids.include?(self.id)
errors.add(:parent_id, :invalid) if self.ancestor_ids.include?(self.id)
end
end
end
26 changes: 13 additions & 13 deletions spec/mongoid/tree_spec.rb
Expand Up @@ -21,12 +21,12 @@
expect(a.inverse_of).to eq(:children)
end

it "should store parent_ids as Array with [] as default with index" do
f = Node.fields['parent_ids']
it "should store ancestor_ids as Array with [] as default with index" do
f = Node.fields['ancestor_ids']
expect(f).to be
expect(f.options[:type]).to eq(Array)
expect(f.options[:default]).to eq([])
expect(Node.index_specification(:parent_ids => 1)).to be
expect(Node.index_specification(:ancestor_ids => 1)).to be
end

it "should store the depth as Integer with index" do
Expand Down Expand Up @@ -84,34 +84,34 @@
expect(child.parent_id).to eq(root.id)
end

it "should rebuild its parent_ids" do
it "should rebuild its ancestor_ids" do
root = Node.create; child = Node.create
root.children << child
expect(child.parent_ids).to eq([root.id])
expect(child.ancestor_ids).to eq([root.id])
end

it "should rebuild its children's parent_ids when its own parent_ids changed" do
it "should rebuild its children's ancestor_ids when its own ancestor_ids changed" do
other_root = node(:other_root); child = node(:child); subchild = node(:subchild);
other_root.children << child
subchild.reload # To get the updated version
expect(subchild.parent_ids).to eq([other_root.id, child.id])
expect(subchild.ancestor_ids).to eq([other_root.id, child.id])
end

it "should correctly rebuild its descendants' parent_ids when moved into an other subtree" do
it "should correctly rebuild its descendants' ancestor_ids when moved into an other subtree" do
subchild = node(:subchild); subsubchild = node(:subsubchild); other_child = node(:other_child)
other_child.children << subchild
subsubchild.reload
expect(subsubchild.parent_ids).to eq([node(:other_root).id, other_child.id, subchild.id])
expect(subsubchild.ancestor_ids).to eq([node(:other_root).id, other_child.id, subchild.id])
end

it "should rebuild its children's parent_ids when its own parent_id is removed" do
it "should rebuild its children's ancestor_ids when its own parent_id is removed" do
c = node(:child)
c.parent_id = nil
c.save
expect(node(:subchild).parent_ids).to eq([node(:child).id])
expect(node(:subchild).ancestor_ids).to eq([node(:child).id])
end

it "should not rebuild its children's parent_ids when it's not required" do
it "should not rebuild its children's ancestor_ids when it's not required" do
root = node(:root)
expect(root).not_to receive(:rearrange_children)
root.save
Expand Down Expand Up @@ -195,7 +195,7 @@
describe ':delete_descendants' do
it "should delete all descendants" do
root = node(:root)
expect(Node).to receive(:delete_all).with(:conditions => { :parent_ids => root.id })
expect(Node).to receive(:delete_all).with(:conditions => { :ancestor_ids => root.id })
root.delete_descendants
end
end
Expand Down