Skip to content

Commit d7de53c

Browse files
committed
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 ```
1 parent 12cd190 commit d7de53c

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

CHANGELOG.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,22 @@ Breaking changes:
1616

1717
Features:
1818

19-
- [#1336](https://github.com/rails-api/active_model_serializers/pull/1336) Added support for Grape >= 0.13, < 1.0
19+
- [#1378](https://github.com/rails-api/active_model_serializers/pull/1378) Change association blocks
20+
to be evaluated in *serializer* scope, rather than *association* scope. (@bf4)
21+
* Syntax changes from e.g.
22+
`has_many :titles do customers.pluck(:title) end` (in #1356) to
23+
`has_many :titles do object.customers.pluck(:title) end`
24+
- [#1356](https://github.com/rails-api/active_model_serializers/pull/1356) Add inline syntax for
25+
attributes and associations (@bf4 @beauby @noahsilas)
26+
* Allows defining attributes so that they don't conflict with existing methods. e.g. `attribute
27+
:title do 'Mr. Topum Hat' end`
28+
* Allows defining associations so that they don't conflict with existing methods. e.g. `has_many
29+
:titles do customers.pluck(:title) end`
30+
* Allows dynamic associations, as compared to compare to using
31+
[`virtual_value`](https://github.com/rails-api/active_model_serializers/pull/1356#discussion_r47146466).
32+
e.g. `has_many :reviews, virtual_value: [{ id: 1 }, { id: 2 }]`
33+
* Removes dynamically defined methods on the serializer
34+
- [#1336](https://github.com/rails-api/active_model_serializers/pull/1336) Added support for Grape >= 0.13, < 1.0 (@johnhamelink)
2035
- [#1291](https://github.com/rails-api/active_model_serializers/pull/1291) Add logging (@maurogeorge)
2136
- [#1225](https://github.com/rails-api/active_model_serializers/pull/1225) Better serializer lookup, use nested serializer when it exists (@beauby)
2237
- [#1172](https://github.com/rails-api/active_model_serializers/pull/1172) Better serializer registration, get more than just the first module (@bf4)

lib/active_model/serializer/reflection.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ class Serializer
88
# has_one :author, serializer: AuthorSerializer
99
# has_many :comments
1010
# has_many :comments, key: :last_comments do
11-
# last(1)
11+
# object.comments.last(1)
1212
# end
1313
# end
1414
#
15-
# Notice that the association block is evaluated in the context of the association.
15+
# Notice that the association block is evaluated in the context of the serializer.
1616
# Specifically, the association 'comments' is evaluated two different ways:
1717
# 1) as 'comments' and named 'comments'.
18-
# 2) as 'comments.last(1)' and named 'last_comments'.
18+
# 2) as 'object.comments.last(1)' and named 'last_comments'.
1919
#
2020
# PostSerializer._reflections #=>
2121
# # [
@@ -29,7 +29,7 @@ class Serializer
2929
# @api private
3030
def value(instance)
3131
if block
32-
instance.read_attribute_for_serialization(name).instance_eval(&block)
32+
instance.instance_eval(&block)
3333
else
3434
instance.read_attribute_for_serialization(name)
3535
end

test/fixtures/poro.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Profile < Model
3333
class ProfileSerializer < ActiveModel::Serializer
3434
attributes :name, :description
3535

36+
# TODO: is this used anywhere?
3637
def arguments_passed_in?
3738
instance_options[:my_options] == :accessible
3839
end
@@ -75,6 +76,7 @@ def blog
7576
Blog.new(id: 999, name: 'Custom blog')
7677
end
7778

79+
# TODO: is this used anywhere?
7880
def custom_options
7981
instance_options
8082
end

test/serializers/associations_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def test_associations_custom_keys
129129
class InlineAssociationTestPostSerializer < ActiveModel::Serializer
130130
has_many :comments
131131
has_many :comments, key: :last_comments do
132-
last(1)
132+
object.comments.last(1)
133133
end
134134
end
135135

0 commit comments

Comments
 (0)