Skip to content

Commit 02029a3

Browse files
committed
Merge pull request #265 from Turbo87/cleanup
Ember: CrateVersionRoute/Controller Cleanup
2 parents ab19699 + 5feda69 commit 02029a3

File tree

8 files changed

+57
-53
lines changed

8 files changed

+57
-53
lines changed

app/adapters/crate.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import ApplicationAdapter from './application';
2+
3+
export default ApplicationAdapter.extend({
4+
follow(id) {
5+
return this.ajax(this.urlForFollowAction(id), 'PUT');
6+
},
7+
8+
unfollow(id) {
9+
return this.ajax(this.urlForFollowAction(id), 'DELETE');
10+
},
11+
12+
urlForFollowAction(id) {
13+
return `${this.buildURL('crate', id)}/follow`;
14+
},
15+
});

app/adapters/version.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import ApplicationAdapter from './application';
2+
3+
export default ApplicationAdapter.extend({
4+
getDownloadUrl(dlPath) {
5+
return this.ajax(dlPath, 'GET').then(response => response.url);
6+
},
7+
});

app/controllers/crate/version.js

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import Ember from 'ember';
22
import DS from 'ember-data';
3-
import ajax from 'ic-ajax';
43
import moment from 'moment';
54

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

12-
extraDownloads: Ember.computed('downloads.meta.extra_downloads', function() {
13-
return this.get('downloads.meta.extra_downloads') || [];
11+
downloadsContext: computed('requestedVersion', 'model', 'crate', function() {
12+
return this.get('requestedVersion') ? this.get('model') : this.get('crate');
1413
}),
14+
downloads: computed.alias('downloadsContext.version_downloads'),
15+
extraDownloads: computed.alias('downloads.content.meta.extra_downloads'),
1516

1617
fetchingFollowing: true,
1718
following: false,
1819
currentVersion: computed.alias('model'),
1920
requestedVersion: null,
20-
keywords: [],
21+
keywords: computed.alias('crate.keywords'),
2122

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

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

92-
return ajax({
93-
url: version.get('dl_path'),
94-
dataType: 'json',
95-
}).then((data) => {
93+
version.getDownloadUrl().then(url => {
9694
this.incrementProperty('crate.downloads');
9795
this.incrementProperty('currentVersion.downloads');
98-
Ember.$('#download-frame').attr('src', data.url);
96+
Ember.$('#download-frame').attr('src', url);
9997
}).finally(() => this.set('isDownloading', false));
10098
},
10199

102100
toggleFollow() {
103101
this.set('fetchingFollowing', true);
104-
this.set('following', !this.get('following'));
105-
var url = `/api/v1/crates/${this.get('crate.name')}/follow`;
106-
var method;
107-
if (this.get('following')) {
108-
method = 'put';
109-
} else {
110-
method = 'delete';
111-
}
112102

113-
ajax({
114-
method,
115-
url
116-
}).finally(() => this.set('fetchingFollowing', false));
103+
let crate = this.get('crate');
104+
let op = this.toggleProperty('following') ?
105+
crate.follow() : crate.unfollow();
106+
107+
return op.finally(() => this.set('fetchingFollowing', false));
117108
},
118109
},
119110

120-
downloadData: Ember.computed('downloads', 'extraDownloads', function() {
121-
let { downloads, extraDownloads: extra } = this.getProperties('downloads', 'extraDownloads');
122-
if (!downloads || !extra) {
111+
downloadData: computed('downloads', 'extraDownloads', 'requestedVersion', function() {
112+
let downloads = this.get('downloads');
113+
if (!downloads) {
123114
return;
124115
}
125116

117+
let extra = this.get('extraDownloads') || [];
118+
126119
var dates = {};
127120
var versions = [];
128121
for (var i = 0; i < 90; i++) {

app/models/crate.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,12 @@ export default DS.Model.extend({
2121
version_downloads: DS.hasMany('version-download', { async: true }),
2222
keywords: DS.hasMany('keywords', { async: true }),
2323
reverse_dependencies: DS.hasMany('dependency', { async: true }),
24+
25+
follow() {
26+
return this.store.adapterFor('crate').follow(this.get('id'));
27+
},
28+
29+
unfollow() {
30+
return this.store.adapterFor('crate').unfollow(this.get('id'));
31+
},
2432
});

app/models/version.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ export default DS.Model.extend({
1414
authors: DS.hasMany('users', { async: true }),
1515
dependencies: DS.hasMany('dependency', { async: true }),
1616
version_downloads: DS.hasMany('version-download', { async: true }),
17+
18+
getDownloadUrl() {
19+
return this.store.adapterFor('version').getDownloadUrl(this.get('dl_path'));
20+
},
1721
});

app/routes/crate/version.js

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ export default Ember.Route.extend({
1919
controller.set('requestedVersion', requestedVersion);
2020
controller.set('fetchingFollowing', true);
2121

22-
crate.get('keywords')
23-
.then((keywords) => controller.set('keywords', keywords));
24-
2522
if (this.session.get('currentUser')) {
2623
ajax(`/api/v1/crates/${crate.get('name')}/following`)
2724
.then((d) => controller.set('following', d.following))
@@ -43,24 +40,8 @@ export default Ember.Route.extend({
4340
});
4441
},
4542

46-
// can't do this in setupController because it won't be called
47-
// when going from "All Versions" to the current version
48-
afterModel(model) {
49-
this._super(...arguments);
50-
51-
const controller = this.controllerFor(this.routeName);
52-
const context = controller.get('requestedVersion') ? model : this.modelFor('crate');
53-
54-
context.get('version_downloads').then(downloads => {
55-
controller.set('downloads', downloads);
56-
});
57-
},
58-
5943
serialize(model) {
60-
if (!model) {
61-
return { version_num: '' };
62-
} else {
63-
return { version_num: model.get('num') };
64-
}
44+
let version_num = model ? model.get('num') : '';
45+
return { version_num };
6546
},
6647
});

app/routes/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import ajax from 'ic-ajax';
44
export default Ember.Route.extend({
55
model() {
66
function addCrates(store, crates) {
7-
for (var i = 0; i < crates.length; i++) {
8-
crates[i] = store.push(store.normalize('crate', crates[i]));
7+
for (var i = 0; i < crates.length; i++) {
8+
crates[i] = store.push(store.normalize('crate', crates[i]));
9+
}
910
}
10-
}
1111

1212
return ajax('/summary').then((data) => {
1313
addCrates(this.store, data.new_crates);

app/templates/crate/version.hbs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,7 @@
185185
<div class='stat'>
186186
<span class='num'>
187187
<img src="/assets/download.png"/>
188-
{{#if requestedVersion}}
189-
{{ format-num currentVersion.downloads }}
190-
{{else}}
191-
{{ format-num crate.downloads }}
192-
{{/if}}
188+
{{ format-num downloadsContext.downloads }}
193189
</span>
194190
<span class='desc small'>Downloads all time</span>
195191
</div>

0 commit comments

Comments
 (0)