Skip to content

Commit

Permalink
make shouldSerializeHasMany public #2494 (#4279)
Browse files Browse the repository at this point in the history
  • Loading branch information
jondayton authored and bmac committed Jun 2, 2016
1 parent da342e6 commit e4758d3
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
4 changes: 4 additions & 0 deletions FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,7 @@ entry in `config/features.json`.
}
```
- `ds-check-should-serialize-relationships`
Adds public method for `shouldSerializeHasMany`, used to determine if a
`hasMany` relationship can be serialized.
6 changes: 5 additions & 1 deletion addon/serializers/json-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,12 @@ const JSONAPISerializer = JSONSerializer.extend({
*/
serializeHasMany(snapshot, json, relationship) {
var key = relationship.key;
var shouldSerializeHasMany = '_shouldSerializeHasMany';
if (isEnabled("ds-check-should-serialize-relationships")) {
shouldSerializeHasMany = 'shouldSerializeHasMany';
}

if (this._shouldSerializeHasMany(snapshot, key, relationship)) {
if (this[shouldSerializeHasMany](snapshot, key, relationship)) {
var hasMany = snapshot.hasMany(key);
if (hasMany !== undefined) {

Expand Down
27 changes: 26 additions & 1 deletion addon/serializers/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,27 @@ var JSONSerializer = Serializer.extend({
return attrs && attrs[key] && attrs[key].serialize === true;
},

/**
Check if the given hasMany relationship should be serialized
@method shouldSerializeHasMany
@param {DS.Snapshot} snapshot
@param {String} key
@param {String} relationshipType
@return {boolean} true if the hasMany relationship should be serialized
*/

shouldSerializeHasMany(snapshot, key, relationship) {
if ((this._shouldSerializeHasMany !== JSONSerializer.prototype._shouldSerializeHasMany)) {
deprecate('The private method _shouldSerializeHasMany has been promoted to the public API. Please remove the underscore to use the public shouldSerializeHasMany method.', false, {
id: 'ds.serializer.private-should-serialize-has-many',
until: '3.0.0'
});
}

return this._shouldSerializeHasMany(snapshot, key, relationship);
},

/**
Check if the given hasMany relationship should be serialized
Expand Down Expand Up @@ -1196,8 +1217,12 @@ var JSONSerializer = Serializer.extend({
*/
serializeHasMany(snapshot, json, relationship) {
var key = relationship.key;
var shouldSerializeHasMany = '_shouldSerializeHasMany';
if (isEnabled("ds-check-should-serialize-relationships")) {
shouldSerializeHasMany = 'shouldSerializeHasMany';
}

if (this._shouldSerializeHasMany(snapshot, key, relationship)) {
if (this[shouldSerializeHasMany](snapshot, key, relationship)) {
var hasMany = snapshot.hasMany(key, { ids: true });
if (hasMany !== undefined) {
// if provided, use the mapping provided by `attrs` in
Expand Down
3 changes: 2 additions & 1 deletion config/features.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"ds-extended-errors": null,
"ds-links-in-record-array": null,
"ds-overhaul-references": null,
"ds-payload-type-hooks": null
"ds-payload-type-hooks": null,
"ds-check-should-serialize-relationships": null
}
26 changes: 26 additions & 0 deletions tests/integration/serializers/json-serializer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,32 @@ test("serializeHasMany omits unknown relationships on pushed record", function(a
assert.ok(!json.hasOwnProperty("comments"), "Does not add the relationship key to json");
});

test("shouldSerializeHasMany", function(assert) {

run(function() {
post = env.store.createRecord('post', { title: "Rails is omakase", id: "1" });
comment = env.store.createRecord('comment', { body: "Omakase is delicious", post: post, id: "1" });
});

var snapshot = post._createSnapshot();
var relationship = snapshot.record.relationshipFor('comments');
var key = relationship.key;

var shouldSerialize = env.store.serializerFor("post").shouldSerializeHasMany(snapshot, relationship, key);

assert.ok(shouldSerialize, 'shouldSerializeHasMany correctly identifies with hasMany relationship');
});

if (isEnabled("ds-check-should-serialize-relationships")) {
testInDebug("_shouldSerializeHasMany deprecation", function(assert) {
env.store.serializerFor("post")._shouldSerializeHasMany = Ember.K;

assert.expectDeprecation(function() {
env.store.serializerFor("post").shouldSerializeHasMany();
}, /_shouldSerializeHasMany has been promoted to the public API/);
});
}

test("serializeIntoHash", function(assert) {
run(function() {
post = env.store.createRecord('post', { title: "Rails is omakase" });
Expand Down

0 comments on commit e4758d3

Please sign in to comment.