Skip to content

Instantly share code, notes, and snippets.

@d11wtq
Created July 4, 2011 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d11wtq/1063558 to your computer and use it in GitHub Desktop.
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 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