Sha256: 818bc53b83fa6af90fdadb4d42f2c054f979fa7d9fc2666bd1876f36c5c7e27c

Contents?: true

Size: 1.11 KB

Versions: 5

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

module ActsAsRecursiveTree
  module Preloaders
    #
    # Preloads all descendants records for a given node and sets the parent and child associations on each record
    # based on the preloaded data. After this, calling #parent or #children will not trigger a database query.
    #
    class Descendants
      def initialize(node, includes: nil)
        @node       = node
        @parent_key = node._recursive_tree_config.parent_key
        @includes   = includes
      end

      def preload!
        apply_records(@node)
      end

      private

      def records
        @records ||= begin
          descendants = @node.descendants
          descendants = descendants.includes(*@includes) if @includes
          descendants.to_a
        end
      end

      def apply_records(parent_node)
        children = records.select { |child| child.send(@parent_key) == parent_node.id }

        parent_node.association(:children).target = children

        children.each do |child|
          child.association(:parent).target = parent_node
          apply_records(child)
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
acts_as_recursive_tree-4.1.0 lib/acts_as_recursive_tree/preloaders/descendants.rb
acts_as_recursive_tree-4.0.0 lib/acts_as_recursive_tree/preloaders/descendants.rb
acts_as_recursive_tree-3.5.0 lib/acts_as_recursive_tree/preloaders/descendants.rb
acts_as_recursive_tree-3.4.0 lib/acts_as_recursive_tree/preloaders/descendants.rb
acts_as_recursive_tree-3.3.0 lib/acts_as_recursive_tree/preloaders/descendants.rb