mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Allow to set key for associations through options
This commit is contained in:
parent
48590a2e37
commit
280fd65db8
@ -45,7 +45,7 @@ module ActiveModel
|
|||||||
|
|
||||||
class HasOne < Association
|
class HasOne < Association
|
||||||
def key
|
def key
|
||||||
"#{name}_id"
|
@options[:key] || "#{name}_id"
|
||||||
end
|
end
|
||||||
|
|
||||||
def embedded_key
|
def embedded_key
|
||||||
@ -55,7 +55,7 @@ module ActiveModel
|
|||||||
|
|
||||||
class HasMany < Association
|
class HasMany < Association
|
||||||
def key
|
def key
|
||||||
"#{name.singularize}_ids"
|
@options[:key] || "#{name.singularize}_ids"
|
||||||
end
|
end
|
||||||
|
|
||||||
def embedded_key
|
def embedded_key
|
||||||
|
|||||||
@ -10,6 +10,7 @@ module ActiveModel
|
|||||||
@association.include = false
|
@association.include = false
|
||||||
@association.embed = :ids
|
@association.embed = :ids
|
||||||
@association.options[:root] = nil
|
@association.options[:root] = nil
|
||||||
|
@association.options[:key] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_associations_definition
|
def test_associations_definition
|
||||||
@ -30,6 +31,13 @@ module ActiveModel
|
|||||||
}, @post_serializer.as_json)
|
}, @post_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options
|
||||||
|
@association.options[:key] = 'key'
|
||||||
|
assert_equal({
|
||||||
|
'title' => 'Title 1', 'body' => 'Body 1', 'key' => @post.comments.map { |c| c.object_id }
|
||||||
|
}, @post_serializer.serializable_hash)
|
||||||
|
end
|
||||||
|
|
||||||
def test_associations_embedding_objects_serialization_using_serializable_hash
|
def test_associations_embedding_objects_serialization_using_serializable_hash
|
||||||
@association.embed = :objects
|
@association.embed = :objects
|
||||||
assert_equal({
|
assert_equal({
|
||||||
|
|||||||
@ -10,6 +10,7 @@ module ActiveModel
|
|||||||
@association.include = false
|
@association.include = false
|
||||||
@association.embed = :ids
|
@association.embed = :ids
|
||||||
@association.options[:root] = nil
|
@association.options[:root] = nil
|
||||||
|
@association.options[:key] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_associations_definition
|
def test_associations_definition
|
||||||
@ -30,6 +31,13 @@ module ActiveModel
|
|||||||
}, @user_serializer.as_json)
|
}, @user_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options
|
||||||
|
@association.options[:key] = 'key'
|
||||||
|
assert_equal({
|
||||||
|
'name' => 'Name 1', 'email' => 'mail@server.com', 'key' => @user.profile.object_id
|
||||||
|
}, @user_serializer.serializable_hash)
|
||||||
|
end
|
||||||
|
|
||||||
def test_associations_embedding_objects_serialization_using_serializable_hash
|
def test_associations_embedding_objects_serialization_using_serializable_hash
|
||||||
@association.embed = :objects
|
@association.embed = :objects
|
||||||
assert_equal({
|
assert_equal({
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user