1+ require 'active_model/serializer/field'
2+
13module ActiveModel
24 class Serializer
35 # Holds all the meta-data about an association as it was specified in the
46 # ActiveModel::Serializer class.
57 #
68 # @example
7- # class PostSerializer < ActiveModel::Serializer
9+ # class PostSerializer < ActiveModel::Serializer
810 # has_one :author, serializer: AuthorSerializer
911 # has_many :comments
1012 # has_many :comments, key: :last_comments do
1113 # object.comments.last(1)
1214 # 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
1421 #
15- # Notice that the association block is evaluated in the context of the serializer.
1622 # Specifically, the association 'comments' is evaluated two different ways:
1723 # 1) as 'comments' and named 'comments'.
1824 # 2) as 'object.comments.last(1)' and named 'last_comments'.
@@ -21,20 +27,13 @@ class Serializer
2127 # # [
2228 # # HasOneReflection.new(:author, serializer: AuthorSerializer),
2329 # # HasManyReflection.new(:comments)
30+ # # HasManyReflection.new(:comments, { key: :last_comments }, #<Block>)
31+ # # HasManyReflection.new(:secret_meta_data, { if: :is_admin? })
2432 # # ]
2533 #
2634 # So you can inspect reflections in your Adapters.
2735 #
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
3837 # Build association. This method is used internally to
3938 # build serializer's association by its reflection.
4039 #
0 commit comments