1
+ require 'active_model/serializer/field'
2
+
1
3
module ActiveModel
2
4
class Serializer
3
5
# Holds all the meta-data about an association as it was specified in the
4
6
# ActiveModel::Serializer class.
5
7
#
6
8
# @example
7
- # class PostSerializer < ActiveModel::Serializer
9
+ # class PostSerializer < ActiveModel::Serializer
8
10
# has_one :author, serializer: AuthorSerializer
9
11
# has_many :comments
10
12
# has_many :comments, key: :last_comments do
11
13
# object.comments.last(1)
12
14
# end
13
- # end
15
+ # has_many :secret_meta_data, if: :is_admin?
16
+ #
17
+ # def is_admin?
18
+ # current_user.admin?
19
+ # end
20
+ # end
14
21
#
15
- # Notice that the association block is evaluated in the context of the serializer.
16
22
# Specifically, the association 'comments' is evaluated two different ways:
17
23
# 1) as 'comments' and named 'comments'.
18
24
# 2) as 'object.comments.last(1)' and named 'last_comments'.
@@ -21,20 +27,13 @@ class Serializer
21
27
# # [
22
28
# # HasOneReflection.new(:author, serializer: AuthorSerializer),
23
29
# # HasManyReflection.new(:comments)
30
+ # # HasManyReflection.new(:comments, { key: :last_comments }, #<Block>)
31
+ # # HasManyReflection.new(:secret_meta_data, { if: :is_admin? })
24
32
# # ]
25
33
#
26
34
# So you can inspect reflections in your Adapters.
27
35
#
28
- Reflection = Struct . new ( :name , :options , :block ) do
29
- # @api private
30
- def value ( instance )
31
- if block
32
- instance . instance_eval ( &block )
33
- else
34
- instance . read_attribute_for_serialization ( name )
35
- end
36
- end
37
-
36
+ class Reflection < Field
38
37
# Build association. This method is used internally to
39
38
# build serializer's association by its reflection.
40
39
#
0 commit comments