mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Extract attributes filtering from serializer into adapter.
This commit is contained in:
parent
066990184b
commit
658810e6a0
@ -23,6 +23,7 @@ Fixes:
|
|||||||
- [#1214](https://github.com/rails-api/active_model_serializers/pull/1214) retrieve the key from the reflection options when building associations (@NullVoxPopuli, @hut8)
|
- [#1214](https://github.com/rails-api/active_model_serializers/pull/1214) retrieve the key from the reflection options when building associations (@NullVoxPopuli, @hut8)
|
||||||
|
|
||||||
Misc:
|
Misc:
|
||||||
|
- [#1232](https://github.com/rails-api/active_model_serializers/pull/1232) fields option no longer handled at serializer level (@beauby)
|
||||||
- [#1178](https://github.com/rails-api/active_model_serializers/pull/1178) env CAPTURE_STDERR=false lets devs see hard failures (@bf4)
|
- [#1178](https://github.com/rails-api/active_model_serializers/pull/1178) env CAPTURE_STDERR=false lets devs see hard failures (@bf4)
|
||||||
- [#1177](https://github.com/rails-api/active_model_serializers/pull/1177) Remove Adapter autoloads in favor of require (@bf4)
|
- [#1177](https://github.com/rails-api/active_model_serializers/pull/1177) Remove Adapter autoloads in favor of require (@bf4)
|
||||||
- [#1117](https://github.com/rails-api/active_model_serializers/pull/1117) FlattenJson adapter no longer inherits Json adapter, renamed to Attributes (@bf4)
|
- [#1117](https://github.com/rails-api/active_model_serializers/pull/1117) FlattenJson adapter no longer inherits Json adapter, renamed to Attributes (@bf4)
|
||||||
|
|||||||
@ -148,13 +148,8 @@ module ActiveModel
|
|||||||
root || object.class.model_name.to_s.underscore
|
root || object.class.model_name.to_s.underscore
|
||||||
end
|
end
|
||||||
|
|
||||||
def attributes(options = {})
|
def attributes
|
||||||
attributes =
|
attributes = self.class._attributes.dup
|
||||||
if options[:fields]
|
|
||||||
self.class._attributes & options[:fields]
|
|
||||||
else
|
|
||||||
self.class._attributes.dup
|
|
||||||
end
|
|
||||||
|
|
||||||
attributes.each_with_object({}) do |name, hash|
|
attributes.each_with_object({}) do |name, hash|
|
||||||
unless self.class._fragmented
|
unless self.class._fragmented
|
||||||
|
|||||||
@ -57,7 +57,10 @@ module ActiveModel
|
|||||||
|
|
||||||
def resource_object_for(options)
|
def resource_object_for(options)
|
||||||
cache_check(serializer) do
|
cache_check(serializer) do
|
||||||
serializer.attributes(options)
|
attributes = serializer.attributes
|
||||||
|
attributes.slice!(*options[:fields]) if options[:fields]
|
||||||
|
|
||||||
|
attributes
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -47,7 +47,7 @@ module ActiveModel
|
|||||||
|
|
||||||
fields = options.delete(:fields)
|
fields = options.delete(:fields)
|
||||||
if fields
|
if fields
|
||||||
@fieldset = ActiveModel::Serializer::Fieldset.new(fields, serializer.json_key)
|
@fieldset = ActiveModel::Serializer::Fieldset.new(fields)
|
||||||
else
|
else
|
||||||
@fieldset = options[:fieldset]
|
@fieldset = options[:fieldset]
|
||||||
end
|
end
|
||||||
@ -60,7 +60,7 @@ module ActiveModel
|
|||||||
if serializer.respond_to?(:each)
|
if serializer.respond_to?(:each)
|
||||||
serializable_hash_for_collection(options)
|
serializable_hash_for_collection(options)
|
||||||
else
|
else
|
||||||
serializable_hash_for_single_resource(options)
|
serializable_hash_for_single_resource
|
||||||
end
|
end
|
||||||
|
|
||||||
ApiObjects::JsonApi.add!(hash)
|
ApiObjects::JsonApi.add!(hash)
|
||||||
@ -99,8 +99,8 @@ module ActiveModel
|
|||||||
hash
|
hash
|
||||||
end
|
end
|
||||||
|
|
||||||
def serializable_hash_for_single_resource(options)
|
def serializable_hash_for_single_resource
|
||||||
primary_data = primary_data_for(serializer, options)
|
primary_data = primary_data_for(serializer)
|
||||||
relationships = relationships_for(serializer)
|
relationships = relationships_for(serializer)
|
||||||
included = included_resources(@include_tree)
|
included = included_resources(@include_tree)
|
||||||
hash = { data: primary_data }
|
hash = { data: primary_data }
|
||||||
@ -134,22 +134,22 @@ module ActiveModel
|
|||||||
{ id: id.to_s, type: type }
|
{ id: id.to_s, type: type }
|
||||||
end
|
end
|
||||||
|
|
||||||
def resource_object_for(serializer, options = {})
|
def resource_object_for(serializer)
|
||||||
options[:fields] = fieldset && fieldset.fields_for(serializer)
|
|
||||||
|
|
||||||
cache_check(serializer) do
|
cache_check(serializer) do
|
||||||
result = resource_identifier_for(serializer)
|
resource_object = resource_identifier_for(serializer)
|
||||||
attributes = serializer.attributes(options).except(:id)
|
requested_fields = fieldset && fieldset.fields_for(resource_object[:type])
|
||||||
result[:attributes] = attributes if attributes.any?
|
attributes = serializer.attributes.except(:id)
|
||||||
result
|
attributes.slice!(*requested_fields) if requested_fields
|
||||||
|
resource_object[:attributes] = attributes if attributes.any?
|
||||||
|
resource_object
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def primary_data_for(serializer, options)
|
def primary_data_for(serializer)
|
||||||
if serializer.respond_to?(:each)
|
if serializer.respond_to?(:each)
|
||||||
serializer.map { |s| resource_object_for(s, options) }
|
serializer.map { |s| resource_object_for(s) }
|
||||||
else
|
else
|
||||||
resource_object_for(serializer, options)
|
resource_object_for(serializer)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -187,7 +187,7 @@ module ActiveModel
|
|||||||
else
|
else
|
||||||
return unless serializer && serializer.object
|
return unless serializer && serializer.object
|
||||||
|
|
||||||
primary_data = primary_data_for(serializer, instance_options)
|
primary_data = primary_data_for(serializer)
|
||||||
relationships = relationships_for(serializer)
|
relationships = relationships_for(serializer)
|
||||||
primary_data[:relationships] = relationships if relationships.any?
|
primary_data[:relationships] = relationships if relationships.any?
|
||||||
|
|
||||||
|
|||||||
@ -1,8 +1,7 @@
|
|||||||
module ActiveModel
|
module ActiveModel
|
||||||
class Serializer
|
class Serializer
|
||||||
class Fieldset
|
class Fieldset
|
||||||
def initialize(fields, root = nil)
|
def initialize(fields)
|
||||||
@root = root
|
|
||||||
@raw_fields = fields
|
@raw_fields = fields
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -10,27 +9,19 @@ module ActiveModel
|
|||||||
@fields ||= parsed_fields
|
@fields ||= parsed_fields
|
||||||
end
|
end
|
||||||
|
|
||||||
def fields_for(serializer)
|
def fields_for(type)
|
||||||
key = serializer.json_key
|
fields[type.singularize.to_sym] || fields[type.pluralize.to_sym]
|
||||||
fields[key.to_sym] || fields[key.pluralize.to_sym]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
ActiveModelSerializers.silence_warnings do
|
ActiveModelSerializers.silence_warnings do
|
||||||
attr_reader :raw_fields, :root
|
attr_reader :raw_fields
|
||||||
end
|
end
|
||||||
|
|
||||||
def parsed_fields
|
def parsed_fields
|
||||||
if raw_fields.is_a?(Hash)
|
if raw_fields.is_a?(Hash)
|
||||||
raw_fields.inject({}) { |h, (k, v)| h[k.to_sym] = v.map(&:to_sym); h }
|
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
|
else
|
||||||
{}
|
{}
|
||||||
end
|
end
|
||||||
|
|||||||
@ -58,8 +58,10 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_limiting_fields
|
def test_limiting_fields
|
||||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, fields: ['title'])
|
actual = ActiveModel::SerializableResource.new(
|
||||||
|
[@first_post, @second_post], adapter: :json_api,
|
||||||
|
fields: { posts: ['title'] })
|
||||||
|
.serializable_hash
|
||||||
expected = [
|
expected = [
|
||||||
{
|
{
|
||||||
id: '1',
|
id: '1',
|
||||||
@ -86,7 +88,7 @@ module ActiveModel
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
assert_equal(expected, @adapter.serializable_hash[:data])
|
assert_equal(expected, actual[:data])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -18,11 +18,6 @@ module ActiveModel
|
|||||||
@profile_serializer.class._attributes)
|
@profile_serializer.class._attributes)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_attributes_with_fields_option
|
|
||||||
assert_equal({ name: 'Name 1' },
|
|
||||||
@profile_serializer.attributes(fields: [:name]))
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_attributes_inheritance_definition
|
def test_attributes_inheritance_definition
|
||||||
assert_equal([:id, :body], @serializer_klass._attributes)
|
assert_equal([:id, :body], @serializer_klass._attributes)
|
||||||
end
|
end
|
||||||
|
|||||||
@ -4,22 +4,11 @@ module ActiveModel
|
|||||||
class Serializer
|
class Serializer
|
||||||
class FieldsetTest < Minitest::Test
|
class FieldsetTest < Minitest::Test
|
||||||
def test_fieldset_with_hash
|
def test_fieldset_with_hash
|
||||||
fieldset = ActiveModel::Serializer::Fieldset.new({ 'post' => %w(id title), 'coment' => ['body'] })
|
fieldset = ActiveModel::Serializer::Fieldset.new('post' => %w(id title), 'comment' => ['body'])
|
||||||
|
expected = { :post => [:id, :title], :comment => [:body] }
|
||||||
|
|
||||||
assert_equal(
|
assert_equal(expected, fieldset.fields)
|
||||||
{ :post => [:id, :title], :coment => [:body] },
|
|
||||||
fieldset.fields
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_fieldset_with_array_of_fields_and_root_name
|
|
||||||
fieldset = ActiveModel::Serializer::Fieldset.new(['title'], 'post')
|
|
||||||
|
|
||||||
assert_equal(
|
|
||||||
{ :post => [:title] },
|
|
||||||
fieldset.fields
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user