mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Update comments regarding lazy_association and TODOs
This commit is contained in:
parent
ff5ab21a45
commit
5e01a93fc0
@ -193,6 +193,7 @@ module ActiveModel
|
||||
cache_keys << object_cache_key(serializer, adapter_instance)
|
||||
|
||||
serializer.associations(include_directive).each do |association|
|
||||
# TODO(BF): Process relationship without evaluating lazy_association
|
||||
association_serializer = association.lazy_association.serializer
|
||||
if association_serializer.respond_to?(:each)
|
||||
association_serializer.each do |sub_serializer|
|
||||
|
||||
@ -40,17 +40,6 @@ module ActiveModel
|
||||
cached_result[:virtual_value] || reflection_options[:virtual_value]
|
||||
end
|
||||
|
||||
# NOTE(BF): Kurko writes:
|
||||
# 1. This class is doing a lot more than it should. It has business logic (key/meta/links) and
|
||||
# it also looks like a factory (serializer/serialize_object/instantiate_serializer/serializer_class).
|
||||
# It's hard to maintain classes that you can understand what it's really meant to be doing,
|
||||
# so it ends up having all sorts of methods.
|
||||
# Perhaps we could replace all these methods with a class called... Serializer.
|
||||
# See how association is doing the job a serializer again?
|
||||
# 2. I've seen code like this in many other places.
|
||||
# Perhaps we should just have it all in one place: Serializer.
|
||||
# We already have a class called Serializer, I know,
|
||||
# and that is doing things that are not responsibility of a serializer.
|
||||
def serializer_class
|
||||
return @serializer_class if defined?(@serializer_class)
|
||||
serializer_for_options = { namespace: namespace }
|
||||
@ -82,8 +71,6 @@ module ActiveModel
|
||||
end
|
||||
end
|
||||
|
||||
# NOTE(BF): This serializer throw/catch should only happen when the serializer is a collection
|
||||
# serializer. This is a good reason for the reflection to have a to_many? type method.
|
||||
def instantiate_serializer(object)
|
||||
serializer_options = association_options.fetch(:parent_serializer_options).except(:serializer)
|
||||
serializer_options[:serializer_context_class] = association_options.fetch(:parent_serializer).class
|
||||
|
||||
@ -257,6 +257,7 @@ module ActiveModelSerializers
|
||||
|
||||
def process_relationships(serializer, include_slice)
|
||||
serializer.associations(include_slice).each do |association|
|
||||
# TODO(BF): Process relationship without evaluating lazy_association
|
||||
process_relationship(association.lazy_association.serializer, include_slice[association.key])
|
||||
end
|
||||
end
|
||||
|
||||
@ -33,6 +33,7 @@ module ActiveModelSerializers
|
||||
|
||||
private
|
||||
|
||||
# TODO(BF): Avoid db hit on belong_to_ releationship by using foreign_key on self
|
||||
def data_for(association)
|
||||
if association.collection?
|
||||
data_for_many(association)
|
||||
@ -42,6 +43,7 @@ module ActiveModelSerializers
|
||||
end
|
||||
|
||||
def data_for_one(association)
|
||||
# TODO(BF): Process relationship without evaluating lazy_association
|
||||
serializer = association.lazy_association.serializer
|
||||
if (virtual_value = association.virtual_value)
|
||||
virtual_value
|
||||
@ -53,6 +55,7 @@ module ActiveModelSerializers
|
||||
end
|
||||
|
||||
def data_for_many(association)
|
||||
# TODO(BF): Process relationship without evaluating lazy_association
|
||||
collection_serializer = association.lazy_association.serializer
|
||||
if collection_serializer.respond_to?(:each)
|
||||
collection_serializer.map do |serializer|
|
||||
|
||||
Loading…
Reference in New Issue
Block a user