|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Alchemy |
| 4 | + # Preloads page trees with all associations and children |
| 5 | + # |
| 6 | + # This service efficiently loads page trees to avoid N+1 queries. |
| 7 | + # It handles folded pages and preloads all necessary associations. |
| 8 | + # |
| 9 | + # @example Preload all pages for a language |
| 10 | + # preloader = Alchemy::PageTreePreloader.new(language: language, user: current_user) |
| 11 | + # root_pages = preloader.call |
| 12 | + # |
| 13 | + # @example Preload subtree from a specific page |
| 14 | + # preloader = Alchemy::PageTreePreloader.new(from: page, user: current_user) |
| 15 | + # page_with_descendants = preloader.call |
| 16 | + # |
| 17 | + class PageTreePreloader |
| 18 | + # @param from [Page, nil] Starting page for loading descendants |
| 19 | + # @param language [Language, nil] Language to load pages for (loads all language's contentpages) |
| 20 | + # @param user [User, nil] User for folding support |
| 21 | + def initialize(from: nil, language: nil, user: nil) |
| 22 | + @from = from |
| 23 | + @language = language |
| 24 | + @user = user |
| 25 | + end |
| 26 | + |
| 27 | + # Preloads and returns the page tree |
| 28 | + # |
| 29 | + # @return [Array<Page>] Root pages with preloaded children, or array with single page when using from: |
| 30 | + def call |
| 31 | + pages = load_pages |
| 32 | + return pages if pages.empty? |
| 33 | + |
| 34 | + folded_page_ids = load_folded_page_ids |
| 35 | + preload_children_associations(pages, folded_page_ids: folded_page_ids) |
| 36 | + |
| 37 | + return_result(pages) |
| 38 | + end |
| 39 | + |
| 40 | + private |
| 41 | + |
| 42 | + attr_reader :from, :language, :user |
| 43 | + |
| 44 | + # Load the initial page collection |
| 45 | + def load_pages |
| 46 | + if from |
| 47 | + # Load subtree from specific page |
| 48 | + from.self_and_descendants.preload(*preload_associations, :folded_pages).to_a |
| 49 | + elsif language |
| 50 | + # Load all contentpages for language |
| 51 | + Page.with_language(language.id).contentpages.preload(*preload_associations, :folded_pages).to_a |
| 52 | + else |
| 53 | + Rails.logger.warn("Neither a start page not language given! Skipping preloading pages.") |
| 54 | + [] |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + # Load folded page IDs for the user |
| 59 | + def load_folded_page_ids |
| 60 | + if user && Alchemy.user_class < ActiveRecord::Base |
| 61 | + FoldedPage.folded_for_user(user).pluck(:page_id).to_set |
| 62 | + else |
| 63 | + Set.new |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + # Preload children associations for a collection of pages |
| 68 | + # This manually populates the children association to prevent N+1 queries |
| 69 | + # |
| 70 | + # @param pages [Array<Page>] The pages to preload children for |
| 71 | + # @param folded_page_ids [Set] Optional set of page IDs that should have empty children |
| 72 | + # @return [void] |
| 73 | + def preload_children_associations(pages, folded_page_ids: Set.new) |
| 74 | + # Group pages by parent_id for efficient lookup |
| 75 | + pages_by_parent = pages.group_by(&:parent_id) |
| 76 | + |
| 77 | + # Manually populate the children association for each page |
| 78 | + pages.each do |page| |
| 79 | + children_records = pages_by_parent[page.id] || [] |
| 80 | + |
| 81 | + # If page is folded, set children to empty array |
| 82 | + page.association(:children).target = if folded_page_ids.include?(page.id) |
| 83 | + [] |
| 84 | + else |
| 85 | + children_records.sort_by(&:lft) |
| 86 | + end |
| 87 | + page.association(:children).loaded! |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + # Return appropriate result based on input |
| 92 | + def return_result(pages) |
| 93 | + if from |
| 94 | + # Return the starting page in an array (which now has preloaded descendants) |
| 95 | + # We need to return the actual instance from the pages array, not the @from instance |
| 96 | + # because the children associations were set on the pages array instances |
| 97 | + starting_page = pages.find { |p| p.id == from.id } || from |
| 98 | + [starting_page] |
| 99 | + else |
| 100 | + # Return only root pages - their children are now preloaded |
| 101 | + pages.group_by(&:parent_id)[nil] || [] |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + # Associations to preload for sitemap rendering |
| 106 | + def preload_associations |
| 107 | + Page.sitemap_preload_associations |
| 108 | + end |
| 109 | + end |
| 110 | +end |
0 commit comments