mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Refactor.
This commit is contained in:
parent
343f8b96bd
commit
d9c680599a
@ -50,8 +50,7 @@ module ActiveModel
|
|||||||
else
|
else
|
||||||
serializer.object.class.model_name.singular
|
serializer.object.class.model_name.singular
|
||||||
end
|
end
|
||||||
id = serializer.id.to_s if serializer.respond_to?('id')
|
id = serializer.respond_to?('id') ? serializer.id.to_s : serializer.object.id.to_s
|
||||||
id ||= serializer.object.id.to_s
|
|
||||||
|
|
||||||
{ id: id, type: type }
|
{ id: id, type: type }
|
||||||
end
|
end
|
||||||
@ -85,25 +84,18 @@ module ActiveModel
|
|||||||
|
|
||||||
def attributes_for_serializer(serializer, options)
|
def attributes_for_serializer(serializer, options)
|
||||||
if serializer.respond_to?(:each)
|
if serializer.respond_to?(:each)
|
||||||
result = []
|
serializer.map { |s| resource_object_for(s, options) }
|
||||||
serializer.each do |object|
|
|
||||||
result << resource_object_for(object, options)
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
result = resource_object_for(serializer, options)
|
resource_object_for(serializer, options)
|
||||||
end
|
end
|
||||||
result
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def resource_object_for(serializer, options)
|
def resource_object_for(serializer, options)
|
||||||
options[:fields] = @fieldset && @fieldset.fields_for(serializer)
|
options[:fields] = @fieldset && @fieldset.fields_for(serializer)
|
||||||
|
|
||||||
cache_check(serializer) do
|
cache_check(serializer) do
|
||||||
attributes = serializer.attributes(options)
|
|
||||||
attributes.delete(:id)
|
|
||||||
|
|
||||||
result = resource_identifier(serializer)
|
result = resource_identifier(serializer)
|
||||||
|
attributes = serializer.attributes(options).except(:id)
|
||||||
result[:attributes] = attributes if attributes.any?
|
result[:attributes] = attributes if attributes.any?
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
@ -127,25 +119,29 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def resource_relationship_value(serializer, options = {})
|
||||||
|
if serializer.respond_to?(:each)
|
||||||
|
serializer.map { |s| resource_identifier(s) }
|
||||||
|
else
|
||||||
|
if options[:virtual_value]
|
||||||
|
options[:virtual_value]
|
||||||
|
elsif serializer.object
|
||||||
|
resurce_identifier(serializer)
|
||||||
|
else
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
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
|
key = association.key
|
||||||
serializer = association.serializer
|
serializer = association.serializer
|
||||||
opts = association.options
|
options = association.options
|
||||||
value = if serializer.respond_to?(:each)
|
value = resource_relationship_value(serializer, options)
|
||||||
serializer.map { |s| resource_identifier(s) }
|
|
||||||
else
|
|
||||||
if opts[:virtual_value]
|
|
||||||
opts[:virtual_value]
|
|
||||||
elsif serializer && serializer.object
|
|
||||||
resource_identifier(serializer)
|
|
||||||
else
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
attrs[:relationships][association.key] = { data: value }
|
attrs[:relationships][association.key] = { data: value }
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user