Skip to content

Commit

Permalink
Consider evaluating association in serializer context
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
bf4 committed Dec 23, 2015
1 parent b6a4ad1 commit c9c7666
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/general/serializers.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ has_many :comments, key: :reviews
has_many :comments, serializer: CommentPreviewSerializer
has_many :reviews, virtual_value: [{ id: 1 }, { id: 2 }]
has_many :comments, key: :last_comments do
last(1)
object.comments.last(1)
end
```

Expand Down
8 changes: 4 additions & 4 deletions lib/active_model/serializer/reflection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ class Serializer
# 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 #=>
# # [
Expand All @@ -29,7 +29,7 @@ class Serializer
# @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
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/poro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Profile < Model
class ProfileSerializer < ActiveModel::Serializer
attributes :name, :description

# TODO: is this used anywhere?
def arguments_passed_in?
instance_options[:my_options] == :accessible
end
Expand Down Expand Up @@ -75,6 +76,7 @@ def blog
Blog.new(id: 999, name: 'Custom blog')
end

# TODO: is this used anywhere?
def custom_options
instance_options
end
Expand Down
2 changes: 1 addition & 1 deletion test/serializers/associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def test_associations_custom_keys
class InlineAssociationTestPostSerializer < ActiveModel::Serializer
has_many :comments
has_many :comments, key: :last_comments do
last(1)
object.comments.last(1)
end
end

Expand Down

0 comments on commit c9c7666

Please sign in to comment.