Skip to content

Instantly share code, notes, and snippets.

@tonywok
Created April 2, 2014 17:37
Show Gist options
  • Select an option

  • Save tonywok/9939074 to your computer and use it in GitHub Desktop.

Select an option

Save tonywok/9939074 to your computer and use it in GitHub Desktop.
Hacking polymorphic has_many into AMS. (e.g has_many :tasks, embed: :ids, include: true, polymorphic: true)
module ActiveModel
class Serializer
module Associations
class HasMany
def key_with_polymorphism
return @name if !option(:key) && option(:polymorphic)
key_without_polymorphism
end
alias_method_chain :key, :polymorphism
def serialize_ids_with_polymorphism
return associated_object.map do |item|
{id: item.id, type: item.type}
end if option(:polymorphic)
serialize_ids_without_polymorphism
end
alias_method_chain :serialize_ids, :polymorphism
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment