diff --git a/app/controllers/alchemy/admin/pages_controller.rb b/app/controllers/alchemy/admin/pages_controller.rb index 31c7749eba..c74be87dc3 100644 --- a/app/controllers/alchemy/admin/pages_controller.rb +++ b/app/controllers/alchemy/admin/pages_controller.rb @@ -404,20 +404,7 @@ def set_preview_mode end def page_includes - { - :tags, - language: :site, - draft_version: { - elements: [ - :page, - :touchable_pages, - { - ingredients: :related_object, - contents: :essence, - }, - ], - }, - } + Alchemy::EagerLoading.page_includes(version: :draft_version) end end end diff --git a/app/controllers/alchemy/pages_controller.rb b/app/controllers/alchemy/pages_controller.rb index aee18b9179..d92f4e4ea8 100644 --- a/app/controllers/alchemy/pages_controller.rb +++ b/app/controllers/alchemy/pages_controller.rb @@ -246,20 +246,7 @@ def page_not_found! end def page_includes - { - :tags, - language: :site, - public_version: { - elements: [ - :page, - :touchable_pages, - { - ingredients: :related_object, - contents: :essence, - }, - ], - }, - } + Alchemy::EagerLoading.page_includes(version: :public_version) end end end diff --git a/app/jobs/alchemy/publish_page_job.rb b/app/jobs/alchemy/publish_page_job.rb index c257a6a899..d06f5d8284 100644 --- a/app/jobs/alchemy/publish_page_job.rb +++ b/app/jobs/alchemy/publish_page_job.rb @@ -6,18 +6,7 @@ class PublishPageJob < BaseJob def perform(page_id, public_on:) page = Alchemy::Page.includes( - :tags, - language: :site, - draft_version: { - elements: [ - :page, - :touchable_pages, - { - ingredients: :related_object, - contents: :essence, - }, - ], - }, + Alchemy::EagerLoading.page_includes(version: :draft_version) ).find(page_id) Alchemy::Page::Publisher.new(page).publish!(public_on: public_on) end diff --git a/app/models/alchemy/eager_loading.rb b/app/models/alchemy/eager_loading.rb new file mode 100644 index 0000000000..3e63346f0c --- /dev/null +++ b/app/models/alchemy/eager_loading.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +module Alchemy + # Eager loading parameters for loading pages + class EagerLoading + class << self + # Eager loading parameters for {ActiveRecord::Base.includes} + # + # Pass this to +includes+ whereever you load an {Alchemy::Page} + # + # Alchemy::Page.includes(Alchemy::EagerLoading.page_includes).find_by(urlname: "my-page") + # + # + # @param version [Symbol] Type of page version to eager load + # @return [Array] + def page_includes(version: :public_version) + [ + :tags, + { + language: :site, + version => { + elements: [ + :page, + :touchable_pages, + { + ingredients: :related_object, + contents: :essence, + }, + ], + }, + }, + ] + end + end + end +end