mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Refactor ResourceIdentifier
This commit is contained in:
parent
4076a480b7
commit
5e1e138d47
@ -2,23 +2,8 @@ module ActiveModelSerializers
|
|||||||
module Adapter
|
module Adapter
|
||||||
class JsonApi
|
class JsonApi
|
||||||
class ResourceIdentifier
|
class ResourceIdentifier
|
||||||
def self.type_for(class_name, serializer_type = nil, transform_options = {})
|
def self.type_for(serializer, serializer_type = nil, transform_options = {})
|
||||||
if serializer_type
|
raw_type = serializer_type ? serializer_type : raw_type_from_serializer_object(serializer.object)
|
||||||
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)
|
JsonApi.send(:transform_key_casing!, raw_type, transform_options)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -30,6 +15,17 @@ module ActiveModelSerializers
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.raw_type_from_serializer_object(object)
|
||||||
|
class_name = object.class.name # should use model_name
|
||||||
|
serializer_type = class_name.underscore
|
||||||
|
singularize = ActiveModelSerializers.config.jsonapi_resource_type == :singular
|
||||||
|
inflection = singularize ? :singularize : :pluralize
|
||||||
|
serializer_type = ActiveSupport::Inflector.public_send(inflection, serializer_type)
|
||||||
|
serializer_type
|
||||||
|
.gsub!('/'.freeze, ActiveModelSerializers.config.jsonapi_namespace_separator)
|
||||||
|
serializer_type
|
||||||
|
end
|
||||||
|
|
||||||
# {http://jsonapi.org/format/#document-resource-identifier-objects Resource Identifier Objects}
|
# {http://jsonapi.org/format/#document-resource-identifier-objects Resource Identifier Objects}
|
||||||
def initialize(serializer, options)
|
def initialize(serializer, options)
|
||||||
@id = id_for(serializer)
|
@id = id_for(serializer)
|
||||||
@ -48,7 +44,7 @@ module ActiveModelSerializers
|
|||||||
private
|
private
|
||||||
|
|
||||||
def type_for(serializer, transform_options)
|
def type_for(serializer, transform_options)
|
||||||
self.class.type_for(serializer.object.class.name, serializer._type, transform_options)
|
self.class.type_for(serializer, serializer._type, transform_options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def id_for(serializer)
|
def id_for(serializer)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user