Sha256: 708ed69eb740ff3753103100a2ef0c0a8afa9b0b31cf21099a943d4184b7a97c

Contents?: true

Size: 1.76 KB

Versions: 1

Compression:

Stored size: 1.76 KB

Contents

module Parentry
  module Navigation
    def root_id
      path_ids[0]
    end

    def root
      unscoped_find(root_id)
    end

    def root?
      parent_id.nil?
    end

    def path_ids
      parse_parentry
    end

    def path(scopes = {})
      with_depth_scopes(scopes) do
        parentry_scope.where(id: path_ids).order_by_parentry
      end
    end

    def ancestor_ids
      path_ids[0..-2]
    end

    def ancestors(scopes = {})
      with_depth_scopes(scopes) do
        parentry_scope.where(id: ancestor_ids).order_by_parentry
      end
    end

    def leaf?
      children.size == 0
    end
    alias_method :childless?, :leaf?

    def children?
      !leaf?
    end

    def sibling_ids
      siblings.pluck(:id)
    end

    def siblings
      parentry_scope.where.not(id: id).where.not(parent_id: nil).where(parent_id: parent_id)
    end

    def siblings?
      siblings.size > 0
    end

    def only_child?
      !siblings?
    end

    def subtree_ids(scopes = {})
      subtree(scopes).pluck(:id)
    end

    def subtree(scopes = {})
      with_depth_scopes(scopes) do
        parentry_scope.where("#{parentry_column} <@ ?", parentry)
      end
    end

    def descendant_ids(scopes = {})
      descendants(scopes).pluck(:id)
    end

    def descendants(scopes = {})
      with_depth_scopes(scopes) do
        subtree.where.not(id: id)
      end
    end

    def depth
      return depth_offset if root?
      return parent.depth + 1 if changes[:parent_id].present?
      path_ids.size - 1 + depth_offset
    end

    private

    def unscoped_find(id)
      parentry_scope.unscoped { parentry_scope.find(id) }
    end

    def with_depth_scopes(scopes)
      scopes.reduce(yield) do |memo, (scope, depth)|
        memo.send(scope, depth)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
parentry-0.4.0 lib/parentry/navigation.rb