Sha256: 77cb7fffbc35fffd9ec51092988f6c71107175f5d5ae64a96e838adc469e26f6

Contents?: true

Size: 1.78 KB

Versions: 3

Compression:

Stored size: 1.78 KB

Contents

# encoding: UTF-8

module Spontaneous::Model::Page
  module PageTree
    extend Spontaneous::Concern

    # InstanceMethods

    def ancestors
      @ancestors ||= ancestor_path.map { |id| content_model[id] }
    end

    def ancestor(depth)
      ancestors[depth]
    end

    # Returns a list of all the pages at a certain depth in the page tree for any page
    def at_depth(depth)
      return root_at_depth(depth) if is_root?
      parent_depth = [0, depth - 1].max
      parent = ancestor(parent_depth)
      unordered_pages = parent.children
      # This is made more complex because the #children method is unordered
      # whereas the actual page order must come from the boxes
      return unordered_pages if parent.boxes.empty?
      ordered_pages = []
      # Iterate through the list of boxes and pull out of the
      # unordered page list each page that belongs to the current box
      parent.boxes.each do |box|
        box_id = box.schema_id.to_s
        in_box, unordered_pages = unordered_pages.partition { |p| p.box_sid == box_id }
        ordered_pages.concat in_box.sort { |a, b| a.position <=> b.position }
      end
      ordered_pages
    end

    def root_at_depth(depth)
      case depth
      when 0
        self
      when 1
        self.children
      else
        raise ArgumentError.new("Cannot calculate descendents of root beyond a depth of 1")
      end
    end

    def ancestor?(page)
      ancestor_path.include?(page.id)
    end

    def active?(page)
      page.id == self.id or ancestor?(page)
    end

    def generation
      parent ? parent.children : [root].compact
    end

    def siblings
      generation.reject { |p| p.id == id }
    end

    def >(page)
      page.ancestors.include?(self)
    end

    def >=(page)
      page == self or self.>(page)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spontaneous-0.2.0.beta4 lib/spontaneous/model/page/page_tree.rb
spontaneous-0.2.0.beta3 lib/spontaneous/model/page/page_tree.rb
spontaneous-0.2.0.beta2 lib/spontaneous/model/page/page_tree.rb