2011-10-30 23:02:41 +00:00
|
|
|
module Locomotive
|
|
|
|
module Extensions
|
|
|
|
module Page
|
|
|
|
module Tree
|
|
|
|
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2012-02-01 01:01:42 +00:00
|
|
|
include ::Mongoid::Tree
|
|
|
|
include ::Mongoid::Tree::Ordering
|
2011-10-30 23:02:41 +00:00
|
|
|
|
2012-02-16 21:07:00 +00:00
|
|
|
## fields ##
|
|
|
|
field :depth, :type => Integer, :default => 0
|
|
|
|
|
|
|
|
## callbacks ##
|
|
|
|
before_save :persist_depth
|
|
|
|
before_destroy :delete_descendants
|
|
|
|
|
2011-10-30 23:02:41 +00:00
|
|
|
## indexes ##
|
|
|
|
index :position
|
2012-03-13 15:37:19 +00:00
|
|
|
index [[:depth, Mongo::ASCENDING], [:position, Mongo::ASCENDING]]
|
2011-10-30 23:02:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
|
2012-02-01 01:01:42 +00:00
|
|
|
# Returns the pages tree from the site with the most minimal amount of queries.
|
|
|
|
# This method should only be used for read-only purpose since
|
|
|
|
# the mongodb returns the minimal set of required attributes to build
|
|
|
|
# the tree.
|
|
|
|
#
|
|
|
|
# @param [ Locomotive::Site ] site The site owning the pages
|
|
|
|
#
|
|
|
|
# @return [ Array ] The first array of pages (depth = 0)
|
|
|
|
#
|
2011-10-30 23:02:41 +00:00
|
|
|
def quick_tree(site, minimal_attributes = true)
|
2012-02-21 00:45:45 +00:00
|
|
|
pages = (minimal_attributes ? site.pages.unscoped.minimal_attributes : site.pages.unscoped).order_by([:depth.asc, :position.asc]).to_a
|
2011-10-30 23:02:41 +00:00
|
|
|
|
|
|
|
tmp = []
|
|
|
|
|
|
|
|
while !pages.empty?
|
|
|
|
tmp << _quick_tree(pages.delete_at(0), pages)
|
|
|
|
end
|
|
|
|
|
|
|
|
tmp
|
|
|
|
end
|
|
|
|
|
2012-02-01 01:01:42 +00:00
|
|
|
#:nodoc:
|
2011-10-30 23:02:41 +00:00
|
|
|
def _quick_tree(current_page, pages)
|
|
|
|
i, children = 0, []
|
|
|
|
|
|
|
|
while !pages.empty?
|
|
|
|
page = pages[i]
|
|
|
|
|
|
|
|
break if page.nil?
|
|
|
|
|
|
|
|
if page.parent_id == current_page.id
|
|
|
|
page = pages.delete_at(i)
|
|
|
|
|
|
|
|
children << _quick_tree(page, pages)
|
|
|
|
else
|
|
|
|
i += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
current_page.instance_eval do
|
|
|
|
def children=(list); @children = list; end
|
|
|
|
def children; @children || []; end
|
|
|
|
end
|
|
|
|
|
|
|
|
current_page.children = children
|
|
|
|
|
|
|
|
current_page
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-02-01 01:01:42 +00:00
|
|
|
# Returns the children of this node but with the minimal set of required attributes
|
|
|
|
#
|
|
|
|
# @return [ Array ] The children pages ordered by their position
|
|
|
|
#
|
2012-03-06 21:55:45 +00:00
|
|
|
def children_with_minimal_attributes( attrs = [] )
|
|
|
|
self.children.minimal_attributes( attrs )
|
2012-02-01 01:01:42 +00:00
|
|
|
end
|
2011-10-30 23:02:41 +00:00
|
|
|
|
2012-02-01 01:01:42 +00:00
|
|
|
# Assigns the new position of each child of this node.
|
|
|
|
#
|
|
|
|
# @param [ Array ] ids The ordered list of page ids (string)
|
|
|
|
#
|
|
|
|
def sort_children!(ids)
|
|
|
|
cached_children = self.children.to_a
|
|
|
|
ids.each_with_index do |id, position|
|
|
|
|
child = cached_children.detect { |p| p._id == BSON::ObjectId(id) }
|
|
|
|
child.position = position
|
|
|
|
child.save
|
2011-10-30 23:02:41 +00:00
|
|
|
end
|
|
|
|
end
|
2012-02-01 01:01:42 +00:00
|
|
|
|
2012-02-16 21:07:00 +00:00
|
|
|
def depth
|
|
|
|
self.parent_ids.count
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def persist_depth
|
2012-02-20 16:21:32 +00:00
|
|
|
self.attributes['depth'] = self.depth
|
|
|
|
self.depth_will_change!
|
2012-02-16 21:07:00 +00:00
|
|
|
end
|
|
|
|
|
2011-10-30 23:02:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-03-06 21:55:45 +00:00
|
|
|
end
|