Skip to content

Commit

Permalink
Merge pull request #4350 from pangratz/cleanup-ds-transform-pass-options
Browse files Browse the repository at this point in the history
[CLEANUP ds-transform-pass-options]
  • Loading branch information
bmac committed Apr 29, 2016
2 parents 3d974d8 + f5df246 commit 71abed3
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 110 deletions.
7 changes: 0 additions & 7 deletions FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,12 @@ entry in `config/features.json`.

Allow `null`/`undefined` values for `boolean` attributes via `DS.attr('boolean', { allowNull: true })`

Note that this feature only works when `ds-transform-pass-options` is enabled too.

- `ds-improved-ajax`

This feature allows to customize how a request is formed by overwriting
`methodForRequest`, `urlForRequest`, `headersForRequest` and `bodyForRequest`
in the `DS.RESTAdapter`.

- `ds-transform-pass-options`

Pass options specified for a `DS.attr` to the `DS.Tranform`'s `serialize` and
`deserialize` methods (described in [RFC 1](https://github.com/emberjs/rfcs/pull/1))

- `ds-pushpayload-return`

Enables `pushPayload` to return the model(s) that are created or
Expand Down
16 changes: 6 additions & 10 deletions addon/-private/transforms/boolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ export default Transform.extend({
deserialize(serialized, options) {
var type = typeof serialized;

if (isEnabled('ds-transform-pass-options')) {
if (isEnabled('ds-boolean-transform-allow-null')) {
if (isNone(serialized) && options.allowNull === true) {
return null;
}
if (isEnabled('ds-boolean-transform-allow-null')) {
if (isNone(serialized) && options.allowNull === true) {
return null;
}
}

Expand All @@ -50,11 +48,9 @@ export default Transform.extend({
},

serialize(deserialized, options) {
if (isEnabled('ds-transform-pass-options')) {
if (isEnabled('ds-boolean-transform-allow-null')) {
if (isNone(deserialized) && options.allowNull === true) {
return null;
}
if (isEnabled('ds-boolean-transform-allow-null')) {
if (isNone(deserialized) && options.allowNull === true) {
return null;
}
}

Expand Down
60 changes: 28 additions & 32 deletions addon/attr.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,34 @@ function getValue(record, key) {
});
```
The `options` hash is passed as second argument to a transforms'
`serialize` and `deserialize` method. This allows to configure a
transformation and adapt the corresponding value, based on the config:
```app/models/post.js
export default DS.Model.extend({
text: DS.attr('text', {
uppercase: true
})
});
```
```app/transforms/text.js
export default DS.Transform.extend({
serialize: function(value, options) {
if (options.uppercase) {
return value.toUpperCase();
}
return value;
},
deserialize: function(value) {
return value;
}
})
```
@namespace
@method attr
@for DS
Expand Down Expand Up @@ -130,35 +158,3 @@ export default function attr(type, options) {
}
}).meta(meta);
}

// TODO add to documentation of `attr` function above, once this feature is added
// /**
// * The `options` hash is passed as second argument to a transforms'
// * `serialize` and `deserialize` method. This allows to configure a
// * transformation and adapt the corresponding value, based on the config:
// *
// * ```app/models/post.js
// * export default DS.Model.extend({
// * text: DS.attr('text', {
// * uppercase: true
// * })
// * });
// * ```
// *
// * ```app/transforms/text.js
// * export default DS.Transform.extend({
// * serialize: function(value, options) {
// * if (options.uppercase) {
// * return value.toUpperCase();
// * }
// *
// * return value;
// * },
// *
// * deserialize: function(value) {
// * return value;
// * }
// * })
// * ```
// *
// */
21 changes: 4 additions & 17 deletions addon/serializers/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import {

import { errorsArrayToHash } from "ember-data/adapters/errors";

import isEnabled from 'ember-data/-private/features';

var get = Ember.get;
var isNone = Ember.isNone;
var assign = Ember.assign || Ember.merge;
Expand Down Expand Up @@ -187,21 +185,14 @@ export default Serializer.extend({
@return {Object} data The transformed data object
*/
applyTransforms(typeClass, data) {
let attributes;
if (isEnabled('ds-transform-pass-options')) {
attributes = get(typeClass, 'attributes');
}
let attributes = get(typeClass, 'attributes');

typeClass.eachTransformedAttribute((key, typeClass) => {
if (!(key in data)) { return; }

var transform = this.transformFor(typeClass);
if (isEnabled('ds-transform-pass-options')) {
var transformMeta = attributes.get(key);
data[key] = transform.deserialize(data[key], transformMeta.options);
} else {
data[key] = transform.deserialize(data[key]);
}
var transformMeta = attributes.get(key);
data[key] = transform.deserialize(data[key], transformMeta.options);
});

return data;
Expand Down Expand Up @@ -1089,11 +1080,7 @@ export default Serializer.extend({
var value = snapshot.attr(key);
if (type) {
var transform = this.transformFor(type);
if (isEnabled('ds-transform-pass-options')) {
value = transform.serialize(value, attribute.options);
} else {
value = transform.serialize(value);
}
value = transform.serialize(value, attribute.options);
}

// if provided, use the mapping provided by `attrs` in
Expand Down
6 changes: 4 additions & 2 deletions addon/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ export default Ember.Object.extend({
Example
```javascript
serialize: function(deserialized) {
serialize: function(deserialized, options) {
return Ember.isEmpty(deserialized) ? null : Number(deserialized);
}
```
@method serialize
@param deserialized The deserialized value
@param options hash of options passed to `DS.attr`
@return The serialized value
*/
serialize: null,
Expand All @@ -63,13 +64,14 @@ export default Ember.Object.extend({
Example
```javascript
deserialize: function(serialized) {
deserialize: function(serialized, options) {
return empty(serialized) ? null : Number(serialized);
}
```
@method deserialize
@param serialized The serialized value
@param options hash of options passed to `DS.attr`
@return The deserialized value
*/
deserialize: null
Expand Down
1 change: 0 additions & 1 deletion config/features.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"ds-boolean-transform-allow-null": null,
"ds-improved-ajax": null,
"ds-transform-pass-options": true,
"ds-pushpayload-return": null,
"ds-extended-errors": null,
"ds-links-in-record-array": null
Expand Down
70 changes: 32 additions & 38 deletions tests/integration/serializers/json-serializer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import {module, test} from 'qunit';

import DS from 'ember-data';

import isEnabled from 'ember-data/-private/features';

var Post, post, Comment, comment, Favorite, favorite, env;
var run = Ember.run;

Expand Down Expand Up @@ -907,52 +905,48 @@ test('normalizeResponse ignores unmapped attributes', function(assert) {
assert.equal(post.data.attributes.title, "Rails is omakase");
});

if (isEnabled('ds-transform-pass-options')) {

test('options are passed to transform for serialization', function(assert) {
assert.expect(1);

env.registry.register('transform:custom', DS.Transform.extend({
serialize: function(deserialized, options) {
assert.deepEqual(options, { custom: 'config' });
}
}));
test('options are passed to transform for serialization', function(assert) {
assert.expect(1);

Post.reopen({
custom: DS.attr('custom', {
custom: 'config'
})
});
env.registry.register('transform:custom', DS.Transform.extend({
serialize: function(deserialized, options) {
assert.deepEqual(options, { custom: 'config' });
}
}));

var post;
run(function() {
post = env.store.createRecord('post', { custom: 'value' });
});
Post.reopen({
custom: DS.attr('custom', {
custom: 'config'
})
});

env.serializer.serialize(post._createSnapshot());
var post;
run(function() {
post = env.store.createRecord('post', { custom: 'value' });
});

test('options are passed to transform for normalization', function(assert) {
assert.expect(1);
env.serializer.serialize(post._createSnapshot());
});

env.registry.register('transform:custom', DS.Transform.extend({
deserialize: function(serialized, options) {
assert.deepEqual(options, { custom: 'config' });
}
}));
test('options are passed to transform for normalization', function(assert) {
assert.expect(1);

Post.reopen({
custom: DS.attr('custom', {
custom: 'config'
})
});
env.registry.register('transform:custom', DS.Transform.extend({
deserialize: function(serialized, options) {
assert.deepEqual(options, { custom: 'config' });
}
}));

env.serializer.normalize(Post, {
custom: 'value'
});
Post.reopen({
custom: DS.attr('custom', {
custom: 'config'
})
});

}
env.serializer.normalize(Post, {
custom: 'value'
});
});

test('Serializer should respect the attrs hash in links', function(assert) {
env.registry.register("serializer:post", DS.JSONSerializer.extend({
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/model-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ test("a DS.Model can describe Boolean attributes", function(assert) {
assert.converts('boolean', 1, true);
assert.converts('boolean', 0, false);

if (isEnabled('ds-transform-pass-options') && isEnabled('ds-boolean-transform-allow-null')) {
if (isEnabled('ds-boolean-transform-allow-null')) {
assert.converts('boolean', null, null, { allowNull: true });
assert.converts('boolean', undefined, null, { allowNull: true });

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/transform/boolean-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module("unit/transform - DS.BooleanTransform");
test("#serialize", function(assert) {
var transform = new DS.BooleanTransform();

if (isEnabled('ds-transform-pass-options') && isEnabled('ds-boolean-transform-allow-null')) {
if (isEnabled('ds-boolean-transform-allow-null')) {
assert.equal(transform.serialize(null, { allowNull: true }), null);
assert.equal(transform.serialize(undefined, { allowNull: true }), null);

Expand All @@ -29,7 +29,7 @@ test("#serialize", function(assert) {
test("#deserialize", function(assert) {
var transform = new DS.BooleanTransform();

if (isEnabled('ds-transform-pass-options') && isEnabled('ds-boolean-transform-allow-null')) {
if (isEnabled('ds-boolean-transform-allow-null')) {
assert.equal(transform.deserialize(null, { allowNull: true }), null);
assert.equal(transform.deserialize(undefined, { allowNull: true }), null);

Expand Down

0 comments on commit 71abed3

Please sign in to comment.