From f5df24677fb7a9ca265d636f38c6e6d959f936f6 Mon Sep 17 00:00:00 2001 From: pangratz Date: Thu, 21 Apr 2016 16:16:17 +0200 Subject: [PATCH] [CLEANUP ds-transform-pass-options] --- FEATURES.md | 7 -- addon/-private/transforms/boolean.js | 16 ++--- addon/attr.js | 60 ++++++++-------- addon/serializers/json.js | 21 ++---- addon/transform.js | 6 +- config/features.json | 1 - .../serializers/json-serializer-test.js | 70 +++++++++---------- tests/unit/model-test.js | 2 +- tests/unit/transform/boolean-test.js | 4 +- 9 files changed, 77 insertions(+), 110 deletions(-) diff --git a/FEATURES.md b/FEATURES.md index a3904ed37eb..8fcdc171391 100644 --- a/FEATURES.md +++ b/FEATURES.md @@ -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 diff --git a/addon/-private/transforms/boolean.js b/addon/-private/transforms/boolean.js index 9aabfaa6683..e9724eaf1ed 100644 --- a/addon/-private/transforms/boolean.js +++ b/addon/-private/transforms/boolean.js @@ -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; } } @@ -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; } } diff --git a/addon/attr.js b/addon/attr.js index 87a95d3615e..673fb7ee429 100644 --- a/addon/attr.js +++ b/addon/attr.js @@ -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 @@ -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; -// * } -// * }) -// * ``` -// * -// */ diff --git a/addon/serializers/json.js b/addon/serializers/json.js index 37fdf3da119..caee949ba49 100644 --- a/addon/serializers/json.js +++ b/addon/serializers/json.js @@ -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; @@ -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; @@ -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 diff --git a/addon/transform.js b/addon/transform.js index e9c45f2104a..232c79e5f72 100644 --- a/addon/transform.js +++ b/addon/transform.js @@ -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, @@ -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 diff --git a/config/features.json b/config/features.json index 9ae9b4b894a..1750dc2d709 100644 --- a/config/features.json +++ b/config/features.json @@ -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 diff --git a/tests/integration/serializers/json-serializer-test.js b/tests/integration/serializers/json-serializer-test.js index 64dcd01e07d..521828b9dac 100644 --- a/tests/integration/serializers/json-serializer-test.js +++ b/tests/integration/serializers/json-serializer-test.js @@ -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; @@ -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({ diff --git a/tests/unit/model-test.js b/tests/unit/model-test.js index a79cebb57be..67ed420f3ba 100644 --- a/tests/unit/model-test.js +++ b/tests/unit/model-test.js @@ -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 }); diff --git a/tests/unit/transform/boolean-test.js b/tests/unit/transform/boolean-test.js index aa257be005c..32c6a69d057 100644 --- a/tests/unit/transform/boolean-test.js +++ b/tests/unit/transform/boolean-test.js @@ -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); @@ -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);