Skip to content

Commit

Permalink
Merge pull request #4086 from pangratz/ds-transform-pass-options
Browse files Browse the repository at this point in the history
[FEATURE ds-transform-pass-options] pass options to DS.Transform
  • Loading branch information
igorT committed Jan 27, 2016
2 parents eb83d5c + 9ab5c5e commit 8b0e53c
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 3 deletions.
5 changes: 5 additions & 0 deletions FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ entry in `config/features.json`.
- `ds-references`

Adds references as described in [RFC 57](https://github.com/emberjs/rfcs/pull/57)

- `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))
32 changes: 32 additions & 0 deletions addon/attr.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,35 @@ 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;
// * }
// * })
// * ```
// *
// */
20 changes: 18 additions & 2 deletions addon/serializers/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {

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

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

var get = Ember.get;
var isNone = Ember.isNone;
var merge = Ember.merge;
Expand Down Expand Up @@ -185,11 +187,21 @@ 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');
}

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

var transform = this.transformFor(typeClass);
data[key] = transform.deserialize(data[key]);
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]);
}
});

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

// 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
@@ -1,4 +1,5 @@
{
"ds-finder-include": null,
"ds-references": null
"ds-references": null,
"ds-transform-pass-options": null
}
49 changes: 49 additions & 0 deletions tests/integration/serializers/json-serializer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ 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 @@ -904,3 +906,50 @@ 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' });
}
}));

Post.reopen({
custom: DS.attr('custom', {
custom: 'config'
})
});

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

env.serializer.serialize(post._createSnapshot());
});

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

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

Post.reopen({
custom: DS.attr('custom', {
custom: 'config'
})
});

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

}

0 comments on commit 8b0e53c

Please sign in to comment.