Skip to content

Commit

Permalink
Merge 57bdfdd into a1755c5
Browse files Browse the repository at this point in the history
  • Loading branch information
tchak committed Mar 7, 2013
2 parents a1755c5 + 57bdfdd commit d324835
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
12 changes: 4 additions & 8 deletions lib/active_model/serializer/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,8 @@ def serializables
end

def serialize_ids
# Use pluck or select_columns if available
# return collection.ids if collection.respond_to?(:ids)
ids_key = "#{key.to_s.singularize}_ids"

if !option(:include) && !option(:embed_key) && source_serializer.object.respond_to?(ids_key)
source_serializer.object.send(ids_key)
if !option(:embed_key) && source_serializer.object.respond_to?(:read_association_ids_for_serialization)
source_serializer.object.read_association_ids_for_serialization(name.to_s.singularize.to_sym)
else
associated_object.map do |item|
item.read_attribute_for_serialization(embed_key)
Expand Down Expand Up @@ -212,8 +208,8 @@ def serialize_ids
else
nil
end
elsif !option(:embed_key) && source_serializer.object.respond_to?("#{name}_id")
source_serializer.object.send("#{name}_id")
elsif !option(:embed_key) && source_serializer.object.respond_to?(:read_association_id_for_serialization)
source_serializer.object.read_association_id_for_serialization(name.to_sym)
elsif associated_object
associated_object.read_attribute_for_serialization(embed_key)
else
Expand Down
48 changes: 48 additions & 0 deletions test/association_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def method_missing(meth, *args)
@attributes[$1.to_sym] = args[0]
elsif @attributes.key?(meth)
@attributes[meth]
elsif meth.to_s =~ /_id$/
@attributes[meth.to_s.gsub(/_id$/, '').to_sym].id
elsif meth.to_s =~ /_ids$/
@attributes[meth.to_s.gsub(/_ids$/, 's').to_sym].map(&:id)
else
super
end
Expand Down Expand Up @@ -379,6 +383,50 @@ def test_embed_ids_include_true_does_not_serialize_multiple_times

assert_equal 1, serialized_times
end

def test_include_with_read_association_id_for_serialization_hook
@post_serializer_class.class_eval do
has_one :comment, :embed => :ids, :include => true
end

association_name = nil
@post.class_eval do
define_method :read_association_id_for_serialization, lambda { |name|
association_name = name
send("#{name}_id")
}
end

include_bare! :comment

assert_equal :comment, association_name

assert_equal({
:comment_id => 1
}, @hash)
end

def test_include_with_read_association_ids_for_serialization_hook
@post_serializer_class.class_eval do
has_many :comments, :embed => :ids, :include => false
end

association_name = nil
@post.class_eval do
define_method :read_association_ids_for_serialization, lambda { |name|
association_name = name
send("#{name}_ids")
}
end

include_bare! :comments

assert_equal :comment, association_name

assert_equal({
:comment_ids => [1]
}, @hash)
end
end

class InclusionTest < AssociationTest
Expand Down

0 comments on commit d324835

Please sign in to comment.