Sha256: b710b5c308ad3675cf96ee17e34a50a773b58557f3a4a7291b65156c5a9a2931
Contents?: true
Size: 1.74 KB
Versions: 6
Compression:
Stored size: 1.74 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(subtree_conditions) 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
6 entries across 6 versions & 1 rubygems