mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Namespace separator setting for json-api and tests (#1874)
Adds jsonapi_namespace_separator configuration Also: * Enable getting type from record class without serializer Needs Followup: - https://github.com/rails-api/active_model_serializers/pull/1874#discussion_r74607042 - https://github.com/rails-api/active_model_serializers/pull/1874#discussion_r74607734
This commit is contained in:
committed by
Benjamin Fleischer
parent
9217bc2ec4
commit
6de3f31b6e
@@ -223,6 +223,25 @@ module ActiveModelSerializers
|
||||
assert_equal expected, relationships
|
||||
end
|
||||
|
||||
def test_underscore_model_namespace_with_namespace_separator_for_linked_resource_type
|
||||
spammy_post = Post.new(id: 123)
|
||||
spammy_post.related = [Spam::UnrelatedLink.new(id: 456)]
|
||||
serializer = SpammyPostSerializer.new(spammy_post)
|
||||
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
|
||||
relationships = with_namespace_separator '--' do
|
||||
adapter.serializable_hash[:data][:relationships]
|
||||
end
|
||||
expected = {
|
||||
related: {
|
||||
data: [{
|
||||
type: 'spam--unrelated-links',
|
||||
id: '456'
|
||||
}]
|
||||
}
|
||||
}
|
||||
assert_equal expected, relationships
|
||||
end
|
||||
|
||||
def test_multiple_references_to_same_resource
|
||||
serializer = ActiveModel::Serializer::CollectionSerializer.new([@first_comment, @second_comment])
|
||||
adapter = ActiveModelSerializers::Adapter::JsonApi.new(
|
||||
|
||||
@@ -39,6 +39,26 @@ module ActiveModelSerializers
|
||||
test_type_inflection(AuthorSerializer, 'authors', :plural)
|
||||
end
|
||||
|
||||
def test_type_with_namespace
|
||||
Object.const_set(:Admin, Module.new)
|
||||
model = Class.new(::Model)
|
||||
Admin.const_set(:PowerUser, model)
|
||||
serializer = Class.new(ActiveModel::Serializer)
|
||||
Admin.const_set(:PowerUserSerializer, serializer)
|
||||
with_namespace_separator '--' do
|
||||
admin_user = Admin::PowerUser.new
|
||||
serializer = Admin::PowerUserSerializer.new(admin_user)
|
||||
expected = {
|
||||
id: admin_user.id,
|
||||
type: 'admin--power-users'
|
||||
}
|
||||
|
||||
identifier = ResourceIdentifier.new(serializer, {})
|
||||
actual = identifier.as_json
|
||||
assert_equal(expected, actual)
|
||||
end
|
||||
end
|
||||
|
||||
def test_id_defined_on_object
|
||||
test_id(AuthorSerializer, @model.id.to_s)
|
||||
end
|
||||
|
||||
@@ -9,6 +9,14 @@ module SerializationTesting
|
||||
ActiveModelSerializers::SerializableResource.new(obj).to_json
|
||||
end
|
||||
|
||||
def with_namespace_separator(seperator)
|
||||
original_seperator = ActiveModelSerializers.config.jsonapi_namespace_separator
|
||||
ActiveModelSerializers.config.jsonapi_namespace_separator = seperator
|
||||
yield
|
||||
ensure
|
||||
ActiveModelSerializers.config.jsonapi_namespace_separator = original_seperator
|
||||
end
|
||||
|
||||
# Aliased as :with_configured_adapter to clarify that
|
||||
# this method tests the configured adapter.
|
||||
# When not testing configuration, it may be preferable
|
||||
|
||||
Reference in New Issue
Block a user