Sha256: 021ef654ccd2c5e65300a5d85f4183f73793f339e397dac83ee4de05f36fe422

Contents?: true

Size: 1.78 KB

Versions: 7

Compression:

Stored size: 1.78 KB

Contents

# encoding: UTF-8

module Spontaneous::Plugins
  module PageTree
    extend ActiveSupport::Concern

    # InstanceMethods

    def ancestors
      @ancestors ||= ancestor_path.map { |id| Spontaneous::Content[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]
    end

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

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

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
spontaneous-0.2.0.alpha7 lib/spontaneous/plugins/page_tree.rb
spontaneous-0.2.0.alpha6 lib/spontaneous/plugins/page_tree.rb
spontaneous-0.2.0.alpha5 lib/spontaneous/plugins/page_tree.rb
spontaneous-0.2.0.alpha4 lib/spontaneous/plugins/page_tree.rb
spontaneous-0.2.0.alpha3 lib/spontaneous/plugins/page_tree.rb
spontaneous-0.2.0.alpha2 lib/spontaneous/plugins/page_tree.rb
spontaneous-0.2.0.alpha1 lib/spontaneous/plugins/page_tree.rb