Skip to content

Ember: CrateVersionRoute/Controller Cleanup #265

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
merged 11 commits into from
Feb 8, 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
15 changes: 15 additions & 0 deletions app/adapters/crate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import ApplicationAdapter from './application';

export default ApplicationAdapter.extend({
follow(id) {
return this.ajax(this.urlForFollowAction(id), 'PUT');
},

unfollow(id) {
return this.ajax(this.urlForFollowAction(id), 'DELETE');
},

urlForFollowAction(id) {
return `${this.buildURL('crate', id)}/follow`;
},
});
7 changes: 7 additions & 0 deletions app/adapters/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ApplicationAdapter from './application';

export default ApplicationAdapter.extend({
getDownloadUrl(dlPath) {
return this.ajax(dlPath, 'GET').then(response => response.url);
},
});
41 changes: 17 additions & 24 deletions app/controllers/crate/version.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Ember from 'ember';
import DS from 'ember-data';
import ajax from 'ic-ajax';
import moment from 'moment';

const NUM_VERSIONS = 5;
Expand All @@ -9,15 +8,17 @@ const { computed } = Ember;
export default Ember.Controller.extend({
isDownloading: false,

extraDownloads: Ember.computed('downloads.meta.extra_downloads', function() {
return this.get('downloads.meta.extra_downloads') || [];
downloadsContext: computed('requestedVersion', 'model', 'crate', function() {
return this.get('requestedVersion') ? this.get('model') : this.get('crate');
}),
downloads: computed.alias('downloadsContext.version_downloads'),
extraDownloads: computed.alias('downloads.content.meta.extra_downloads'),

fetchingFollowing: true,
following: false,
currentVersion: computed.alias('model'),
requestedVersion: null,
keywords: [],
keywords: computed.alias('crate.keywords'),

sortedVersions: computed.readOnly('crate.versions'),

Expand Down Expand Up @@ -89,40 +90,32 @@ export default Ember.Controller.extend({
download(version) {
this.set('isDownloading', true);

return ajax({
url: version.get('dl_path'),
dataType: 'json',
}).then((data) => {
version.getDownloadUrl().then(url => {
this.incrementProperty('crate.downloads');
this.incrementProperty('currentVersion.downloads');
Ember.$('#download-frame').attr('src', data.url);
Ember.$('#download-frame').attr('src', url);
}).finally(() => this.set('isDownloading', false));
},

toggleFollow() {
this.set('fetchingFollowing', true);
this.set('following', !this.get('following'));
var url = `/api/v1/crates/${this.get('crate.name')}/follow`;
var method;
if (this.get('following')) {
method = 'put';
} else {
method = 'delete';
}

ajax({
method,
url
}).finally(() => this.set('fetchingFollowing', false));
let crate = this.get('crate');
let op = this.toggleProperty('following') ?
crate.follow() : crate.unfollow();

return op.finally(() => this.set('fetchingFollowing', false));
},
},

downloadData: Ember.computed('downloads', 'extraDownloads', function() {
let { downloads, extraDownloads: extra } = this.getProperties('downloads', 'extraDownloads');
if (!downloads || !extra) {
downloadData: computed('downloads', 'extraDownloads', 'requestedVersion', function() {
let downloads = this.get('downloads');
if (!downloads) {
return;
}

let extra = this.get('extraDownloads') || [];

var dates = {};
var versions = [];
for (var i = 0; i < 90; i++) {
Expand Down
8 changes: 8 additions & 0 deletions app/models/crate.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ export default DS.Model.extend({
version_downloads: DS.hasMany('version-download', { async: true }),
keywords: DS.hasMany('keywords', { async: true }),
reverse_dependencies: DS.hasMany('dependency', { async: true }),

follow() {
return this.store.adapterFor('crate').follow(this.get('id'));
},

unfollow() {
return this.store.adapterFor('crate').unfollow(this.get('id'));
},
});
4 changes: 4 additions & 0 deletions app/models/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ export default DS.Model.extend({
authors: DS.hasMany('users', { async: true }),
dependencies: DS.hasMany('dependency', { async: true }),
version_downloads: DS.hasMany('version-download', { async: true }),

getDownloadUrl() {
return this.store.adapterFor('version').getDownloadUrl(this.get('dl_path'));
},
});
23 changes: 2 additions & 21 deletions app/routes/crate/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ export default Ember.Route.extend({
controller.set('requestedVersion', requestedVersion);
controller.set('fetchingFollowing', true);

crate.get('keywords')
.then((keywords) => controller.set('keywords', keywords));

if (this.session.get('currentUser')) {
ajax(`/api/v1/crates/${crate.get('name')}/following`)
.then((d) => controller.set('following', d.following))
Expand All @@ -43,24 +40,8 @@ export default Ember.Route.extend({
});
},

// can't do this in setupController because it won't be called
// when going from "All Versions" to the current version
afterModel(model) {
this._super(...arguments);

const controller = this.controllerFor(this.routeName);
const context = controller.get('requestedVersion') ? model : this.modelFor('crate');

context.get('version_downloads').then(downloads => {
controller.set('downloads', downloads);
});
},

serialize(model) {
if (!model) {
return { version_num: '' };
} else {
return { version_num: model.get('num') };
}
let version_num = model ? model.get('num') : '';
return { version_num };
},
});
6 changes: 3 additions & 3 deletions app/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import ajax from 'ic-ajax';
export default Ember.Route.extend({
model() {
function addCrates(store, crates) {
for (var i = 0; i < crates.length; i++) {
crates[i] = store.push(store.normalize('crate', crates[i]));
for (var i = 0; i < crates.length; i++) {
crates[i] = store.push(store.normalize('crate', crates[i]));
}
}
}

return ajax('/summary').then((data) => {
addCrates(this.store, data.new_crates);
Expand Down
6 changes: 1 addition & 5 deletions app/templates/crate/version.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,7 @@
<div class='stat'>
<span class='num'>
<img src="/assets/download.png"/>
{{#if requestedVersion}}
{{ format-num currentVersion.downloads }}
{{else}}
{{ format-num crate.downloads }}
{{/if}}
{{ format-num downloadsContext.downloads }}
</span>
<span class='desc small'>Downloads all time</span>
</div>
Expand Down