mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Merge pull request #1247 from beauby/jsonapi-toplevel-links
Add support for toplevel JSON API links.
This commit is contained in:
commit
da7e6dc795
@ -1,7 +1,7 @@
|
|||||||
require 'set'
|
require 'set'
|
||||||
module ActiveModel
|
module ActiveModel
|
||||||
class SerializableResource
|
class SerializableResource
|
||||||
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key])
|
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key, :links])
|
||||||
|
|
||||||
# Primary interface to composing a resource with a serializer and adapter.
|
# Primary interface to composing a resource with a serializer and adapter.
|
||||||
# @return the serializable_resource, ready for #as_json/#to_json/#serializable_hash.
|
# @return the serializable_resource, ready for #as_json/#to_json/#serializable_hash.
|
||||||
|
|||||||
@ -65,6 +65,11 @@ module ActiveModel
|
|||||||
|
|
||||||
ApiObjects::JsonApi.add!(hash)
|
ApiObjects::JsonApi.add!(hash)
|
||||||
|
|
||||||
|
if instance_options[:links]
|
||||||
|
hash[:links] ||= {}
|
||||||
|
hash[:links].update(instance_options[:links])
|
||||||
|
end
|
||||||
|
|
||||||
hash
|
hash
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
32
test/adapter/json_api/links_test.rb
Normal file
32
test/adapter/json_api/links_test.rb
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
module ActiveModel
|
||||||
|
class Serializer
|
||||||
|
module Adapter
|
||||||
|
class JsonApi
|
||||||
|
class LinksTest < Minitest::Test
|
||||||
|
def setup
|
||||||
|
@post = Post.new(id: 1337, comments: [], author: nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_toplevel_links
|
||||||
|
hash = ActiveModel::SerializableResource.new(
|
||||||
|
@post,
|
||||||
|
adapter: :json_api,
|
||||||
|
links: {
|
||||||
|
self: {
|
||||||
|
href: '//posts'
|
||||||
|
}
|
||||||
|
}).serializable_hash
|
||||||
|
expected = {
|
||||||
|
self: {
|
||||||
|
href: '//posts'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert_equal(expected, hash[:links])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in New Issue
Block a user