Skip to content

Commit 7d4f0c5

Browse files
committed
Merge branch 'bf4-consider_association_blocks'
2 parents 41ae5f7 + d7de53c commit 7d4f0c5

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ Breaking changes:
1616

1717
Features:
1818

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`
1924
- [#1356](https://github.com/rails-api/active_model_serializers/pull/1356) Add inline syntax for
2025
attributes and associations (@bf4 @beauby @noahsilas)
2126
* Allows defining attributes so that they don't conflict with existing methods. e.g. `attribute

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)