@@ -14,6 +14,19 @@ class Serializer
14
14
# end
15
15
# has_many :secret_meta_data, if: :is_admin?
16
16
#
17
+ # has_one :blog do |serializer|
18
+ # meta count: object.roles.count
19
+ # serializer.cached_blog
20
+ # end
21
+ #
22
+ # private
23
+ #
24
+ # def cached_blog
25
+ # cache_store.fetch("cached_blog:#{object.updated_at}") do
26
+ # Blog.find(object.blog_id)
27
+ # end
28
+ # end
29
+ #
17
30
# def is_admin?
18
31
# current_user.admin?
19
32
# end
@@ -32,43 +45,82 @@ class Serializer
32
45
# # ]
33
46
#
34
47
# So you can inspect reflections in your Adapters.
35
- #
36
48
class Reflection < Field
37
49
def initialize ( *)
38
50
super
39
- @_links = { }
40
- @_include_data = Serializer . config . include_data_default
41
- @_meta = nil
51
+ options [ :links ] = { }
52
+ options [ :include_data_setting ] = Serializer . config . include_data_default
53
+ options [ :meta ] = nil
42
54
end
43
55
56
+ # @api public
57
+ # @example
58
+ # has_one :blog do
59
+ # include_data false
60
+ # link :self, 'a link'
61
+ # link :related, 'another link'
62
+ # link :self, '//example.com/link_author/relationships/bio'
63
+ # id = object.profile.id
64
+ # link :related do
65
+ # "//example.com/profiles/#{id}" if id != 123
66
+ # end
67
+ # link :related do
68
+ # ids = object.likes.map(&:id).join(',')
69
+ # href "//example.com/likes/#{ids}"
70
+ # meta ids: ids
71
+ # end
72
+ # end
44
73
def link ( name , value = nil , &block )
45
- @_links [ name ] = block || value
74
+ options [ :links ] [ name ] = block || value
46
75
:nil
47
76
end
48
77
78
+ # @api public
79
+ # @example
80
+ # has_one :blog do
81
+ # include_data false
82
+ # meta(id: object.blog.id)
83
+ # meta liked: object.likes.any?
84
+ # link :self do
85
+ # href object.blog.id.to_s
86
+ # meta(id: object.blog.id)
87
+ # end
49
88
def meta ( value = nil , &block )
50
- @_meta = block || value
89
+ options [ :meta ] = block || value
51
90
:nil
52
91
end
53
92
93
+ # @api public
94
+ # @example
95
+ # has_one :blog do
96
+ # include_data false
97
+ # link :self, 'a link'
98
+ # link :related, 'another link'
99
+ # end
100
+ #
101
+ # has_one :blog do
102
+ # include_data false
103
+ # link :self, 'a link'
104
+ # link :related, 'another link'
105
+ # end
106
+ #
107
+ # belongs_to :reviewer do
108
+ # meta name: 'Dan Brown'
109
+ # include_data true
110
+ # end
111
+ #
112
+ # has_many :tags, serializer: TagSerializer do
113
+ # link :self, '//example.com/link_author/relationships/tags'
114
+ # include_data :if_sideloaded
115
+ # end
54
116
def include_data ( value = true )
55
- @_include_data = value
117
+ options [ :include_data_setting ] = value
56
118
:nil
57
119
end
58
120
59
121
# @param serializer [ActiveModel::Serializer]
60
122
# @yield [ActiveModel::Serializer]
61
123
# @return [:nil, associated resource or resource collection]
62
- # @example
63
- # has_one :blog do |serializer|
64
- # serializer.cached_blog
65
- # end
66
- #
67
- # def cached_blog
68
- # cache_store.fetch("cached_blog:#{object.updated_at}") do
69
- # Blog.find(object.blog_id)
70
- # end
71
- # end
72
124
def value ( serializer , include_slice )
73
125
@object = serializer . object
74
126
@scope = serializer . scope
@@ -103,7 +155,6 @@ def value(serializer, include_slice)
103
155
# comments_reflection.build_association(post_serializer, foo: 'bar')
104
156
#
105
157
# @api private
106
- #
107
158
def build_association ( parent_serializer , parent_serializer_options , include_slice = { } )
108
159
reflection_options = options . dup
109
160
@@ -113,8 +164,8 @@ def build_association(parent_serializer, parent_serializer_options, include_slic
113
164
association_value = value ( parent_serializer , include_slice )
114
165
serializer_class = parent_serializer . class . serializer_for ( association_value , reflection_options )
115
166
reflection_options [ :include_data ] = include_data? ( include_slice )
116
- reflection_options [ :links ] = @_links
117
- reflection_options [ :meta ] = @_meta
167
+ reflection_options [ :links ] = options [ :links ]
168
+ reflection_options [ :meta ] = options [ :meta ]
118
169
119
170
if serializer_class
120
171
serializer = catch ( :no_serializer ) do
@@ -138,15 +189,18 @@ def build_association(parent_serializer, parent_serializer_options, include_slic
138
189
139
190
protected
140
191
192
+ # used in instance exec
141
193
attr_accessor :object , :scope
142
194
143
195
private
144
196
145
197
def include_data? ( include_slice )
146
- if @_include_data == :if_sideloaded
147
- include_slice . key? ( name )
148
- else
149
- @_include_data
198
+ include_data_setting = options [ :include_data_setting ]
199
+ case include_data_setting
200
+ when :if_sideloaded then include_slice . key? ( name )
201
+ when true then true
202
+ when false then false
203
+ else fail ArgumentError , "Unknown include_data_setting '#{ include_data_setting . inspect } '"
150
204
end
151
205
end
152
206
0 commit comments