Benjamin Fleischer
2016-02-08 18:00:51 -06:00
6 changed files with 147 additions and 0 deletions

View File

@@ -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

View 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