mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Merge pull request #1633 from bf4/fix_reflection_block_context
Add serializer to association block context
This commit is contained in:
commit
27a5de262f
@ -3,6 +3,7 @@
|
|||||||
Breaking changes:
|
Breaking changes:
|
||||||
|
|
||||||
Features:
|
Features:
|
||||||
|
- [#1633](https://github.com/rails-api/active_model_serializers/pull/1633) Yield 'serializer' to serializer association blocks. (@bf4)
|
||||||
- [#1616](https://github.com/rails-api/active_model_serializers/pull/1616) SerializableResource handles no serializer like controller. (@bf4)
|
- [#1616](https://github.com/rails-api/active_model_serializers/pull/1616) SerializableResource handles no serializer like controller. (@bf4)
|
||||||
- [#1618](https://github.com/rails-api/active_model_serializers/issues/1618) Get collection root key for
|
- [#1618](https://github.com/rails-api/active_model_serializers/issues/1618) Get collection root key for
|
||||||
empty collection from explicit serializer option, when possible. (@bf4)
|
empty collection from explicit serializer option, when possible. (@bf4)
|
||||||
|
|||||||
@ -48,6 +48,18 @@ has_one :blog, key: :site
|
|||||||
has_one :maker, virtual_value: { id: 1 }
|
has_one :maker, virtual_value: { id: 1 }
|
||||||
```
|
```
|
||||||
|
|
||||||
|
``ruby
|
||||||
|
has_one :blog do |serializer|
|
||||||
|
serializer.cached_blog
|
||||||
|
end
|
||||||
|
|
||||||
|
def cached_blog
|
||||||
|
cache_store.fetch("cached_blog:#{object.updated_at}") do
|
||||||
|
Blog.find(object.blog_id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
#### ::has_many
|
#### ::has_many
|
||||||
|
|
||||||
e.g.
|
e.g.
|
||||||
|
|||||||
@ -56,12 +56,25 @@ module ActiveModel
|
|||||||
:nil
|
:nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @param serializer [ActiveModel::Serializer]
|
||||||
|
# @yield [ActiveModel::Serializer]
|
||||||
|
# @return [:nil, associated resource or resource collection]
|
||||||
|
# @example
|
||||||
|
# has_one :blog do |serializer|
|
||||||
|
# serializer.cached_blog
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# def cached_blog
|
||||||
|
# cache_store.fetch("cached_blog:#{object.updated_at}") do
|
||||||
|
# Blog.find(object.blog_id)
|
||||||
|
# end
|
||||||
|
# end
|
||||||
def value(serializer)
|
def value(serializer)
|
||||||
@object = serializer.object
|
@object = serializer.object
|
||||||
@scope = serializer.scope
|
@scope = serializer.scope
|
||||||
|
|
||||||
if block
|
if block
|
||||||
block_value = instance_eval(&block)
|
block_value = instance_exec(serializer, &block)
|
||||||
if block_value == :nil
|
if block_value == :nil
|
||||||
serializer.read_attribute_for_serialization(name)
|
serializer.read_attribute_for_serialization(name)
|
||||||
else
|
else
|
||||||
|
|||||||
@ -38,8 +38,9 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
has_many :roles do
|
has_many :roles do |serializer|
|
||||||
meta count: object.posts.count
|
meta count: object.posts.count
|
||||||
|
serializer.cached_roles
|
||||||
end
|
end
|
||||||
|
|
||||||
has_one :blog do
|
has_one :blog do
|
||||||
@ -60,6 +61,12 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
meta liked: object.likes.any?
|
meta liked: object.likes.any?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def cached_roles
|
||||||
|
[
|
||||||
|
Role.new(id: 'from-serializer-method')
|
||||||
|
]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@ -67,7 +74,7 @@ module ActiveModel
|
|||||||
@blog = Blog.new(id: 1337, name: 'extra')
|
@blog = Blog.new(id: 1337, name: 'extra')
|
||||||
@bio = Bio.new(id: 1337)
|
@bio = Bio.new(id: 1337)
|
||||||
@like = Like.new(id: 1337)
|
@like = Like.new(id: 1337)
|
||||||
@role = Role.new(id: 1337)
|
@role = Role.new(id: 'from-record')
|
||||||
@profile = Profile.new(id: 1337)
|
@profile = Profile.new(id: 1337)
|
||||||
@location = Location.new(id: 1337)
|
@location = Location.new(id: 1337)
|
||||||
@reviewer = Author.new(id: 1337)
|
@reviewer = Author.new(id: 1337)
|
||||||
@ -144,7 +151,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_relationship_meta
|
def test_relationship_meta
|
||||||
expected = {
|
expected = {
|
||||||
data: [{ id: '1337', type: 'roles' }],
|
data: [{ id: 'from-serializer-method', type: 'roles' }],
|
||||||
meta: { count: 1 }
|
meta: { count: 1 }
|
||||||
}
|
}
|
||||||
assert_relationship(:roles, expected)
|
assert_relationship(:roles, expected)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user