Skip to content

Instantly share code, notes, and snippets.

@Zooip
Created December 18, 2017 07:44
Show Gist options
  • Save Zooip/3f22978429b3d76f1ab5962ab115b675 to your computer and use it in GitHub Desktop.
Save Zooip/3f22978429b3d76f1ab5962ab115b675 to your computer and use it in GitHub Desktop.
JsonAPI-rb Devourable Serializable
export const song_attributes = <%= SerializableSong.devour_attributes.to_json %>
export const song_document_attributes = <%= SerializableSongDocument.devour_attributes.to_json %>
class SerializableBase < JSONAPI::Serializable::Resource
def self.relationship_multiplicity
@relationship_multiplicity||={}
end
def self.has_many(name, options = {}, &block)
self.relationship_multiplicity[name.to_sym]=:has_many
super(name, options, &block)
end
def self.has_one(name, options = {}, &block)
self.relationship_multiplicity[name.to_sym]=:has_one
super(name, options, &block)
end
def self.belongs_to(name, options = {}, &block)
self.relationship_multiplicity[name.to_sym]=:has_one
super(name, options, &block)
end
def self.devour_attributes
hash=self.attribute_blocks.keys.map{|k| [k,'']}.to_h
relationship_multiplicity.each do |name, multiplicity|
hash[name]={
jsonApi: multiplicity.to_s.camelize(:lower)
}
end
hash
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment