Refactor resource_identifier.

This commit is contained in:
Lucas Hosseini 2015-09-01 09:58:48 +02:00
parent d9c680599a
commit c4faafdebc

View File

@ -44,13 +44,25 @@ module ActiveModel
private private
def resource_identifier(serializer) def resource_identifier_type(serializer)
type = if ActiveModel::Serializer.config.jsonapi_resource_type == :plural if ActiveModel::Serializer.config.jsonapi_resource_type == :singular
serializer.object.class.model_name.plural
else
serializer.object.class.model_name.singular serializer.object.class.model_name.singular
else
serializer.object.class.model_name.plural
end end
id = serializer.respond_to?('id') ? serializer.id.to_s : serializer.object.id.to_s end
def resource_identifier_id(serializer)
if serializer.respond_to?('id')
serializer.id.to_s
else
serializer.object.id.to_s
end
end
def resource_identifier(serializer)
type = resource_identifier_type(serializer)
id = resource_identifier_id(serializer)
{ id: id, type: type } { id: id, type: type }
end end
@ -125,8 +137,8 @@ module ActiveModel
else else
if options[:virtual_value] if options[:virtual_value]
options[:virtual_value] options[:virtual_value]
elsif serializer.object elsif serializer && serializer.object
resurce_identifier(serializer) resource_identifier(serializer)
else else
nil nil
end end
@ -135,19 +147,13 @@ module ActiveModel
def add_resource_relationships(attrs, serializer, options = {}) def add_resource_relationships(attrs, serializer, options = {})
options[:add_included] = options.fetch(:add_included, true) options[:add_included] = options.fetch(:add_included, true)
attrs[:relationships] = {} if serializer.associations.any? attrs[:relationships] = {} if serializer.associations.any?
serializer.associations.each do |association| serializer.associations.each do |association|
key = association.key value = resource_relationship_value(association.serializer, association.options)
serializer = association.serializer
options = association.options
value = resource_relationship_value(serializer, options)
attrs[:relationships][association.key] = { data: value } attrs[:relationships][association.key] = { data: value }
if options[:add_included] if options[:add_included]
Array(serializer).each do |s| Array(association.serializer).each do |s|
add_included(key, s) add_included(association.key, s)
end end
end end
end end