Add support for resource-level JSON API links.

This commit is contained in:
Lucas Hosseini
2015-10-06 19:35:09 +02:00
parent 827a623d16
commit 3804dcc238
4 changed files with 108 additions and 5 deletions

View File

@@ -50,6 +50,9 @@ module ActiveModel
self._attributes ||= []
class_attribute :_attributes_keys # @api private : maps attribute value to explict key name, @see Serializer#attribute
self._attributes_keys ||= {}
class_attribute :_links # @api private : links definitions, @see Serializer#link
self._links ||= {}
serializer.class_attribute :_cache # @api private : the cache object
serializer.class_attribute :_fragmented # @api private : @see ::fragmented
serializer.class_attribute :_cache_key # @api private : when present, is first item in cache_key
@@ -72,6 +75,7 @@ module ActiveModel
caller_line = caller.first
base._attributes = _attributes.dup
base._attributes_keys = _attributes_keys.dup
base._links = _links.dup
base._cache_digest = digest_caller_file(caller_line)
super
end
@@ -83,6 +87,10 @@ module ActiveModel
self._type = type
end
def self.link(name, value = nil, &block)
_links[name] = block || value
end
# @example
# class AdminAuthorSerializer < ActiveModel::Serializer
# attributes :id, :name, :recent_edits
@@ -249,6 +257,12 @@ module ActiveModel
end
end
# @api private
# Used by JsonApi adapter to build resource links.
def links
self.class._links
end
protected
attr_accessor :instance_options