-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement custom preloads - backport to 4-x-stable #132
Closed
apauly
wants to merge
12
commits into
salsify:4-x-stable
from
apauly:feature/custom-preloads-backport
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1d8d709
Implement custom preloads (#129)
apauly ba502f8
Update circleci config
fccb83a
Install sqlite for Ruby 2.6.10
d84821d
no sudo
fe12400
Ruby 2.6 requires older sqlite3
4cfa504
Add the actual older version
a2cf258
Fix sqlite3 version for ruby 2.6
738d9d6
Rubocop ++ do not use locate_many with older ruby version
28e4f77
Rubocop
29b6761
Use ruby 2.6.10 image
13f5df3
Appraisals
ed537e1
WIP
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,42 @@ | ||
# frozen_string_literal: true | ||
|
||
module Goldiloader | ||
module CustomPreloads | ||
def initialize | ||
super | ||
@custom_preloads = nil | ||
end | ||
|
||
def preloaded(model, cache_name:, key:, &block) | ||
unless preloaded?(cache_name) | ||
ids = models.map do |record| | ||
record.public_send(key) | ||
end | ||
|
||
# We're using instance_exec instead of a simple yield to make sure that the | ||
# given block does not have any references to the model instance as this might | ||
# lead to unexpected results. The block will be executed in the context of the | ||
# class of the model instead. | ||
block_context = models.first.class | ||
preloaded_hash = block_context.instance_exec(ids, &block) | ||
store_preloaded(cache_name, preloaded_hash) | ||
end | ||
fetch_preloaded(cache_name, model, key: key) | ||
end | ||
|
||
private | ||
|
||
def store_preloaded(cache_name, preloaded_hash) | ||
@custom_preloads ||= {} | ||
@custom_preloads[cache_name] = preloaded_hash | ||
end | ||
|
||
def fetch_preloaded(cache_name, instance, key:) | ||
@custom_preloads&.dig(cache_name, instance.public_send(key)) | ||
end | ||
|
||
def preloaded?(cache_name) | ||
@custom_preloads&.key?(cache_name) | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this install of SQLite be removed if we're pinning the gem to a corresponding older version?