mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 06:46:50 +00:00
Merge pull request #273 from seanabrahams/embed-ids-not-using-local-method
Fixes #267
This commit is contained in:
commit
c1dacccbdc
@ -133,7 +133,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def serialize_ids
|
def serialize_ids
|
||||||
ids_key = "#{@name.to_s.singularize}_ids".to_sym
|
ids_key = "#{@name.to_s.singularize}_ids".to_sym
|
||||||
if !option(:embed_key) && source_serializer.object.respond_to?(ids_key)
|
if !option(:embed_key) && !source_serializer.respond_to?(@name.to_s) && source_serializer.object.respond_to?(ids_key)
|
||||||
source_serializer.object.read_attribute_for_serialization(ids_key)
|
source_serializer.object.read_attribute_for_serialization(ids_key)
|
||||||
else
|
else
|
||||||
associated_object.map do |item|
|
associated_object.map do |item|
|
||||||
@ -219,7 +219,7 @@ module ActiveModel
|
|||||||
else
|
else
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
elsif !option(:embed_key) && source_serializer.object.respond_to?(id_key)
|
elsif !option(:embed_key) && !source_serializer.respond_to?(@name.to_s) && source_serializer.object.respond_to?(id_key)
|
||||||
source_serializer.object.read_attribute_for_serialization(id_key)
|
source_serializer.object.read_attribute_for_serialization(id_key)
|
||||||
elsif associated_object
|
elsif associated_object
|
||||||
associated_object.read_attribute_for_serialization(embed_key)
|
associated_object.read_attribute_for_serialization(embed_key)
|
||||||
|
|||||||
@ -398,8 +398,6 @@ class AssociationTest < ActiveModel::TestCase
|
|||||||
|
|
||||||
include_bare! :comment
|
include_bare! :comment
|
||||||
|
|
||||||
assert_equal :comment_id, association_name
|
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
:comment_id => 1
|
:comment_id => 1
|
||||||
}, @hash)
|
}, @hash)
|
||||||
@ -423,8 +421,6 @@ class AssociationTest < ActiveModel::TestCase
|
|||||||
|
|
||||||
include_bare! :comments
|
include_bare! :comments
|
||||||
|
|
||||||
assert_equal :comment_ids, association_name
|
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
:comment_ids => [1]
|
:comment_ids => [1]
|
||||||
}, @hash)
|
}, @hash)
|
||||||
|
|||||||
@ -417,6 +417,33 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
}, serializer.as_json)
|
}, serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_methods_take_priority_over_associations
|
||||||
|
post_serializer = Class.new(ActiveModel::Serializer) do
|
||||||
|
attributes :title
|
||||||
|
has_many :comments
|
||||||
|
embed :ids
|
||||||
|
|
||||||
|
def comments
|
||||||
|
object.comments[0,1]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
post = Post.new(title: "My Post")
|
||||||
|
comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)]
|
||||||
|
post.comments = comments
|
||||||
|
|
||||||
|
post.class_eval do
|
||||||
|
define_method :comment_ids, lambda {
|
||||||
|
self.comments.map { |c| c.read_attribute_for_serialization(:id) }
|
||||||
|
}
|
||||||
|
end
|
||||||
|
json = post_serializer.new(post).as_json
|
||||||
|
assert_equal({
|
||||||
|
title: "My Post",
|
||||||
|
comment_ids: [1]
|
||||||
|
}, json)
|
||||||
|
end
|
||||||
|
|
||||||
def test_embed_objects
|
def test_embed_objects
|
||||||
serializer = post_serializer
|
serializer = post_serializer
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user