mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Extract attributes filtering from serializer into adapter.
This commit is contained in:
@@ -57,7 +57,10 @@ module ActiveModel
|
||||
|
||||
def resource_object_for(options)
|
||||
cache_check(serializer) do
|
||||
serializer.attributes(options)
|
||||
attributes = serializer.attributes
|
||||
attributes.slice!(*options[:fields]) if options[:fields]
|
||||
|
||||
attributes
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -47,7 +47,7 @@ module ActiveModel
|
||||
|
||||
fields = options.delete(:fields)
|
||||
if fields
|
||||
@fieldset = ActiveModel::Serializer::Fieldset.new(fields, serializer.json_key)
|
||||
@fieldset = ActiveModel::Serializer::Fieldset.new(fields)
|
||||
else
|
||||
@fieldset = options[:fieldset]
|
||||
end
|
||||
@@ -60,7 +60,7 @@ module ActiveModel
|
||||
if serializer.respond_to?(:each)
|
||||
serializable_hash_for_collection(options)
|
||||
else
|
||||
serializable_hash_for_single_resource(options)
|
||||
serializable_hash_for_single_resource
|
||||
end
|
||||
|
||||
ApiObjects::JsonApi.add!(hash)
|
||||
@@ -99,8 +99,8 @@ module ActiveModel
|
||||
hash
|
||||
end
|
||||
|
||||
def serializable_hash_for_single_resource(options)
|
||||
primary_data = primary_data_for(serializer, options)
|
||||
def serializable_hash_for_single_resource
|
||||
primary_data = primary_data_for(serializer)
|
||||
relationships = relationships_for(serializer)
|
||||
included = included_resources(@include_tree)
|
||||
hash = { data: primary_data }
|
||||
@@ -134,22 +134,22 @@ module ActiveModel
|
||||
{ id: id.to_s, type: type }
|
||||
end
|
||||
|
||||
def resource_object_for(serializer, options = {})
|
||||
options[:fields] = fieldset && fieldset.fields_for(serializer)
|
||||
|
||||
def resource_object_for(serializer)
|
||||
cache_check(serializer) do
|
||||
result = resource_identifier_for(serializer)
|
||||
attributes = serializer.attributes(options).except(:id)
|
||||
result[:attributes] = attributes if attributes.any?
|
||||
result
|
||||
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
|
||||
resource_object[:attributes] = attributes if attributes.any?
|
||||
resource_object
|
||||
end
|
||||
end
|
||||
|
||||
def primary_data_for(serializer, options)
|
||||
def primary_data_for(serializer)
|
||||
if serializer.respond_to?(:each)
|
||||
serializer.map { |s| resource_object_for(s, options) }
|
||||
serializer.map { |s| resource_object_for(s) }
|
||||
else
|
||||
resource_object_for(serializer, options)
|
||||
resource_object_for(serializer)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -187,7 +187,7 @@ module ActiveModel
|
||||
else
|
||||
return unless serializer && serializer.object
|
||||
|
||||
primary_data = primary_data_for(serializer, instance_options)
|
||||
primary_data = primary_data_for(serializer)
|
||||
relationships = relationships_for(serializer)
|
||||
primary_data[:relationships] = relationships if relationships.any?
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class Fieldset
|
||||
def initialize(fields, root = nil)
|
||||
@root = root
|
||||
def initialize(fields)
|
||||
@raw_fields = fields
|
||||
end
|
||||
|
||||
@@ -10,27 +9,19 @@ module ActiveModel
|
||||
@fields ||= parsed_fields
|
||||
end
|
||||
|
||||
def fields_for(serializer)
|
||||
key = serializer.json_key
|
||||
fields[key.to_sym] || fields[key.pluralize.to_sym]
|
||||
def fields_for(type)
|
||||
fields[type.singularize.to_sym] || fields[type.pluralize.to_sym]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
ActiveModelSerializers.silence_warnings do
|
||||
attr_reader :raw_fields, :root
|
||||
attr_reader :raw_fields
|
||||
end
|
||||
|
||||
def parsed_fields
|
||||
if raw_fields.is_a?(Hash)
|
||||
raw_fields.inject({}) { |h, (k, v)| h[k.to_sym] = v.map(&:to_sym); h }
|
||||
elsif raw_fields.is_a?(Array)
|
||||
if root.nil?
|
||||
raise ArgumentError, 'The root argument must be specified if the fields argument is an array.'.freeze
|
||||
end
|
||||
hash = {}
|
||||
hash[root.to_sym] = raw_fields.map(&:to_sym)
|
||||
hash
|
||||
else
|
||||
{}
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user