From a771f3081607774205f244f13e1e6a6eeaf79b53 Mon Sep 17 00:00:00 2001 From: Sean Abrahams Date: Wed, 10 Apr 2013 14:36:42 -0700 Subject: [PATCH 1/2] If output is as expected, is this assert important? --- test/association_test.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/association_test.rb b/test/association_test.rb index b106de14..2cfbd961 100644 --- a/test/association_test.rb +++ b/test/association_test.rb @@ -398,8 +398,6 @@ class AssociationTest < ActiveModel::TestCase include_bare! :comment - assert_equal :comment_id, association_name - assert_equal({ :comment_id => 1 }, @hash) @@ -423,8 +421,6 @@ class AssociationTest < ActiveModel::TestCase include_bare! :comments - assert_equal :comment_ids, association_name - assert_equal({ :comment_ids => [1] }, @hash) From 79acd87829ca109ff6f93d5b213b9ea43989610c Mon Sep 17 00:00:00 2001 From: Sean Abrahams Date: Wed, 10 Apr 2013 14:38:53 -0700 Subject: [PATCH 2/2] Use method instead of asssociation_ids if method exists. Fixes #267 --- lib/active_model/serializer/associations.rb | 4 +-- test/serializer_test.rb | 27 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/active_model/serializer/associations.rb b/lib/active_model/serializer/associations.rb index 33b66ade..46606342 100644 --- a/lib/active_model/serializer/associations.rb +++ b/lib/active_model/serializer/associations.rb @@ -133,7 +133,7 @@ module ActiveModel def serialize_ids 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) else associated_object.map do |item| @@ -219,7 +219,7 @@ module ActiveModel else nil 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) elsif associated_object associated_object.read_attribute_for_serialization(embed_key) diff --git a/test/serializer_test.rb b/test/serializer_test.rb index b45e270c..9ad5b422 100644 --- a/test/serializer_test.rb +++ b/test/serializer_test.rb @@ -406,6 +406,33 @@ class SerializerTest < ActiveModel::TestCase }, serializer.as_json) 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 serializer = post_serializer