mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Consider evaluating association in serializer context
For discussion: Consider evaluating association in serializer context That way, associations are really just anything that can be conditionally included. They no longer have to actually be methods on the object or serializer. e.g. ```diff has_many :comments do - last(1) + Comment.active.for_serialization(object).last(1) end ```
This commit is contained in:
@@ -8,14 +8,14 @@ module ActiveModel
|
||||
# has_one :author, serializer: AuthorSerializer
|
||||
# has_many :comments
|
||||
# has_many :comments, key: :last_comments do
|
||||
# last(1)
|
||||
# object.comments.last(1)
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# Notice that the association block is evaluated in the context of the association.
|
||||
# Notice that the association block is evaluated in the context of the serializer.
|
||||
# Specifically, the association 'comments' is evaluated two different ways:
|
||||
# 1) as 'comments' and named 'comments'.
|
||||
# 2) as 'comments.last(1)' and named 'last_comments'.
|
||||
# 2) as 'object.comments.last(1)' and named 'last_comments'.
|
||||
#
|
||||
# PostSerializer._reflections #=>
|
||||
# # [
|
||||
@@ -29,7 +29,7 @@ module ActiveModel
|
||||
# @api private
|
||||
def value(instance)
|
||||
if block
|
||||
instance.read_attribute_for_serialization(name).instance_eval(&block)
|
||||
instance.instance_eval(&block)
|
||||
else
|
||||
instance.read_attribute_for_serialization(name)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user