mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16: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
|
||||
def key
|
||||
"#{name}_id"
|
||||
@options[:key] || "#{name}_id"
|
||||
end
|
||||
|
||||
def embedded_key
|
||||
@ -55,7 +55,7 @@ module ActiveModel
|
||||
|
||||
class HasMany < Association
|
||||
def key
|
||||
"#{name.singularize}_ids"
|
||||
@options[:key] || "#{name.singularize}_ids"
|
||||
end
|
||||
|
||||
def embedded_key
|
||||
|
||||
@ -10,6 +10,7 @@ module ActiveModel
|
||||
@association.include = false
|
||||
@association.embed = :ids
|
||||
@association.options[:root] = nil
|
||||
@association.options[:key] = nil
|
||||
end
|
||||
|
||||
def test_associations_definition
|
||||
@ -30,6 +31,13 @@ module ActiveModel
|
||||
}, @post_serializer.as_json)
|
||||
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
|
||||
@association.embed = :objects
|
||||
assert_equal({
|
||||
|
||||
@ -10,6 +10,7 @@ module ActiveModel
|
||||
@association.include = false
|
||||
@association.embed = :ids
|
||||
@association.options[:root] = nil
|
||||
@association.options[:key] = nil
|
||||
end
|
||||
|
||||
def test_associations_definition
|
||||
@ -30,6 +31,13 @@ module ActiveModel
|
||||
}, @user_serializer.as_json)
|
||||
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
|
||||
@association.embed = :objects
|
||||
assert_equal({
|
||||
|
||||
Loading…
Reference in New Issue
Block a user