mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Add filter to allow users implement filter method to include/exclude attributes and relations
This commit is contained in:
parent
f6ea07dd22
commit
841f3b8181
@ -10,7 +10,7 @@ module ActiveModel
|
|||||||
class << self
|
class << self
|
||||||
def inherited(base)
|
def inherited(base)
|
||||||
base._attributes = []
|
base._attributes = []
|
||||||
base._associations = []
|
base._associations = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@ -70,7 +70,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@_associations << klass.new(attr, options)
|
@_associations[attr] = klass.new(attr, options)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -94,29 +94,41 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def attributes
|
def attributes
|
||||||
self.class._attributes.each_with_object({}) do |name, hash|
|
filter(self.class._attributes.dup).each_with_object({}) do |name, hash|
|
||||||
hash[name] = send(name)
|
hash[name] = send(name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def associations
|
def associations
|
||||||
self.class._associations.each_with_object({}) do |association, hash|
|
associations = self.class._associations
|
||||||
if association.embed_ids?
|
included_associations = filter(associations.keys)
|
||||||
hash[association.key] = serialize_ids association
|
associations.each_with_object({}) do |(name, association), hash|
|
||||||
elsif association.embed_objects?
|
if included_associations.include? name
|
||||||
hash[association.embedded_key] = serialize association
|
if association.embed_ids?
|
||||||
|
hash[association.key] = serialize_ids association
|
||||||
|
elsif association.embed_objects?
|
||||||
|
hash[association.embedded_key] = serialize association
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def filter(keys)
|
||||||
|
keys
|
||||||
|
end
|
||||||
|
|
||||||
def serializable_data
|
def serializable_data
|
||||||
embedded_in_root_associations.merge!(super)
|
embedded_in_root_associations.merge!(super)
|
||||||
end
|
end
|
||||||
|
|
||||||
def embedded_in_root_associations
|
def embedded_in_root_associations
|
||||||
self.class._associations.each_with_object({}) do |association, hash|
|
associations = self.class._associations
|
||||||
if association.embed_in_root?
|
included_associations = filter(associations.keys)
|
||||||
hash[association.embedded_key] = serialize association
|
associations.each_with_object({}) do |(name, association), hash|
|
||||||
|
if included_associations.include? name
|
||||||
|
if association.embed_in_root?
|
||||||
|
hash[association.embedded_key] = serialize association
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
49
test/unit/active_model/serializer/filter_test.rb
Normal file
49
test/unit/active_model/serializer/filter_test.rb
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
module ActiveModel
|
||||||
|
class Serializer
|
||||||
|
class FilterAttributesTest < ActiveModel::TestCase
|
||||||
|
def setup
|
||||||
|
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||||
|
@profile_serializer = ProfileSerializer.new(@profile)
|
||||||
|
@profile_serializer.instance_eval do
|
||||||
|
def filter(keys)
|
||||||
|
keys - [:description]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_filtered_attributes_serialization
|
||||||
|
assert_equal({
|
||||||
|
'profile' => { name: 'Name 1' }
|
||||||
|
}, @profile_serializer.as_json)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class FilterAssociationsTest < ActiveModel::TestCase
|
||||||
|
def setup
|
||||||
|
@association = PostSerializer._associations[:comments]
|
||||||
|
@old_association = @association.dup
|
||||||
|
@association.embed = :ids
|
||||||
|
@association.embed_in_root = true
|
||||||
|
@post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
|
||||||
|
@post_serializer = PostSerializer.new(@post)
|
||||||
|
@post_serializer.instance_eval do
|
||||||
|
def filter(keys)
|
||||||
|
keys - [:body, :comments]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def teardown
|
||||||
|
PostSerializer._associations[:comments] = @old_association
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_filtered_associations_serialization
|
||||||
|
assert_equal({
|
||||||
|
'post' => { title: 'Title 1' }
|
||||||
|
}, @post_serializer.as_json)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -4,7 +4,7 @@ module ActiveModel
|
|||||||
class Serializer
|
class Serializer
|
||||||
class HasManyTest < ActiveModel::TestCase
|
class HasManyTest < ActiveModel::TestCase
|
||||||
def setup
|
def setup
|
||||||
@association = PostSerializer._associations[0]
|
@association = PostSerializer._associations[:comments]
|
||||||
@old_association = @association.dup
|
@old_association = @association.dup
|
||||||
@association.embed = :ids
|
@association.embed = :ids
|
||||||
@post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
|
@post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
|
||||||
@ -12,7 +12,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
PostSerializer._associations[0] = @old_association
|
PostSerializer._associations[:comments] = @old_association
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_associations_definition
|
def test_associations_definition
|
||||||
@ -85,7 +85,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_associations_embedding_ids_including_objects_serialization_using_as_json
|
def test_associations_embedding_ids_including_objects_serialization_using_as_json
|
||||||
PostSerializer.embed :ids, include: true
|
PostSerializer.embed :ids, include: true
|
||||||
PostSerializer._associations[0].send :initialize, @association.name, @association.options
|
PostSerializer._associations[:comments].send :initialize, @association.name, @association.options
|
||||||
|
|
||||||
@post_serializer.root = nil
|
@post_serializer.root = nil
|
||||||
assert_equal({
|
assert_equal({
|
||||||
|
|||||||
@ -4,7 +4,7 @@ module ActiveModel
|
|||||||
class Serializer
|
class Serializer
|
||||||
class HasOneTest < ActiveModel::TestCase
|
class HasOneTest < ActiveModel::TestCase
|
||||||
def setup
|
def setup
|
||||||
@association = UserSerializer._associations[0]
|
@association = UserSerializer._associations[:profile]
|
||||||
@old_association = @association.dup
|
@old_association = @association.dup
|
||||||
@association.embed = :ids
|
@association.embed = :ids
|
||||||
@user = User.new({ name: 'Name 1', email: 'mail@server.com', gender: 'M' })
|
@user = User.new({ name: 'Name 1', email: 'mail@server.com', gender: 'M' })
|
||||||
@ -12,7 +12,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
UserSerializer._associations[0] = @old_association
|
UserSerializer._associations[:profile] = @old_association
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_associations_definition
|
def test_associations_definition
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user