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] DS.EmbeddedRecordsMixin support for belongsTo relationships #1637
[BUGFIX] DS.EmbeddedRecordsMixin support for belongsTo relationships #1637
Conversation
@bradleypriest Let me know if you have any questions re this PR and your integration test app ( https://github.com/bradleypriest/ams_integration_app ) |
Hey @pixelhandler sorry, I meant to get to this in the weekend, but my birthday got in the way, I'll try to take a look tonight, thanks for the work |
@bradleypriest Happy Birthday! |
@bradleypriest any update yet? I've been adding new features for dirty/saved state tracking between embedded and parent records over on my fork. I don't see much traction on the embedded records support within the Ember Data project; so figured I'd just maintain a separate library at: https://github.com/pixelhandler/ember-data-extensions. Originally when you reviewed some changes I made to support belongsTo relationships, you asked me to refactor the work into the embedded records mixin. From then I've been continuing to add support for embedded records based on applications I contribute to at my day job. Let me know if I should continue to merge that support (for belongsTo and other dirty/saved state tracking between embedded and parent records) into this PR. I'd like to contribute to the activemodel-adapter project; so I am curious what the direction is for completing support for the active_model_serializers gem. |
Hey @pixelhandler, sorry about the delay, life tends to get in the way. @igorT do you mind taking a look at this and seeing how it compares to what you're doing? |
@igorT do you have a fork w/ support for using belongsTo with embedded records? |
Can someone provide some direction on this topic? I'm confused...
Right now it stands that there is partial support for (embedded) hasMany relationships, but no support for (embedded) belongsTo relationships. In my opinion, it seems like the support for embedded records needs to be all or nothing. It's confusing to have partial support. I use the active_model_serializers gem which can use both hasMany and embed an array of objects and also can use belongsTo and embed an object. @wycats, @tomdale, @igorT - would you mind providing some clarity regarding support for active_model_serializers gem? I would be happy to contribute to that goal listed on the roadmap. |
@pixelhandler I completely understand your frustration. The original support for Embedded relation support was dropped from 1.0 betas because of the level of complexity and edge-cases it was running into. It has had some very basic functionality implemented, but I split it out into the EmbeddedRecordsMixin because there were so many things that it was falling short on, and people were wondering why it didn't just work/ I agree full support is the best option, but adding embedded belongsTo is still the very first step. I guess what I'm saying it's a hard problem to solve, and as horrible as it sounds, none of us have had the time lately. If @igorT can find some time to have a look over this PR maybe we can start moving forward again. At the very least a face-to-face at EmberConf should help kick things off again. /my 2cents |
@bradleypriest this PR is only addressing the missing support for embedding
^ these also can be addressed with a library of mixins, like the embedded records mixin. For the projects I work on we use mixins for dirty tracking / notification of embedded records, basically any embedded record is dirty when parent is dirty and vise-versa. For polymorphism, our db layer is smart we only use a So starting with the missing support for What is the direction for the activemodel-adapter package, going forward? I understand that the current iteration only supports embedded records using I already support embedded records for the daily work I do and we've shared that in a repo for others to use as well. The main reason I opened PRs for the DS.EmbeddedRecordsMixin is that like others I'm confused about the support for active_model_serializers gem using the activemodel-adapter package. I'd like to contribute to the project based on the work we've done for our ember apps. However if the activemodel-adapter package feature set is complete with only support for So I'm not frustrated, I'm very happy using Ember Data and the Adapters provided, as well as using the adapter/serializer API to do more. I'm confused about the direction for the mixins that come with the activemodel-adapter package. I can help others using the active_model_serializers gem. For now I have a fork and do support embedded records there. I'd be happy to contribute that work to the Ember Data project. When I hear we don't have time for |
I would love to see remark from @pixelhandler considered, we truly need embedded records in our application and current state of embedded records in master is one reason wich prevent us from upgrading ( we are on an hacked version of 0.13 ). It's been long time without a post about ember-data in the Ember.js Blog, an update about roadmap and goal would be really interesting. |
@bobbus perhaps make the upgrade for your Ember Data use by using the mixins here: https://github.com/pixelhandler/ember-data-extensions |
@pixelhandler , thanks a lot for sharing, your code looks LOT more cleaner than hacks we added in 0.13. I'm not surprised as I know code on master is full of improvments and it's great that it's going further each week :-) That said, added to the embedded objects feature, we are still hanging our app upgrade because of issue #1308 and #1288 . Hopefully we will upgrade soonish, thanks to your work and maybe |
@pixelhandler I just wanted to thank you for working on this and releasing ember-data-extensions. I also wanted to validate that IMHO, embedded To give a little perspective on what I used this for: I wanted to consume the Github API for events which includes embedded records like this: [
{
"type": "Event",
"public": true,
"payload": {
},
"repo": {
...
},
"actor": {
...
},
"org": {
...
},
"created_at": "2011-09-06T17:26:27Z",
"id": "12345"
}
] I first attempted using |
I had a local fork of DS.EmbeddedRecordMixin with support for This PR by pixelhandler is actually a pretty simple fix: It adds support for ActiveModel::Serializer's We've gone through quite a few versions of this pull request: #1516 and #1633, for example. I know that nobody has any cycles to look at this. But there are quite a few of us who'd love to help out. Is there anything we can do? Answer questions? Write more unit tests? Write more docs? Testify "Yes, I use this code in production and this is the correct solution?" Thank you for taking the time to look at this, and for such an excellent library! |
I don't believe Ember Data will support embedding records over the long haul, closing this PR. |
I think we probably will support embedded records in the long haul; I outlined the issue in #53 (comment), and we should discuss it more. |
if (belongsTo) { | ||
key = this.keyForAttribute(key); | ||
json[key + "_type"] = Ember.String.capitalize(belongsTo.constructor.typeKey); | ||
json[key + "_type"] = capitalize(camelize(belongsTo.constructor.typeKey)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rebase bug
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bradleypriest thanks, I missed that one, I fixed and pushed correction.
@igorT I added two commits per our pairing session today, I can squash but thought it would helpful to see the changes we made. We added Link to your gist for open question to community re flags for embedded 'always' vs more specific serialize/deserialize flags. Proposed flags for serialize/deserialize: https://gist.github.com/igorT/ec4d5eadefe08fa90274 |
I'm using this in production too using my fork of Ember-Data, I'd love to have this merged. It's a pain to maintain my own copy of Ember-Data :). |
Our team has had to come up with some customizations to get this working also. Would love to see this PR merged. |
@bakura10 @axelguiloff @igorT just need to finish off serializing options in the |
@igorT I added the This PR is ready to merge when you give thumbs up. Thanks for the feedback yesterday. |
@rjackson Do you know why the Travis build is failing? |
Any status updates on this? |
@gabelimon I chatted with @igorT last week, he needs to give this PR one more review then said he can merge it in. |
Thanks! |
} | ||
if (includeIds) { | ||
key = this.keyForRelationship(attr, relationship.kind); | ||
json[key] = get(record, attr);//.get(this, 'primaryKey'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commented out code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah that should be deleted
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we would want the commented out code, because we want to get the id, not the whole object?
- active_model_serializer gem `has_one` setting allows embedding objects - Add attrs options for serialize/deserialize using records or ids - Add attrs options for not serializing relationships using serialize:no -Add a hook called removeEmbeddedForeignKey for optional removal of foreign key reference from the embedded record
We added way more lines of tests and comments than actual code. 👍 @pixelhandler |
…gs-to [BUGFIX] DS.EmbeddedRecordsMixin support for belongsTo relationships
This is swell. |
Maybe I'm just being an idiot here but how would I go about using some model for embedded data without an ID? I believe the data you're mocking for in your comments would be something like this.
So is there currently some way to use a model that I've defined for author? Something like
|
@gabelimon models must have an ID! So, you can use a plain object with a raw transform or perhaps just using |
Thanks! I'll be sure to follow up in more appropriate forums next time.
|
See proposal on discuss.emberjs.com and PR #1516, #1633
DS.EmbeddedRecordsMixin handles embedded hasMany relationships, but not embedded belongsTo relationships.
This PR adds in the missing support for embedded objects in a payload that should be extracted/serialized for records with a belongsTo relationship.
has_one :xxxx, embed: :objects