Skip to content

Commit

Permalink
Extract common eager loading parameters into Class
Browse files Browse the repository at this point in the history
This helps keep the duplication down.
  • Loading branch information
mamhoff authored and tvdeyen committed Apr 27, 2022
1 parent 5b61e8b commit 7e5268c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 40 deletions.
15 changes: 1 addition & 14 deletions app/controllers/alchemy/admin/pages_controller.rb
Expand Up @@ -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
Expand Down
15 changes: 1 addition & 14 deletions app/controllers/alchemy/pages_controller.rb
Expand Up @@ -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
13 changes: 1 addition & 12 deletions app/jobs/alchemy/publish_page_job.rb
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions app/models/alchemy/eager_loading.rb
@@ -0,0 +1,35 @@
# 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

0 comments on commit 7e5268c

Please sign in to comment.