-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
lazy_dig
as serializer instance method
- Loading branch information
1 parent
e19c5cd
commit 42f30bd
Showing
6 changed files
with
255 additions
and
19 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,62 @@ | ||
# frozen_string_literal: true | ||
|
||
module AmsLazyRelationships::Core | ||
module LazyDigMethod | ||
def lazy_dig(*relation_names) | ||
relationships = { | ||
multiple: false, | ||
data: [{ | ||
serializer: self.class, | ||
object: object | ||
}] | ||
} | ||
|
||
relation_names.each do |relation_name| | ||
lazy_dig_relationship!(relation_name, relationships) | ||
end | ||
|
||
objects = relationships[:data].map { |r| r[:object] } | ||
|
||
relationships[:multiple] ? objects : objects.first | ||
end | ||
|
||
private | ||
|
||
def lazy_dig_relationship!(relation_name, relationships) | ||
relationships[:data].map! do |serializer:, object:| | ||
next_objects = lazy_dig_next_objects!(relation_name, serializer, object) | ||
next unless next_objects | ||
|
||
relationships[:multiple] ||= next_objects.respond_to?(:to_ary) | ||
|
||
lazy_dig_next_relationships!(relation_name, serializer, next_objects) | ||
end | ||
|
||
relationships[:data].flatten! | ||
relationships[:data].compact! | ||
end | ||
|
||
def lazy_dig_next_objects!(relation_name, serializer, object) | ||
serializer&.send( | ||
:load_lazy_relationship, | ||
relation_name, | ||
object | ||
) | ||
end | ||
|
||
def lazy_dig_next_relationships!(relation_name, serializer, next_objects) | ||
Array.wrap(next_objects).map do |next_object| | ||
next_serializer = serializer.send( | ||
:lazy_serializer_for, | ||
next_object, | ||
relation_name: relation_name | ||
) | ||
|
||
{ | ||
serializer: next_serializer, | ||
object: next_object | ||
} | ||
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