Created
April 2, 2014 17:37
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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