mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +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
@@ -21,13 +21,14 @@ module ActiveModel
|
||||
|
||||
config.default_includes = '*'
|
||||
config.adapter = :attributes
|
||||
config.key_transform = nil
|
||||
config.jsonapi_resource_type = :plural
|
||||
config.jsonapi_namespace_separator = '-'.freeze
|
||||
config.jsonapi_version = '1.0'
|
||||
config.jsonapi_toplevel_meta = {}
|
||||
# Make JSON API top-level jsonapi member opt-in
|
||||
# ref: http://jsonapi.org/format/#document-top-level
|
||||
config.jsonapi_include_toplevel_object = false
|
||||
config.key_transform = nil
|
||||
|
||||
config.schema_path = 'test/support/schemas'
|
||||
end
|
||||
|
||||
@@ -2,11 +2,30 @@ module ActiveModelSerializers
|
||||
module Adapter
|
||||
class JsonApi
|
||||
class ResourceIdentifier
|
||||
def self.type_for(class_name, serializer_type = nil, transform_options = {})
|
||||
if serializer_type
|
||||
raw_type = serializer_type
|
||||
else
|
||||
inflection =
|
||||
if ActiveModelSerializers.config.jsonapi_resource_type == :singular
|
||||
:singularize
|
||||
else
|
||||
:pluralize
|
||||
end
|
||||
|
||||
raw_type = class_name.underscore
|
||||
raw_type = ActiveSupport::Inflector.public_send(inflection, raw_type)
|
||||
raw_type
|
||||
.gsub!('/'.freeze, ActiveModelSerializers.config.jsonapi_namespace_separator)
|
||||
raw_type
|
||||
end
|
||||
JsonApi.send(:transform_key_casing!, raw_type, transform_options)
|
||||
end
|
||||
|
||||
# {http://jsonapi.org/format/#document-resource-identifier-objects Resource Identifier Objects}
|
||||
def initialize(serializer, options)
|
||||
@id = id_for(serializer)
|
||||
@type = JsonApi.send(:transform_key_casing!, type_for(serializer),
|
||||
options)
|
||||
@type = type_for(serializer, options)
|
||||
end
|
||||
|
||||
def as_json
|
||||
@@ -19,13 +38,8 @@ module ActiveModelSerializers
|
||||
|
||||
private
|
||||
|
||||
def type_for(serializer)
|
||||
return serializer._type if serializer._type
|
||||
if ActiveModelSerializers.config.jsonapi_resource_type == :singular
|
||||
serializer.object.class.model_name.singular
|
||||
else
|
||||
serializer.object.class.model_name.plural
|
||||
end
|
||||
def type_for(serializer, transform_options)
|
||||
self.class.type_for(serializer.object.class.name, serializer._type, transform_options)
|
||||
end
|
||||
|
||||
def id_for(serializer)
|
||||
|
||||
Reference in New Issue
Block a user