Created
July 4, 2011 16:13
-
-
Save d11wtq/1063558 to your computer and use it in GitHub Desktop.
Workaround for dm-rest-adapter serialization to JSON with :field and foreign keys
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
# This is used temporarily, until serialization to :raw is done in dm-serializer | |
module DataMapper | |
module Rest | |
class CustomJsonFormat < DataMapperRest::Format::Json | |
def string_representation(resource) | |
model = resource.model | |
hash = {} | |
hash = model.properties.reduce(hash) do |h, property| | |
h.merge(property.field.to_sym => property.dump(property.get(resource))) | |
end | |
hash = model.relationships.reject{ |r| r.source_key == model.key }.reduce(hash) do |h, relationship| | |
keys_hash = relationship.source_key.reduce({}) do |kh, key| | |
kh.merge(key.field.to_sym => key.dump(key.get(resource))) | |
end | |
h.merge(keys_hash) | |
end | |
ActiveSupport::JSON.encode(hash) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment