File tree Expand file tree Collapse file tree 4 files changed +12
-5
lines changed
lib/active_model/serializer Expand file tree Collapse file tree 4 files changed +12
-5
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,11 @@ Breaking changes:
16
16
17
17
Features:
18
18
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 `
19
24
- [ #1356 ] ( https://github.com/rails-api/active_model_serializers/pull/1356 ) Add inline syntax for
20
25
attributes and associations (@bf4 @beauby @noahsilas )
21
26
* Allows defining attributes so that they don't conflict with existing methods. e.g. `attribute
Original file line number Diff line number Diff line change @@ -8,14 +8,14 @@ class Serializer
8
8
# has_one :author, serializer: AuthorSerializer
9
9
# has_many :comments
10
10
# has_many :comments, key: :last_comments do
11
- # last(1)
11
+ # object.comments. last(1)
12
12
# end
13
13
# end
14
14
#
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 .
16
16
# Specifically, the association 'comments' is evaluated two different ways:
17
17
# 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'.
19
19
#
20
20
# PostSerializer._reflections #=>
21
21
# # [
@@ -29,7 +29,7 @@ class Serializer
29
29
# @api private
30
30
def value ( instance )
31
31
if block
32
- instance . read_attribute_for_serialization ( name ) . instance_eval ( &block )
32
+ instance . instance_eval ( &block )
33
33
else
34
34
instance . read_attribute_for_serialization ( name )
35
35
end
Original file line number Diff line number Diff line change @@ -33,6 +33,7 @@ class Profile < Model
33
33
class ProfileSerializer < ActiveModel ::Serializer
34
34
attributes :name , :description
35
35
36
+ # TODO: is this used anywhere?
36
37
def arguments_passed_in?
37
38
instance_options [ :my_options ] == :accessible
38
39
end
@@ -75,6 +76,7 @@ def blog
75
76
Blog . new ( id : 999 , name : 'Custom blog' )
76
77
end
77
78
79
+ # TODO: is this used anywhere?
78
80
def custom_options
79
81
instance_options
80
82
end
Original file line number Diff line number Diff line change @@ -129,7 +129,7 @@ def test_associations_custom_keys
129
129
class InlineAssociationTestPostSerializer < ActiveModel ::Serializer
130
130
has_many :comments
131
131
has_many :comments , key : :last_comments do
132
- last ( 1 )
132
+ object . comments . last ( 1 )
133
133
end
134
134
end
135
135
You can’t perform that action at this time.
0 commit comments