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