Created
December 18, 2017 07:44
-
-
Save Zooip/3f22978429b3d76f1ab5962ab115b675 to your computer and use it in GitHub Desktop.
JsonAPI-rb Devourable Serializable
This file contains 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
export const song_attributes = <%= SerializableSong.devour_attributes.to_json %> | |
export const song_document_attributes = <%= SerializableSongDocument.devour_attributes.to_json %> |
This file contains 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 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