Last active
January 12, 2016 21:38
-
-
Save joshskeen/3fed3921efb8e974b913 to your computer and use it in GitHub Desktop.
how to use a plain old ruby object PORO and an active_model_serializers Serializer (0.10.x)
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
ruby object: | |
class SearchResult | |
extend ActiveModel::Naming | |
include ActiveModel::Serialization | |
attr_accessor :stories, :users, :friends | |
def id | |
hash | |
end | |
def initialize(search:) | |
@stories = search.stories | |
@users = search.users | |
@friends = search.friends | |
end | |
def attributes | |
{ stories: stories, | |
users: users, | |
friends: friends | |
} | |
end | |
end | |
Serializer: | |
class SearchResultSerializer < ActiveModel::Serializer | |
attributes :stories, :users, :friends | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment