-
Notifications
You must be signed in to change notification settings - Fork 40
[MOD-176][Feature] Replace has-many-query with queryHasMany #340
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
Merged
jamescdavis
merged 7 commits into
CenterForOpenScience:develop
from
aaxelb:feature/MOD-176--queryHasMany
Jan 17, 2018
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
56ce7bf
Add osf-model.queryHasMany
aaxelb 8f79095
Add queryHasMany test
aaxelb 1e55e23
Fully remove ember-data-has-many-query
aaxelb 06d0f12
Downgrade imports
aaxelb 1b769dc
Fix test
aaxelb c716b56
Update changelog, query uses
aaxelb e1dfdd6
Update yarn.lock
aaxelb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import Ember from 'ember'; | ||
| import DS from 'ember-data'; | ||
|
|
||
| import HasManyQuery from 'ember-data-has-many-query'; | ||
| import OSFAdapter from 'ember-osf/adapters/osf-adapter'; | ||
| import { authenticatedAJAX } from 'ember-osf/utils/ajax-helpers'; | ||
|
|
||
| /** | ||
| * @module ember-osf | ||
|
|
@@ -14,7 +14,7 @@ import HasManyQuery from 'ember-data-has-many-query'; | |
| * @class OsfModel | ||
| * @public | ||
| */ | ||
| export default DS.Model.extend(HasManyQuery.ModelMixin, { | ||
| export default DS.Model.extend({ | ||
| links: DS.attr('links'), | ||
| embeds: DS.attr('embed'), | ||
|
|
||
|
|
@@ -65,11 +65,55 @@ export default DS.Model.extend(HasManyQuery.ModelMixin, { | |
| remove: relation.canonicalMembers.list.filter(m => currentIds.indexOf(m.getRecord().get('id')) === -1) | ||
| }; | ||
|
|
||
| var other = this.get('_dirtyRelationships.${rel}') || {}; | ||
| var other = this.get(`_dirtyRelationships.${rel}`) || {}; | ||
| Ember.merge(other, changes); | ||
| this.set(`_dirtyRelationships.${rel}`, other); | ||
| } | ||
| }); | ||
| return this._super(...arguments); | ||
| } | ||
| }, | ||
|
|
||
| /* | ||
| * Query a hasMany relationship with query params | ||
| * | ||
| * @method queryHasMany | ||
| * @param {String} propertyName Name of a hasMany relationship on the model | ||
| * @param {Object} queryParams A hash to be serialized into the query string of the request | ||
| * @param {Object} [ajaxOptions] A hash of options to be passed to jQuery.ajax | ||
| * @returns {ArrayPromiseProxy} Promise-like array proxy, resolves to the records fetched | ||
| */ | ||
| queryHasMany(propertyName, queryParams, ajaxOptions) { | ||
| const reference = this.hasMany(propertyName); | ||
| const promise = new Ember.RSVP.Promise((resolve, reject) => { | ||
| // HACK: ember-data discards/ignores the link if an object on the belongsTo side | ||
| // came first. In that case, grab the link where we expect it from OSF's API | ||
| const url = reference.link() || this.get(`links.relationships.${propertyName}.links.related.href`); | ||
| if (!url) { | ||
| reject(`Could not find a link for '${propertyName}' relationship`); | ||
| return; | ||
| } | ||
| const options = Ember.merge({ | ||
| url, | ||
| data: queryParams, | ||
| headers: Ember.get(OSFAdapter, 'headers'), | ||
| }, ajaxOptions); | ||
|
|
||
| authenticatedAJAX(options).then( | ||
| Ember.run.bind(this, this.__queryHasManyDone, resolve), | ||
| reject | ||
| ); | ||
| }); | ||
|
|
||
| const ArrayPromiseProxy = Ember.ArrayProxy.extend(Ember.PromiseProxyMixin); | ||
| return ArrayPromiseProxy.create({ promise }); | ||
| }, | ||
|
|
||
| __queryHasManyDone(resolve, payload) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the double underscore prefix for "superprivate" methods that should never be called directly (only as a callback). AFAIK, this is not a convention in JS, but I like it. |
||
| const store = this.get('store'); | ||
| store.pushPayload(payload); | ||
| const records = payload.data.map(datum => store.peekRecord(datum.type, datum.id)); | ||
| records.meta = payload.meta; | ||
| records.links = payload.links; | ||
| resolve(records); | ||
| }, | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,91 @@ | ||
| import Ember from 'ember'; | ||
| import { moduleForModel, test } from 'ember-qunit'; | ||
|
|
||
| moduleForModel('osf-model', 'Unit | Model | osf model', { | ||
| // Specify the other units that are required for this test. | ||
| needs: [] | ||
| needs: ['model:user', 'model:node'] | ||
| }); | ||
|
|
||
| test('it exists', function(assert) { | ||
| let model = this.subject(); | ||
| // let store = this.store(); | ||
| assert.ok(!!model); | ||
| }); | ||
|
|
||
| test('queryHasMany works', function(assert) { | ||
| const store = this.store(); | ||
| const userId = 'guid1'; | ||
| const userNodesUrl = `https://api.osf.io/v2/users/${userId}/nodes/`; | ||
| const userPayload = { | ||
| 'data': { | ||
| 'relationships': { | ||
| 'nodes': { | ||
| 'links': { | ||
| 'related': { | ||
| 'href': userNodesUrl, | ||
| 'meta': {} | ||
| } | ||
| } | ||
| }, | ||
| }, | ||
| 'links': { | ||
| 'self': `https://api.osf.io/v2/users/${userId}/`, | ||
| 'html': `https://osf.io/${userId}/`, | ||
| }, | ||
| 'attributes': { | ||
| 'family_name': 'Guid', | ||
| 'given_name': 'A', | ||
| 'full_name': 'A Guid', | ||
| }, | ||
| 'type': 'users', | ||
| 'id': userId | ||
| }, | ||
| }; | ||
|
|
||
| const nodeIds = ['guid2', 'guid3', 'guid4'] | ||
| const nodePayloads = []; | ||
| for (const id of nodeIds) { | ||
| nodePayloads.push({ | ||
| 'links': { | ||
| 'self': `https://api.osf.io/v2/nodes/${id}/`, | ||
| 'html': `https://osf.io/${id}/` | ||
| }, | ||
| 'attributes': { | ||
| }, | ||
| 'type': 'nodes', | ||
| 'id': id | ||
| }); | ||
| } | ||
|
|
||
| const queryParams = {'param': 7}; | ||
|
|
||
| Ember.$.mockjax({ | ||
| url: userNodesUrl, | ||
| responseText: { | ||
| 'data': nodePayloads, | ||
| 'meta': { | ||
| 'total': nodePayloads.length, | ||
| }, | ||
| 'links': { | ||
| 'self': `https://api.osf.io/v2/users/${userId}/nodes/`, | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| assert.expect(3 + nodeIds.length); | ||
|
|
||
| const done = assert.async(); | ||
| Ember.run(function() { | ||
| store.pushPayload(userPayload); | ||
| const user = store.peekRecord('user', userId); | ||
| assert.ok(!!user); | ||
| assert.equal(user.get('id'), userId); | ||
| user.queryHasMany('nodes', queryParams).then(function(nodes) { | ||
| assert.equal(nodes.length, nodeIds.length); | ||
| for (let i = 0; i < nodes.length; i++) { | ||
| assert.equal(nodes[i].get('id'), nodeIds[i]); | ||
| } | ||
| done(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Just a drive-by fix -- not sure if it actually caused misbehavior, though.
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.
Wow, been broke for 2 years: https://github.com/aaxelb/ember-osf/blame/06d0f12f7b486e75fc97d5e8d93b829636e02d90/addon/models/osf-model.js#L68
But, like you said, this is likely not causing any misbehavior, yet...