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

[BUGFIX] normalize model name for belongs to relationships #5477

Merged

Conversation

agrobbin
Copy link
Contributor

Given these models:

const User = DS.Model.extend({
  userProfile: DS.belongsTo(),

  streamItems: DS.hasMany()
});

const UserProfile = DS.Model.extend({
  user: DS.belongsTo()
});

const StreamItem = DS.Model.extend({
  user: DS.belongsTo()
});

The inferred model type of User.streamItems will be stream-item while the type of User.userProfile will be userProfile.

This goes against the concept of all model types being dasherized.

With this change, User.userProfile will be converted into user-profile.

I'm not exactly sure where this kind of thing should be tested. It seems like it belongs in same place that the hasMany model type normalization tests reside, but I'm having trouble finding that place!

Copy link
Contributor

@runspired runspired left a comment

Choose a reason for hiding this comment

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

Thanks! And great catch :) We'll be moving the singularize to a different point soon to address #5475 , but that's immaterial to here.

@runspired
Copy link
Contributor

@agrobbin if you have time to add tests, I can help you locate the right spot and draft them :)

@agrobbin
Copy link
Contributor Author

agrobbin commented Jun 7, 2018

@runspired absolutely, just point me in the right direction and I'll add them!

@agrobbin
Copy link
Contributor Author

agrobbin commented Jun 8, 2018

@runspired were you able to locate where tests should go for this change?

@runspired
Copy link
Contributor

@agrobbin haven't looked yet, sorry. I should get back to you this afternoon though :)

@bmac
Copy link
Member

bmac commented Jul 14, 2018

Unless @runspired has a better suggestion I think it would be ok to add a test for this change to tests/unit/model/relationships-test.js.

@agrobbin agrobbin force-pushed the belongs-to-model-name-normalization branch from 1cb1b5a to 7e32805 Compare July 19, 2018 01:24
@agrobbin
Copy link
Contributor Author

@bmac @runspired I just pushed up test coverage for this. Let me know if everything looks good!

const StreamItem = DS.Model.extend();

let User = DS.Model.extend({
streamItems: DS.hasMany(),
Copy link
Contributor

Choose a reason for hiding this comment

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

this should actually assert, as there is no inverse, surprised it does not

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure why it didn't assert, but I added the inverse relationship to StreamItem above.

const UserProfile = DS.Model.extend();

let User = DS.Model.extend({
userProfile: DS.belongsTo(),
Copy link
Contributor

Choose a reason for hiding this comment

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

same comment as below, this should assert and I'm surprised it does not, UserProfile needs the inverse defined.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ditto here, I added the inverse relationship definition to UserProfile above.

@agrobbin agrobbin force-pushed the belongs-to-model-name-normalization branch from 7e32805 to 64461e5 Compare July 19, 2018 01:30
const relationships = get(User, 'relationships');
const relationship = relationships.get('stream-item')[0];

assert.equal(relationship.meta.name, 'streamItems');
Copy link
Contributor

Choose a reason for hiding this comment

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

give asserts a meaningful description as the last arg (same for the other test).

User = store.modelFor('user');

const relationships = get(User, 'relationships');
const relationship = relationships.get('stream-item')[0];
Copy link
Contributor

Choose a reason for hiding this comment

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

we likely want an assertion that we did in fact normalize to stream-item from streamItems here, instead of just relying on it implicitly being tested due to how we access. Same with the test above.

@runspired
Copy link
Contributor

@agrobbin saw you resolved a few comments, left a few more. Resolve those and we should be good :)

Given these models:

```ruby
const User = DS.Model.extend({
  userProfile: DS.belongsTo(),

  streamItems: DS.hasMany()
});

const UserProfile = DS.Model.extend({
  user: DS.belongsTo()
});

const StreamItem = DS.Model.extend({
  user: DS.belongsTo()
});
```

The inferred model type of `User.streamItems` will be `stream-item` while the type of `User.userProfile` will be `userProfile`.

This goes against the concept of all model types being dasherized.

With this change, `User.userProfile` will be converted into `user-profile`.
@agrobbin agrobbin force-pushed the belongs-to-model-name-normalization branch from 64461e5 to ff47a3e Compare July 19, 2018 01:38
@agrobbin
Copy link
Contributor Author

@runspired just pushed some more updates!

@runspired runspired merged commit b044d59 into emberjs:master Jul 19, 2018
@agrobbin
Copy link
Contributor Author

Thanks for all of the help @runspired and @bmac! Looking forward to seeing this included in the next Ember Data release.

@bmac bmac added the beta label Jul 19, 2018
NullVoxPopuli pushed a commit to NullVoxPopuli/data that referenced this pull request Sep 8, 2018
)

Given these models:

```ruby
const User = DS.Model.extend({
  userProfile: DS.belongsTo(),

  streamItems: DS.hasMany()
});

const UserProfile = DS.Model.extend({
  user: DS.belongsTo()
});

const StreamItem = DS.Model.extend({
  user: DS.belongsTo()
});
```

The inferred model type of `User.streamItems` will be `stream-item` while the type of `User.userProfile` will be `userProfile`.

This goes against the concept of all model types being dasherized.

With this change, `User.userProfile` will be converted into `user-profile`.
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

Successfully merging this pull request may close these issues.

3 participants