mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 22:36:50 +00:00
Conflicts: CHANGELOG.md lib/active_model/serializer/adapter/attributes.rb lib/active_model/serializer/adapter/cached_serializer.rb lib/active_model/serializer/adapter/fragment_cache.rb lib/active_model/serializer/adapter/json_api.rb lib/active_model/serializer/adapter/json_api/link.rb test/adapter/fragment_cache_test.rb test/adapter/json_api/links_test.rb test/adapter/json_api/resource_type_config_test.rb
85 lines
2.3 KiB
Ruby
85 lines
2.3 KiB
Ruby
require 'test_helper'
|
|
|
|
module ActiveModelSerializers
|
|
module Adapter
|
|
class JsonApi
|
|
class LinksTest < ActiveSupport::TestCase
|
|
LinkAuthor = Class.new(::Model)
|
|
class LinkAuthorSerializer < ActiveModel::Serializer
|
|
link :self do
|
|
href "//example.com/link_author/#{object.id}"
|
|
meta stuff: 'value'
|
|
end
|
|
|
|
link :other, '//example.com/resource'
|
|
|
|
link :yet_another do
|
|
"//example.com/resource/#{object.id}"
|
|
end
|
|
end
|
|
|
|
def setup
|
|
@post = Post.new(id: 1337, comments: [], author: nil)
|
|
@author = LinkAuthor.new(id: 1337, posts: [@post])
|
|
end
|
|
|
|
def test_toplevel_links
|
|
hash = ActiveModel::SerializableResource.new(
|
|
@post,
|
|
adapter: :json_api,
|
|
links: {
|
|
self: {
|
|
href: '//example.com/posts',
|
|
meta: {
|
|
stuff: 'value'
|
|
}
|
|
}
|
|
}).serializable_hash
|
|
expected = {
|
|
self: {
|
|
href: '//example.com/posts',
|
|
meta: {
|
|
stuff: 'value'
|
|
}
|
|
}
|
|
}
|
|
assert_equal(expected, hash[:links])
|
|
end
|
|
|
|
def test_nil_toplevel_links
|
|
hash = ActiveModel::SerializableResource.new(
|
|
@post,
|
|
adapter: :json_api,
|
|
links: nil
|
|
).serializable_hash
|
|
refute hash.key?(:links), 'No links key to be output'
|
|
end
|
|
|
|
def test_nil_toplevel_links_json_adapter
|
|
hash = ActiveModel::SerializableResource.new(
|
|
@post,
|
|
adapter: :json,
|
|
links: nil
|
|
).serializable_hash
|
|
refute hash.key?(:links), 'No links key to be output'
|
|
end
|
|
|
|
def test_resource_links
|
|
hash = serializable(@author, adapter: :json_api).serializable_hash
|
|
expected = {
|
|
self: {
|
|
href: '//example.com/link_author/1337',
|
|
meta: {
|
|
stuff: 'value'
|
|
}
|
|
},
|
|
other: '//example.com/resource',
|
|
yet_another: '//example.com/resource/1337'
|
|
}
|
|
assert_equal(expected, hash[:data][:links])
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|