mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Compute only requested attributes.
This commit is contained in:
parent
ecc3b35517
commit
0948c4199a
@ -238,10 +238,9 @@ module ActiveModel
|
|||||||
|
|
||||||
# Return the +attributes+ of +object+ as presented
|
# Return the +attributes+ of +object+ as presented
|
||||||
# by the serializer.
|
# by the serializer.
|
||||||
def attributes
|
def attributes(requested_attrs = nil)
|
||||||
attributes = self.class._attributes.dup
|
self.class._attributes.each_with_object({}) do |name, hash|
|
||||||
|
next unless requested_attrs.nil? || requested_attrs.include?(name)
|
||||||
attributes.each_with_object({}) do |name, hash|
|
|
||||||
if self.class._fragmented
|
if self.class._fragmented
|
||||||
hash[name] = self.class._fragmented.public_send(name)
|
hash[name] = self.class._fragmented.public_send(name)
|
||||||
else
|
else
|
||||||
|
|||||||
@ -57,10 +57,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def resource_object_for(options)
|
def resource_object_for(options)
|
||||||
cache_check(serializer) do
|
cache_check(serializer) do
|
||||||
attributes = serializer.attributes
|
serializer.attributes(options[:fields])
|
||||||
attributes.slice!(*options[:fields]) if options[:fields]
|
|
||||||
|
|
||||||
attributes
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -140,8 +140,7 @@ module ActiveModel
|
|||||||
cache_check(serializer) do
|
cache_check(serializer) do
|
||||||
resource_object = resource_identifier_for(serializer)
|
resource_object = resource_identifier_for(serializer)
|
||||||
requested_fields = fieldset && fieldset.fields_for(resource_object[:type])
|
requested_fields = fieldset && fieldset.fields_for(resource_object[:type])
|
||||||
attributes = serializer.attributes.except(:id)
|
attributes = serializer.attributes(requested_fields).except(:id)
|
||||||
attributes.slice!(*requested_fields) if requested_fields
|
|
||||||
resource_object[:attributes] = attributes if attributes.any?
|
resource_object[:attributes] = attributes if attributes.any?
|
||||||
resource_object
|
resource_object
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user