kurko / ember-json-api

JSON API adapter for Ember Data.
MIT License
254 stars 62 forks source link

use keyForRelationship to convert keys for relationships in serializeBel... #20

Closed denisnazarov closed 10 years ago

denisnazarov commented 10 years ago

...ongsTo and serializeHasMany

denisnazarov commented 10 years ago

cc @stefanpenner

gr0uch commented 10 years ago

I am a little confused, the keyForRelationship method just returns the argument, nothing else?

denisnazarov commented 10 years ago

Hey @daliwali,

It should be more clear in this test: https://github.com/plyfe/ember-json-api/blob/convert-keys/tests/integration/serializer_test.js#L100-L106

One can choose to define keyForAttribute/keyForRelationship on the serializer to convert the camelized keys to snake_cased ones, for example to use with a Rails backend that expects snakecased.

This PR fixes serialization of belongsTo/hasMany, which previously wouldn't call keyForRelationship. This would result in part of the attributes being properly converted, and the ones that represent relationships would remain camelcased.

The PR is based on code in ActiveModelAdapter: https://github.com/emberjs/data/blob/master/packages/activemodel-adapter/lib/system/embedded_records_mixin.js#L146

keyForRelationship returns the argument as a default if you choose not to overrride keyForRelationship in your serializer, otherwise we would get function undefined if it wasn't implemented.

Also see: https://github.com/emberjs/data/blob/master/packages/activemodel-adapter/lib/system/active_model_serializer.js#L123-L132