mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 22:36:50 +00:00
Implement has_one's embed ids include true
This commit is contained in:
parent
52bb3f6929
commit
516f5bdceb
@ -74,7 +74,8 @@ module ActiveModel
|
|||||||
self.class._associations.each_with_object({}) do |association, hash|
|
self.class._associations.each_with_object({}) do |association, hash|
|
||||||
if association.embed_ids?
|
if association.embed_ids?
|
||||||
hash[association.key] = serialize_ids association
|
hash[association.key] = serialize_ids association
|
||||||
elsif association.embed_objects?
|
end
|
||||||
|
if association.embed_objects?
|
||||||
associated_data = send(association.name)
|
associated_data = send(association.name)
|
||||||
hash[association.embedded_key] = association.build_serializer(associated_data).serializable_hash
|
hash[association.embedded_key] = association.build_serializer(associated_data).serializable_hash
|
||||||
end
|
end
|
||||||
|
|||||||
@ -13,7 +13,8 @@ module ActiveModel
|
|||||||
@serializer_class = serializer.is_a?(String) ? serializer.constantize : serializer
|
@serializer_class = serializer.is_a?(String) ? serializer.constantize : serializer
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :name, :embed_ids, :embed_objects, :embed_key, :include
|
attr_reader :name, :embed_ids, :embed_objects, :embed_key
|
||||||
|
attr_accessor :include
|
||||||
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
|
||||||
@ -23,6 +24,10 @@ module ActiveModel
|
|||||||
@embed_objects = embed == :object || embed == :objects
|
@embed_objects = embed == :object || embed == :objects
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def embed_objects?
|
||||||
|
@embed_objects || @embed_ids && @include
|
||||||
|
end
|
||||||
|
|
||||||
def build_serializer(object)
|
def build_serializer(object)
|
||||||
@serializer_class ||= Serializer.serializer_for(object)
|
@serializer_class ||= Serializer.serializer_for(object)
|
||||||
|
|
||||||
|
|||||||
@ -7,6 +7,8 @@ module ActiveModel
|
|||||||
def setup
|
def setup
|
||||||
@model = ::Model.new({ :attr1 => 'value1', :attr2 => 'value2', :attr3 => 'value3' })
|
@model = ::Model.new({ :attr1 => 'value1', :attr2 => 'value2', :attr3 => 'value3' })
|
||||||
@model_serializer = AnotherSerializer.new(@model)
|
@model_serializer = AnotherSerializer.new(@model)
|
||||||
|
@model_serializer.class._associations[0].include = false
|
||||||
|
@model_serializer.class._associations[0].embed = :ids
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_associations_definition
|
def test_associations_definition
|
||||||
@ -18,14 +20,12 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_associations_embedding_ids_serialization_using_serializable_hash
|
def test_associations_embedding_ids_serialization_using_serializable_hash
|
||||||
@model_serializer.class._associations[0].embed = :ids
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'attr2' => 'value2', 'attr3' => 'value3', 'model_id' => @model.model.object_id
|
'attr2' => 'value2', 'attr3' => 'value3', 'model_id' => @model.model.object_id
|
||||||
}, @model_serializer.serializable_hash)
|
}, @model_serializer.serializable_hash)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_associations_embedding_ids_serialization_using_as_json
|
def test_associations_embedding_ids_serialization_using_as_json
|
||||||
@model_serializer.class._associations[0].embed = :ids
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'attr2' => 'value2', 'attr3' => 'value3', 'model_id' => @model.model.object_id
|
'attr2' => 'value2', 'attr3' => 'value3', 'model_id' => @model.model.object_id
|
||||||
}, @model_serializer.as_json)
|
}, @model_serializer.as_json)
|
||||||
@ -44,6 +44,20 @@ module ActiveModel
|
|||||||
'attr2' => 'value2', 'attr3' => 'value3', 'model' => { 'attr1' => 'v1', 'attr2' => 'v2' }
|
'attr2' => 'value2', 'attr3' => 'value3', 'model' => { 'attr1' => 'v1', 'attr2' => 'v2' }
|
||||||
}, @model_serializer.as_json)
|
}, @model_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
|
||||||
|
@model_serializer.class._associations[0].include = true
|
||||||
|
assert_equal({
|
||||||
|
'attr2' => 'value2', 'attr3' => 'value3', 'model_id' => @model.model.object_id, 'model' => { 'attr1' => 'v1', 'attr2' => 'v2' }
|
||||||
|
}, @model_serializer.serializable_hash)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_associations_embedding_ids_including_objects_serialization_using_as_json
|
||||||
|
@model_serializer.class._associations[0].include = true
|
||||||
|
assert_equal({
|
||||||
|
'attr2' => 'value2', 'attr3' => 'value3', 'model_id' => @model.model.object_id, 'model' => { 'attr1' => 'v1', 'attr2' => 'v2' }
|
||||||
|
}, @model_serializer.as_json)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user