Skip to content

Commit 701404f

Browse files
committed
Clean up meta handling.
1 parent 207c85f commit 701404f

File tree

4 files changed

+66
-26
lines changed

4 files changed

+66
-26
lines changed

lib/active_model/serializer.rb

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
require 'active_model/serializer/fieldset'
1010
require 'active_model/serializer/lint'
1111
require 'active_model/serializer/links'
12+
require 'active_model/serializer/meta'
1213
require 'active_model/serializer/type'
1314

1415
# ActiveModel::Serializer is an abstract class that is
@@ -20,26 +21,10 @@ class Serializer
2021
include Attributes
2122
include Caching
2223
include Links
24+
include Meta
2325
include Type
2426
require 'active_model/serializer/adapter'
2527

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-
4328
# @param resource [ActiveRecord::Base, ActiveModelSerializers::Model]
4429
# @return [ActiveModel::Serializer]
4530
# Preferentially returns
@@ -145,14 +130,6 @@ def read_attribute_for_serialization(attr)
145130
end
146131
end
147132

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-
156133
protected
157134

158135
attr_accessor :instance_options

lib/active_model/serializer/adapter/json_api.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class JsonApi < Base
66
autoload :PaginationLinks
77
autoload :FragmentCache
88
autoload :Link
9+
autoload :Meta
910
autoload :Deserialization
1011

1112
# TODO: if we like this abstraction and other API objects to it,
@@ -157,7 +158,7 @@ def resource_object_for(serializer)
157158
links = links_for(serializer)
158159
resource_object[:links] = links if links.any?
159160

160-
meta = serializer.meta
161+
meta = meta_for(serializer)
161162
resource_object[:meta] = meta unless meta.nil?
162163

163164
resource_object
@@ -220,6 +221,10 @@ def links_for(serializer)
220221
def pagination_links_for(serializer, options)
221222
JsonApi::PaginationLinks.new(serializer.object, options[:serialization_context]).serializable_hash(options)
222223
end
224+
225+
def meta_for(serializer)
226+
Meta.new(serializer).as_json
227+
end
223228
end
224229
end
225230
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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

lib/active_model/serializer/meta.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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

0 commit comments

Comments
 (0)