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

BooleanTransform should allow null values #3785

Closed
jaaksarv opened this issue Sep 18, 2015 · 6 comments
Closed

BooleanTransform should allow null values #3785

jaaksarv opened this issue Sep 18, 2015 · 6 comments

Comments

@jaaksarv
Copy link
Contributor

Currently BooleanTransform converts null values to false. This should not be default behavior. It is common to have nullable boolean values in database that represent situation where value is not yet defined. StringTransform, NumberTransform and DateTransform all support null values, so should BooleanTransform to make things consistent.

@bmac
Copy link
Member

bmac commented Sep 18, 2015

Hi @jaaksarv. I think you raise a very good point that BooleanTransform is the only transform that doesn't support null. Unfortunately, because of semver I don't think this is a change Ember Data could make until 3.0. For now I think the best work around is to define a custom transform that supports nulls.

// app/transforms/nullable-boolean.js
export default DS.BooleanTransform.extend({
  deserialize: function(serialized) {
    if (serialized === null) {
      return null;
    }
    return this._super(serialized);
  },

  serialize: function(deserialized) {
    if (deserialized === null) {
      return null;
    }
    return this._super(deserialized);
  }
});
// app/models/user.js
export default DS.Model.extend({
  isAdmin: attr('boolean'),
  isStaff: attr('nullable-boolean'),
  email: attr('string')
});

@bmac
Copy link
Member

bmac commented Oct 13, 2015

Hi @jaaksarv. I'm going to close this issue because I don't think we can change the behavior of the BooleanTransform at this time.

Feel free to reopen or create a new pr if you would like to add a new Transform that supports null.

@bmac bmac closed this as completed Oct 13, 2015
@jaaksarv
Copy link
Contributor Author

@bmac Why closing? It should be fixed in 3.0 as you described above.

@bmac bmac reopened this Oct 13, 2015
@bmac
Copy link
Member

bmac commented Oct 13, 2015

Ok I will leave it open.

@fivetanley
Copy link
Member

We should add support for this feature when emberjs/rfcs#1 lands, assuming:

  1. Feature flag the deprecations
  2. Decide later whether this should be the default
  3. defaultValue defaults to false
  4. Opt in with {allowNull: true}
  5. Think more about what null means in regards to true/false

@bmac
Copy link
Member

bmac commented May 26, 2016

This should be solved by #4022 which will be enabled in the next beta.

@bmac bmac closed this as completed May 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants