forked from AlchemyCMS/alchemy_cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request AlchemyCMS#2313 from mamhoff/do-not-eagerload-in-p…
…age-version Eagerload at the controller or job layer
- Loading branch information
Showing
10 changed files
with
156 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters