Created
September 4, 2016 19:59
-
-
Save richmolj/961bd59a7ca521c8b80cc7b59ef87e41 to your computer and use it in GitHub Desktop.
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
class ActiveModel::Serializer | |
def initialize(object, options = {}) | |
@object = object | |
@include_directive = options[:include] | |
end | |
def as_json | |
hash = serialize_attributes(@object) | |
self.relationships.each_pair do |name, relationship| | |
related_record = object.send(name) | |
hash[:relationships][name] = relationship.serializer.new(related_record, @include_directive[name]) | |
end | |
hash | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment