Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE ds-boolean-transform-allow-null] allow null for boolean #4022

Merged
merged 1 commit into from
Mar 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ entry in `config/features.json`.

## Feature Flags

- `ds-boolean-transform-allow-null`

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-finder-include`

Allows an `include` query parameter to be specified with using
Expand Down
24 changes: 22 additions & 2 deletions addon/-private/transforms/boolean.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import Ember from 'ember';
import Transform from "ember-data/transform";
import isEnabled from 'ember-data/-private/features';

const { isNone } = Ember;

/**
The `DS.BooleanTransform` class is used to serialize and deserialize
Expand All @@ -23,9 +27,17 @@ import Transform from "ember-data/transform";
@namespace DS
*/
export default Transform.extend({
deserialize(serialized) {
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use Ember.get(options, 'allowNull') in case options is undefined?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theoretically options should never be undefined because of this and ultimately because of this.

It would only be undefined if the transform is used outside of regular ember-data usage...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good.

return null;
}
}
}

if (type === "boolean") {
return serialized;
} else if (type === "string") {
Expand All @@ -37,7 +49,15 @@ export default Transform.extend({
}
},

serialize(deserialized) {
serialize(deserialized, options) {
if (isEnabled('ds-transform-pass-options')) {
if (isEnabled('ds-boolean-transform-allow-null')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be checking the 2nd argument for { allowNull: true }?

if (isNone(deserialized) && options.allowNull === true) {
return null;
}
}
}

return Boolean(deserialized);
}
});
1 change: 1 addition & 0 deletions config/features.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"ds-boolean-transform-allow-null": null,
"ds-finder-include": null,
"ds-references": null,
"ds-transform-pass-options": null,
Expand Down
24 changes: 19 additions & 5 deletions tests/unit/model-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,9 +822,9 @@ test("when a method is invoked from an event with the same name the arguments ar
assert.equal(eventMethodArgs[1], 2);
});

AssertionPrototype.converts = function converts(type, provided, expected) {
AssertionPrototype.converts = function converts(type, provided, expected, options = {}) {
var Model = DS.Model.extend({
name: DS.attr(type)
name: DS.attr(type, options)
});

var registry, container;
Expand Down Expand Up @@ -930,13 +930,27 @@ test("a DS.Model can describe Number attributes", function(assert) {
});

test("a DS.Model can describe Boolean attributes", function(assert) {
assert.expect(7);

assert.converts('boolean', "1", true);
assert.converts('boolean', "", false);
assert.converts('boolean', 1, true);
assert.converts('boolean', 0, false);
assert.converts('boolean', null, false);

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

assert.converts('boolean', null, false, { allowNull: false });
assert.converts('boolean', undefined, false, { allowNull: false });

// duplicating the tests from the else branch here, so once the feature is
// enabled and the else branch is deleted, those assertions are kept
assert.converts('boolean', null, false);
assert.converts('boolean', undefined, false);
} else {
assert.converts('boolean', null, false);
assert.converts('boolean', undefined, false);
}

assert.converts('boolean', true, true);
assert.converts('boolean', false, false);
});
Expand Down
31 changes: 27 additions & 4 deletions tests/unit/transform/boolean-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import DS from 'ember-data';
import isEnabled from 'ember-data/-private/features';

import {module, test} from 'qunit';

Expand All @@ -7,8 +8,19 @@ module("unit/transform - DS.BooleanTransform");
test("#serialize", function(assert) {
var transform = new DS.BooleanTransform();

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

assert.equal(transform.serialize(null, { allowNull: false }), false);
assert.equal(transform.serialize(undefined, { allowNull: false }), false);

assert.equal(transform.serialize(null, {}), false);
assert.equal(transform.serialize(undefined, {}), false);
} else {
assert.equal(transform.serialize(null), false);
assert.equal(transform.serialize(undefined), false);
}

assert.equal(transform.serialize(true), true);
assert.equal(transform.serialize(false), false);
Expand All @@ -17,8 +29,19 @@ test("#serialize", function(assert) {
test("#deserialize", function(assert) {
var transform = new DS.BooleanTransform();

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

assert.equal(transform.deserialize(null, { allowNull: false }), false);
assert.equal(transform.deserialize(undefined, { allowNull: false }), false);

assert.equal(transform.deserialize(null, {}), false);
assert.equal(transform.deserialize(undefined, {}), false);
} else {
assert.equal(transform.deserialize(null), false);
assert.equal(transform.deserialize(undefined), false);
}

assert.equal(transform.deserialize(true), true);
assert.equal(transform.deserialize(false), false);
Expand Down