mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Merge branch 'bf4-consider_association_blocks'
This commit is contained in:
commit
7d4f0c5c8a
@ -16,6 +16,11 @@ Breaking changes:
|
|||||||
|
|
||||||
Features:
|
Features:
|
||||||
|
|
||||||
|
- [#1378](https://github.com/rails-api/active_model_serializers/pull/1378) Change association blocks
|
||||||
|
to be evaluated in *serializer* scope, rather than *association* scope. (@bf4)
|
||||||
|
* Syntax changes from e.g.
|
||||||
|
`has_many :titles do customers.pluck(:title) end` (in #1356) to
|
||||||
|
`has_many :titles do object.customers.pluck(:title) end`
|
||||||
- [#1356](https://github.com/rails-api/active_model_serializers/pull/1356) Add inline syntax for
|
- [#1356](https://github.com/rails-api/active_model_serializers/pull/1356) Add inline syntax for
|
||||||
attributes and associations (@bf4 @beauby @noahsilas)
|
attributes and associations (@bf4 @beauby @noahsilas)
|
||||||
* Allows defining attributes so that they don't conflict with existing methods. e.g. `attribute
|
* Allows defining attributes so that they don't conflict with existing methods. e.g. `attribute
|
||||||
|
|||||||
@ -8,14 +8,14 @@ module ActiveModel
|
|||||||
# has_one :author, serializer: AuthorSerializer
|
# has_one :author, serializer: AuthorSerializer
|
||||||
# has_many :comments
|
# has_many :comments
|
||||||
# has_many :comments, key: :last_comments do
|
# has_many :comments, key: :last_comments do
|
||||||
# last(1)
|
# object.comments.last(1)
|
||||||
# end
|
# end
|
||||||
# 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:
|
# Specifically, the association 'comments' is evaluated two different ways:
|
||||||
# 1) as 'comments' and named 'comments'.
|
# 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 #=>
|
# PostSerializer._reflections #=>
|
||||||
# # [
|
# # [
|
||||||
@ -29,7 +29,7 @@ module ActiveModel
|
|||||||
# @api private
|
# @api private
|
||||||
def value(instance)
|
def value(instance)
|
||||||
if block
|
if block
|
||||||
instance.read_attribute_for_serialization(name).instance_eval(&block)
|
instance.instance_eval(&block)
|
||||||
else
|
else
|
||||||
instance.read_attribute_for_serialization(name)
|
instance.read_attribute_for_serialization(name)
|
||||||
end
|
end
|
||||||
|
|||||||
2
test/fixtures/poro.rb
vendored
2
test/fixtures/poro.rb
vendored
@ -33,6 +33,7 @@ end
|
|||||||
class ProfileSerializer < ActiveModel::Serializer
|
class ProfileSerializer < ActiveModel::Serializer
|
||||||
attributes :name, :description
|
attributes :name, :description
|
||||||
|
|
||||||
|
# TODO: is this used anywhere?
|
||||||
def arguments_passed_in?
|
def arguments_passed_in?
|
||||||
instance_options[:my_options] == :accessible
|
instance_options[:my_options] == :accessible
|
||||||
end
|
end
|
||||||
@ -75,6 +76,7 @@ PostSerializer = Class.new(ActiveModel::Serializer) do
|
|||||||
Blog.new(id: 999, name: 'Custom blog')
|
Blog.new(id: 999, name: 'Custom blog')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# TODO: is this used anywhere?
|
||||||
def custom_options
|
def custom_options
|
||||||
instance_options
|
instance_options
|
||||||
end
|
end
|
||||||
|
|||||||
@ -129,7 +129,7 @@ module ActiveModel
|
|||||||
class InlineAssociationTestPostSerializer < ActiveModel::Serializer
|
class InlineAssociationTestPostSerializer < ActiveModel::Serializer
|
||||||
has_many :comments
|
has_many :comments
|
||||||
has_many :comments, key: :last_comments do
|
has_many :comments, key: :last_comments do
|
||||||
last(1)
|
object.comments.last(1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user