mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Merge branch 'beauby-resource-level-meta'
Followup concerns: - https://github.com/rails-api/active_model_serializers/pull/1340/files#r47451387 - https://github.com/rails-api/active_model_serializers/pull/1340/files#r48116963 - https://github.com/rails-api/active_model_serializers/pull/1340#discussion_r47451387 - https://github.com/rails-api/active_model_serializers/pull/1340#issuecomment-164306882 - https://github.com/rails-api/active_model_serializers/pull/1340#issuecomment-166202978 - https://github.com/rails-api/active_model_serializers/pull/1340#issuecomment-173028896
This commit is contained in:
@@ -8,6 +8,7 @@ module ActiveModel
|
||||
autoload :Link
|
||||
autoload :Association
|
||||
autoload :ResourceIdentifier
|
||||
autoload :Meta
|
||||
autoload :Deserialization
|
||||
|
||||
# TODO: if we like this abstraction and other API objects to it,
|
||||
@@ -150,6 +151,9 @@ module ActiveModel
|
||||
links = links_for(serializer)
|
||||
resource_object[:links] = links if links.any?
|
||||
|
||||
meta = meta_for(serializer)
|
||||
resource_object[:meta] = meta unless meta.nil?
|
||||
|
||||
resource_object
|
||||
end
|
||||
|
||||
@@ -174,6 +178,10 @@ module ActiveModel
|
||||
def pagination_links_for(serializer, options)
|
||||
JsonApi::PaginationLinks.new(serializer.object, options[:serialization_context]).serializable_hash(options)
|
||||
end
|
||||
|
||||
def meta_for(serializer)
|
||||
Meta.new(serializer).as_json
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
29
lib/active_model/serializer/adapter/json_api/meta.rb
Normal file
29
lib/active_model/serializer/adapter/json_api/meta.rb
Normal file
@@ -0,0 +1,29 @@
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
module Adapter
|
||||
class JsonApi
|
||||
class Meta
|
||||
def initialize(serializer)
|
||||
@object = serializer.object
|
||||
@scope = serializer.scope
|
||||
|
||||
# Use the return value of the block unless it is nil.
|
||||
if serializer._meta.respond_to?(:call)
|
||||
@value = instance_eval(&serializer._meta)
|
||||
else
|
||||
@value = serializer._meta
|
||||
end
|
||||
end
|
||||
|
||||
def as_json
|
||||
@value
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
attr_reader :object, :scope
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
29
lib/active_model/serializer/meta.rb
Normal file
29
lib/active_model/serializer/meta.rb
Normal file
@@ -0,0 +1,29 @@
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
module Meta
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
with_options instance_writer: false, instance_reader: true do |serializer|
|
||||
serializer.class_attribute :_meta # @api private
|
||||
end
|
||||
|
||||
extend ActiveSupport::Autoload
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
# Set the JSON API meta attribute of a serializer.
|
||||
# @example
|
||||
# class AdminAuthorSerializer < ActiveModel::Serializer
|
||||
# meta { stuff: 'value' }
|
||||
# @example
|
||||
# meta do
|
||||
# { comment_count: object.comments.count }
|
||||
# end
|
||||
def meta(value = nil, &block)
|
||||
self._meta = block || value
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user