File tree Expand file tree Collapse file tree 4 files changed +66
-26
lines changed Expand file tree Collapse file tree 4 files changed +66
-26
lines changed Original file line number Diff line number Diff line change 9
9
require 'active_model/serializer/fieldset'
10
10
require 'active_model/serializer/lint'
11
11
require 'active_model/serializer/links'
12
+ require 'active_model/serializer/meta'
12
13
require 'active_model/serializer/type'
13
14
14
15
# ActiveModel::Serializer is an abstract class that is
@@ -20,26 +21,10 @@ class Serializer
20
21
include Attributes
21
22
include Caching
22
23
include Links
24
+ include Meta
23
25
include Type
24
26
require 'active_model/serializer/adapter'
25
27
26
- with_options instance_writer : false , instance_reader : false do |serializer |
27
- serializer . class_attribute :_meta # @api private : meta definition, @see Serializer#meta
28
- end
29
-
30
- # Register a meta attribute for the corresponding resource.
31
- #
32
- # @param [Hash] hash Optional hash
33
- # @param [Block] block Optional block
34
- def self . meta ( hash = nil , &block )
35
- self . _meta =
36
- if !block . nil?
37
- block
38
- else
39
- hash
40
- end
41
- end
42
-
43
28
# @param resource [ActiveRecord::Base, ActiveModelSerializers::Model]
44
29
# @return [ActiveModel::Serializer]
45
30
# Preferentially returns
@@ -145,14 +130,6 @@ def read_attribute_for_serialization(attr)
145
130
end
146
131
end
147
132
148
- def meta
149
- if self . class . _meta . respond_to? ( :call )
150
- instance_eval ( &self . class . _meta )
151
- else
152
- self . class . _meta
153
- end
154
- end
155
-
156
133
protected
157
134
158
135
attr_accessor :instance_options
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ class JsonApi < Base
6
6
autoload :PaginationLinks
7
7
autoload :FragmentCache
8
8
autoload :Link
9
+ autoload :Meta
9
10
autoload :Deserialization
10
11
11
12
# TODO: if we like this abstraction and other API objects to it,
@@ -157,7 +158,7 @@ def resource_object_for(serializer)
157
158
links = links_for ( serializer )
158
159
resource_object [ :links ] = links if links . any?
159
160
160
- meta = serializer . meta
161
+ meta = meta_for ( serializer )
161
162
resource_object [ :meta ] = meta unless meta . nil?
162
163
163
164
resource_object
@@ -220,6 +221,10 @@ def links_for(serializer)
220
221
def pagination_links_for ( serializer , options )
221
222
JsonApi ::PaginationLinks . new ( serializer . object , options [ :serialization_context ] ) . serializable_hash ( options )
222
223
end
224
+
225
+ def meta_for ( serializer )
226
+ Meta . new ( serializer ) . as_json
227
+ end
223
228
end
224
229
end
225
230
end
Original file line number Diff line number Diff line change
1
+ module ActiveModel
2
+ class Serializer
3
+ module Adapter
4
+ class JsonApi
5
+ class Meta
6
+ def initialize ( serializer )
7
+ @object = serializer . object
8
+ @scope = serializer . scope
9
+
10
+ # Use the return value of the block unless it is nil.
11
+ if serializer . _meta . respond_to? ( :call )
12
+ @value = instance_eval ( &serializer . _meta )
13
+ else
14
+ @value = serializer . _meta
15
+ end
16
+ end
17
+
18
+ def as_json
19
+ @value
20
+ end
21
+
22
+ protected
23
+
24
+ attr_reader :object , :scope
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
Original file line number Diff line number Diff line change
1
+ module ActiveModel
2
+ class Serializer
3
+ module Meta
4
+ extend ActiveSupport ::Concern
5
+
6
+ included do
7
+ with_options instance_writer : false , instance_reader : true do |serializer |
8
+ serializer . class_attribute :_meta # @api private
9
+ end
10
+
11
+ extend ActiveSupport ::Autoload
12
+ end
13
+
14
+ module ClassMethods
15
+ # Set the JSON API meta attribute of a serializer.
16
+ # @example
17
+ # class AdminAuthorSerializer < ActiveModel::Serializer
18
+ # meta { stuff: 'value' }
19
+ # @example
20
+ # meta do
21
+ # { comment_count: object.comments.count }
22
+ # end
23
+ def meta ( value = nil , &block )
24
+ self . _meta = block || value
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
You can’t perform that action at this time.
0 commit comments