Skip to content

Commit

Permalink
removing whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Sara Trice committed Nov 26, 2013
1 parent e56807e commit ad2a8c5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
10 changes: 5 additions & 5 deletions README.rdoc
Expand Up @@ -64,7 +64,7 @@ To navigate an Ancestry model, use the following methods on any instance / recor
subtree_ids Returns a list of all ids in the record's subtree
depth Return the depth of the node, root nodes are at depth 0

= Options for has_ancestry
= Options for has_ancestry

The has_ancestry methods supports the following options:

Expand Down Expand Up @@ -115,7 +115,7 @@ When depth caching is enabled (see has_ancestry options), five more named scopes
at_depth(depth) Return nodes that are at depth (node.depth == depth)
from_depth(depth) Return nodes starting from a certain depth (node.depth >= depth)
after_depth(depth) Return nodes that are deeper than depth (node.depth > depth)

The depth scopes are also available through calls to descendants, descendant_ids, subtree, subtree_ids, path and ancestors. In this case, depth values are interpreted relatively. Some examples:

node.subtree(:to_depth => 2) Subtree of node, to a depth of node.depth + 2 (self, children and grandchildren)
Expand Down Expand Up @@ -175,7 +175,7 @@ The result of arrange_serializable can easily be serialized to json with 'to_jso
= Sorting

If you just want to sort an array of nodes as if you were traversing them in preorder, you can use the sort_by_ancestry class method:

TreeNode.sort_by_ancestry(array_of_nodes)

Note that since materialised path trees don't support ordering within a rank, the order of siblings depends on their order in the original array.
Expand All @@ -194,7 +194,7 @@ Most current tree plugins use a parent_id column (has_ancestry, awesome_nested_s
- Remove gem config line from environment.rb: config.gem [old gem]
- Add Ancestry to environment.rb: config.gem :ancestry
- See 'Installation' for more info on installing and configuring gems

3. Change your model
- Remove any macros required by old plugin/gem from app/models/[model].rb
- Add to app/models/[model].rb: <b>has_ancestry</b>
Expand All @@ -215,7 +215,7 @@ Most current tree plugins use a parent_id column (has_ancestry, awesome_nested_s

= Integrity checking and restoration

I don't see any way Ancestry tree integrity could get compromised without explicitly setting cyclic parents or invalid ancestry and circumventing validation with update_attribute, if you do, please let me know.
I don't see any way Ancestry tree integrity could get compromised without explicitly setting cyclic parents or invalid ancestry and circumventing validation with update_attribute, if you do, please let me know.

Ancestry includes some methods for detecting integrity problems and restoring integrity just to be sure. To check integrity use: [Model].check_ancestry_integrity!. An AncestryIntegrityException will be raised if there are any problems. You can also specify :report => :list to return an array of exceptions or :report => :echo to echo any error messages. To restore integrity use: [Model].restore_ancestry_integrity!.

Expand Down
42 changes: 21 additions & 21 deletions lib/ancestry/class_methods.rb
Expand Up @@ -3,8 +3,8 @@ module ClassMethods
# Fetch tree node if necessary
def to_node object
if object.is_a?(self.ancestry_base_class) then object else find(object) end
end
end

# Scope on relative depth options
def scope_depth depth_options, depth
depth_options.inject(self.ancestry_base_class) do |scope, option|
Expand All @@ -26,7 +26,7 @@ def orphan_strategy= orphan_strategy
raise Ancestry::AncestryException.new("Invalid orphan strategy, valid ones are :rootify,:adopt, :restrict and :destroy.")
end
end

# Arrangement
def arrange options = {}
scope =
Expand All @@ -38,8 +38,8 @@ def arrange options = {}
# Get all nodes ordered by ancestry and start sorting them into an empty hash
arrange_nodes scope.where(options)
end
# Arrange array of nodes into a nested hash of the form

# Arrange array of nodes into a nested hash of the form
# {node => children}, where children = {} if the node has no children
def arrange_nodes(nodes)
# Get all nodes ordered by ancestry and start sorting them into an empty hash
Expand All @@ -62,26 +62,26 @@ def arrange_serializable nodes = arrange
parent.serializable_hash.merge 'children' => arrange_serializable(children)
end
end
# Pseudo-preordered array of nodes. Children will always follow parents,

# Pseudo-preordered array of nodes. Children will always follow parents,
# for ordering nodes within a rank provide block, eg. Node.sort_by_ancestry(Node.all) {|a, b| a.rank <=> b.rank}.
def sort_by_ancestry(nodes, &block)
arranged = nodes if nodes.is_a?(Hash)

unless arranged
presorted_nodes = nodes.sort do |a, b|
a_cestry, b_cestry = a.ancestry || '0', b.ancestry || '0'

if block_given? && a_cestry == b_cestry
yield a, b
else
a_cestry <=> b_cestry
end
end

arranged = arrange_nodes(presorted_nodes)
end

arranged.inject([]) do |sorted_nodes, pair|
node, children = pair
sorted_nodes << node
Expand All @@ -94,8 +94,8 @@ def sort_by_ancestry(nodes, &block)
def check_ancestry_integrity! options = {}
parents = {}
exceptions = [] if options[:report] == :list
self.ancestry_base_class.unscoped do

self.ancestry_base_class.unscoped do
# For each node ...
self.ancestry_base_class.find_each do |node|
begin
Expand Down Expand Up @@ -133,7 +133,7 @@ def restore_ancestry_integrity!
parents = {}
# Wrap the whole thing in a transaction ...
self.ancestry_base_class.transaction do
self.ancestry_base_class.unscoped do
self.ancestry_base_class.unscoped do
# For each node ...
self.ancestry_base_class.find_each do |node|
# ... set its ancestry to nil if invalid
Expand All @@ -150,9 +150,9 @@ def restore_ancestry_integrity!
until parent.nil? || parent == node.id
parent = parents[parent]
end
parents[node.id] = nil if parent == node.id
parents[node.id] = nil if parent == node.id
end

# For each node ...
self.ancestry_base_class.find_each do |node|
# ... rebuild ancestry from parents array
Expand All @@ -167,10 +167,10 @@ def restore_ancestry_integrity!
end
end
end

# Build ancestry from parent id's for migration purposes
def build_ancestry_from_parent_ids! parent_id = nil, ancestry = nil
self.ancestry_base_class.unscoped do
self.ancestry_base_class.unscoped do
self.ancestry_base_class.where(:parent_id => parent_id).find_each do |node|
node.without_ancestry_callbacks do
node.update_attribute ancestry_column, ancestry
Expand All @@ -179,12 +179,12 @@ def build_ancestry_from_parent_ids! parent_id = nil, ancestry = nil
end
end
end

# Rebuild depth cache if it got corrupted or if depth caching was just turned on
def rebuild_depth_cache!
raise Ancestry::AncestryException.new("Cannot rebuild depth cache for model without depth caching.") unless respond_to? :depth_cache_column
self.ancestry_base_class.unscoped do

self.ancestry_base_class.unscoped do
self.ancestry_base_class.find_each do |node|
node.update_attribute depth_cache_column, node.depth
end
Expand Down
6 changes: 3 additions & 3 deletions test/has_ancestry_test.rb
Expand Up @@ -742,7 +742,7 @@ def test_sort_by_ancestry

def test_node_excluded_by_default_scope_should_still_move_with_parent
AncestryTestDatabase.with_model(
:width => 3, :depth => 3, :extra_columns => {:deleted_at => :datetime},
:width => 3, :depth => 3, :extra_columns => {:deleted_at => :datetime},
:default_scope_params => {:deleted_at => nil}
) do |model, roots|
roots = model.roots.to_a
Expand All @@ -761,7 +761,7 @@ def test_node_excluded_by_default_scope_should_still_move_with_parent

def test_node_excluded_by_default_scope_should_be_destroyed_with_parent
AncestryTestDatabase.with_model(
:width => 1, :depth => 2, :extra_columns => {:deleted_at => :datetime},
:width => 1, :depth => 2, :extra_columns => {:deleted_at => :datetime},
:default_scope_params => {:deleted_at => nil},
:orphan_strategy => :destroy
) do |model, roots|
Expand All @@ -778,7 +778,7 @@ def test_node_excluded_by_default_scope_should_be_destroyed_with_parent

def test_node_excluded_by_default_scope_should_be_rootified
AncestryTestDatabase.with_model(
:width => 1, :depth => 2, :extra_columns => {:deleted_at => :datetime},
:width => 1, :depth => 2, :extra_columns => {:deleted_at => :datetime},
:default_scope_params => {:deleted_at => nil},
:orphan_strategy => :rootify
) do |model, roots|
Expand Down

0 comments on commit ad2a8c5

Please sign in to comment.