mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06: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
|
||||
def inherited(base)
|
||||
base._attributes = []
|
||||
base._associations = []
|
||||
base._associations = {}
|
||||
end
|
||||
|
||||
def setup
|
||||
@ -70,7 +70,7 @@ module ActiveModel
|
||||
end
|
||||
end
|
||||
|
||||
@_associations << klass.new(attr, options)
|
||||
@_associations[attr] = klass.new(attr, options)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -94,29 +94,41 @@ module ActiveModel
|
||||
end
|
||||
|
||||
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)
|
||||
end
|
||||
end
|
||||
|
||||
def associations
|
||||
self.class._associations.each_with_object({}) do |association, hash|
|
||||
if association.embed_ids?
|
||||
hash[association.key] = serialize_ids association
|
||||
elsif association.embed_objects?
|
||||
hash[association.embedded_key] = serialize association
|
||||
associations = self.class._associations
|
||||
included_associations = filter(associations.keys)
|
||||
associations.each_with_object({}) do |(name, association), hash|
|
||||
if included_associations.include? name
|
||||
if association.embed_ids?
|
||||
hash[association.key] = serialize_ids association
|
||||
elsif association.embed_objects?
|
||||
hash[association.embedded_key] = serialize association
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def filter(keys)
|
||||
keys
|
||||
end
|
||||
|
||||
def serializable_data
|
||||
embedded_in_root_associations.merge!(super)
|
||||
end
|
||||
|
||||
def embedded_in_root_associations
|
||||
self.class._associations.each_with_object({}) do |association, hash|
|
||||
if association.embed_in_root?
|
||||
hash[association.embedded_key] = serialize association
|
||||
associations = self.class._associations
|
||||
included_associations = filter(associations.keys)
|
||||
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
|
||||
|
||||
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 HasManyTest < ActiveModel::TestCase
|
||||
def setup
|
||||
@association = PostSerializer._associations[0]
|
||||
@association = PostSerializer._associations[:comments]
|
||||
@old_association = @association.dup
|
||||
@association.embed = :ids
|
||||
@post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
|
||||
@ -12,7 +12,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def teardown
|
||||
PostSerializer._associations[0] = @old_association
|
||||
PostSerializer._associations[:comments] = @old_association
|
||||
end
|
||||
|
||||
def test_associations_definition
|
||||
@ -85,7 +85,7 @@ module ActiveModel
|
||||
|
||||
def test_associations_embedding_ids_including_objects_serialization_using_as_json
|
||||
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
|
||||
assert_equal({
|
||||
|
||||
@ -4,7 +4,7 @@ module ActiveModel
|
||||
class Serializer
|
||||
class HasOneTest < ActiveModel::TestCase
|
||||
def setup
|
||||
@association = UserSerializer._associations[0]
|
||||
@association = UserSerializer._associations[:profile]
|
||||
@old_association = @association.dup
|
||||
@association.embed = :ids
|
||||
@user = User.new({ name: 'Name 1', email: 'mail@server.com', gender: 'M' })
|
||||
@ -12,7 +12,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def teardown
|
||||
UserSerializer._associations[0] = @old_association
|
||||
UserSerializer._associations[:profile] = @old_association
|
||||
end
|
||||
|
||||
def test_associations_definition
|
||||
|
||||
Loading…
Reference in New Issue
Block a user