mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Move key's initialization code to initializers
This commit is contained in:
parent
280fd65db8
commit
af34adc7b5
@ -7,15 +7,17 @@ module ActiveModel
|
|||||||
@name = name
|
@name = name
|
||||||
@options = options
|
@options = options
|
||||||
|
|
||||||
self.embed = options[:embed]
|
self.embed = options[:embed]
|
||||||
@embed_key = options[:embed_key] || :id
|
@embed_key = options[:embed_key] || :id
|
||||||
@include = options[:include]
|
@include = options[:include]
|
||||||
|
@key = options[:key]
|
||||||
|
@embedded_key = options[:root]
|
||||||
|
|
||||||
self.serializer_class = @options[:serializer]
|
self.serializer_class = @options[:serializer]
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :name, :embed_ids, :embed_objects, :embed_key, :serializer_class, :options
|
attr_reader :name, :embed_ids, :embed_objects, :embed_key, :serializer_class, :options
|
||||||
attr_accessor :include
|
attr_accessor :include, :key, :embedded_key
|
||||||
alias embed_ids? embed_ids
|
alias embed_ids? embed_ids
|
||||||
alias embed_objects? embed_objects
|
alias embed_objects? embed_objects
|
||||||
alias include? include
|
alias include? include
|
||||||
@ -44,22 +46,18 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
class HasOne < Association
|
class HasOne < Association
|
||||||
def key
|
def initialize(*args)
|
||||||
@options[:key] || "#{name}_id"
|
super
|
||||||
end
|
@key ||= "#{name}_id"
|
||||||
|
@embedded_key ||= name.pluralize
|
||||||
def embedded_key
|
|
||||||
@options[:root] || name.pluralize
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class HasMany < Association
|
class HasMany < Association
|
||||||
def key
|
def initialize(*args)
|
||||||
@options[:key] || "#{name.singularize}_ids"
|
super
|
||||||
end
|
@key ||= "#{name.singularize}_ids"
|
||||||
|
@embedded_key ||= name
|
||||||
def embedded_key
|
|
||||||
@options[:root] || name
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -9,8 +9,6 @@ module ActiveModel
|
|||||||
@association = PostSerializer._associations[0]
|
@association = PostSerializer._associations[0]
|
||||||
@association.include = false
|
@association.include = false
|
||||||
@association.embed = :ids
|
@association.embed = :ids
|
||||||
@association.options[:root] = nil
|
|
||||||
@association.options[:key] = nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_associations_definition
|
def test_associations_definition
|
||||||
@ -32,10 +30,13 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options
|
def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options
|
||||||
@association.options[:key] = 'key'
|
old_key = @association.key
|
||||||
|
@association.key = 'key'
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'title' => 'Title 1', 'body' => 'Body 1', 'key' => @post.comments.map { |c| c.object_id }
|
'title' => 'Title 1', 'body' => 'Body 1', 'key' => @post.comments.map { |c| c.object_id }
|
||||||
}, @post_serializer.serializable_hash)
|
}, @post_serializer.serializable_hash)
|
||||||
|
ensure
|
||||||
|
@association.key = old_key
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_associations_embedding_objects_serialization_using_serializable_hash
|
def test_associations_embedding_objects_serialization_using_serializable_hash
|
||||||
@ -54,10 +55,13 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options
|
def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options
|
||||||
@association.embed = :objects
|
@association.embed = :objects
|
||||||
@association.options[:root] = 'root'
|
old_embedded_key = @association.embedded_key
|
||||||
|
@association.embedded_key = 'root'
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'title' => 'Title 1', 'body' => 'Body 1', 'root' => [{ 'content' => 'C1' }, { 'content' => 'C2' }]
|
'title' => 'Title 1', 'body' => 'Body 1', 'root' => [{ 'content' => 'C1' }, { 'content' => 'C2' }]
|
||||||
}, @post_serializer.serializable_hash)
|
}, @post_serializer.serializable_hash)
|
||||||
|
ensure
|
||||||
|
@association.embedded_key = old_embedded_key
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
|
def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
|
||||||
|
|||||||
@ -9,8 +9,6 @@ module ActiveModel
|
|||||||
@association = UserSerializer._associations[0]
|
@association = UserSerializer._associations[0]
|
||||||
@association.include = false
|
@association.include = false
|
||||||
@association.embed = :ids
|
@association.embed = :ids
|
||||||
@association.options[:root] = nil
|
|
||||||
@association.options[:key] = nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_associations_definition
|
def test_associations_definition
|
||||||
@ -32,10 +30,13 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options
|
def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options
|
||||||
@association.options[:key] = 'key'
|
old_key = @association.key
|
||||||
|
@association.key = 'key'
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'name' => 'Name 1', 'email' => 'mail@server.com', 'key' => @user.profile.object_id
|
'name' => 'Name 1', 'email' => 'mail@server.com', 'key' => @user.profile.object_id
|
||||||
}, @user_serializer.serializable_hash)
|
}, @user_serializer.serializable_hash)
|
||||||
|
ensure
|
||||||
|
@association.key = old_key
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_associations_embedding_objects_serialization_using_serializable_hash
|
def test_associations_embedding_objects_serialization_using_serializable_hash
|
||||||
@ -54,10 +55,13 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options
|
def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options
|
||||||
@association.embed = :objects
|
@association.embed = :objects
|
||||||
@association.options[:root] = 'root'
|
old_embedded_key = @association.embedded_key
|
||||||
|
@association.embedded_key = 'root'
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'name' => 'Name 1', 'email' => 'mail@server.com', 'root' => [{ 'name' => 'N1', 'description' => 'D1' }]
|
'name' => 'Name 1', 'email' => 'mail@server.com', 'root' => [{ 'name' => 'N1', 'description' => 'D1' }]
|
||||||
}, @user_serializer.serializable_hash)
|
}, @user_serializer.serializable_hash)
|
||||||
|
ensure
|
||||||
|
@association.embedded_key = old_embedded_key
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
|
def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user