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 dbcfd93
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 42 deletions.
15 changes: 1 addition & 14 deletions app/controllers/alchemy/admin/pages_controller.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
39 changes: 39 additions & 0 deletions app/models/alchemy/eager_loading.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

module Alchemy
# Eager loading parameters for loading pages
class EagerLoading
PAGE_VERSIONS = %i[draft_version public_version]

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)
raise UnsupportedPageVersion unless version.in? PAGE_VERSIONS

[
:tags,
{
language: :site,
version => {
elements: [
:page,
:touchable_pages,
{
ingredients: :related_object,
contents: :essence,
},
],
},
},
]
end
end
end
end
10 changes: 8 additions & 2 deletions lib/alchemy/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ class DefaultLanguageNotFoundError < StandardError
# Raised if no default language configuration can be found.
def message
"No default language configuration found!" \
" Please ensure that you have a 'default_language' defined in Alchemy configuration file."
" Please ensure that you have a 'default_language' defined in Alchemy configuration file."
end
end

class DefaultSiteNotFoundError < StandardError
# Raised if no default site configuration can be found.
def message
"No default site configuration found!" \
" Please ensure that you have a 'default_site' defined in Alchemy configuration file."
" Please ensure that you have a 'default_site' defined in Alchemy configuration file."
end
end

Expand Down Expand Up @@ -90,4 +90,10 @@ def message
"You need to provide a current_user method in your ApplicationController that returns the current authenticated user."
end
end

class UnsupportedPageVersion < StandardError
def message
"Unknown Version! Please use one of #{Alchemy::EagerLoading::PAGE_VERSIONS.join(", ")}"
end
end
end
61 changes: 61 additions & 0 deletions spec/models/alchemy/eager_loading_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Alchemy::EagerLoading do
describe ".page_includes" do
context "with no version param given" do
subject { described_class.page_includes }

it "returns public version includes" do
is_expected.to match_array([
:tags,
{
language: :site,
public_version: {
elements: [
:page,
:touchable_pages,
{
ingredients: :related_object,
contents: :essence,
},
],
},
},
])
end
end

context "with version param given" do
subject { described_class.page_includes(version: :draft_version) }

it "returns version includes" do
is_expected.to match_array([
:tags,
{
language: :site,
draft_version: {
elements: [
:page,
:touchable_pages,
{
ingredients: :related_object,
contents: :essence,
},
],
},
},
])
end
end

context "with unknown version param given" do
subject { described_class.page_includes(version: :foo_baz) }

it "returns version includes" do
expect { subject }.to raise_error(Alchemy::UnsupportedPageVersion)
end
end
end
end

0 comments on commit dbcfd93

Please sign in to comment.