Skip to content

Commit 30ce0b4

Browse files
committed
Auto merge of #3216 - Turbo87:docs-link, r=locks
Move docs.rs link code from controller to `version` model If we want to reuse this in other places then this code should live on the model, not a specific controller. r? `@pichfl`
2 parents 8979f79 + b881199 commit 30ce0b4

File tree

4 files changed

+45
-34
lines changed

4 files changed

+45
-34
lines changed

app/controllers/crate/version.js

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import { inject as service } from '@ember/service';
66
import subDays from 'date-fns/subDays';
77
import { task } from 'ember-concurrency';
88

9-
import ajax from '../../utils/ajax';
10-
119
const NUM_VERSIONS = 5;
1210

1311
export default class CrateVersionController extends Controller {
@@ -127,32 +125,4 @@ export default class CrateVersionController extends Controller {
127125
return readme;
128126
})
129127
loadReadmeTask;
130-
131-
@computed('crate.{documentation,name}', 'currentVersion.num', 'loadDocsBuildsTask.lastSuccessful.value')
132-
get documentationLink() {
133-
// if this is *not* a docs.rs link we'll return it directly
134-
if (this.crate.documentation && !this.crate.documentation.startsWith('https://docs.rs/')) {
135-
return this.crate.documentation;
136-
}
137-
138-
// if we know about a successful docs.rs build, we'll return a link to that
139-
if (this.loadDocsBuildsTask.lastSuccessful) {
140-
let docsBuilds = this.loadDocsBuildsTask.lastSuccessful.value;
141-
if (docsBuilds.length !== 0 && docsBuilds[0].build_status === true) {
142-
return `https://docs.rs/${this.crate.name}/${this.currentVersion.num}`;
143-
}
144-
}
145-
146-
// finally, we'll return the specified documentation link, whatever it is
147-
if (this.crate.documentation) {
148-
return this.crate.documentation;
149-
}
150-
151-
return null;
152-
}
153-
154-
@task(function* () {
155-
return yield ajax(`https://docs.rs/crate/${this.crate.name}/${this.currentVersion.num}/builds.json`);
156-
})
157-
loadDocsBuildsTask;
158128
}

app/models/version.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { alias } from '@ember/object/computed';
44

55
import { task } from 'ember-concurrency';
66

7+
import ajax from '../utils/ajax';
8+
79
export default class Version extends Model {
810
@attr num;
911
@attr dl_path;
@@ -63,6 +65,45 @@ export default class Version extends Model {
6365
}).keepLatest())
6466
loadReadmeTask;
6567

68+
@task(function* () {
69+
return yield ajax(`https://docs.rs/crate/${this.crateName}/${this.num}/builds.json`);
70+
})
71+
loadDocsBuildsTask;
72+
73+
@computed('loadDocsBuildsTask.lastSuccessful.value')
74+
get hasDocsRsLink() {
75+
let docsBuilds = this.loadDocsBuildsTask.lastSuccessful?.value;
76+
return docsBuilds && docsBuilds.length !== 0 && docsBuilds[0].build_status === true;
77+
}
78+
79+
get docsRsLink() {
80+
if (this.hasDocsRsLink) {
81+
return `https://docs.rs/${this.crateName}/${this.num}`;
82+
}
83+
}
84+
85+
get documentationLink() {
86+
let crateDocsLink = this.crate.documentation;
87+
88+
// if this is *not* a docs.rs link we'll return it directly
89+
if (crateDocsLink && !crateDocsLink.startsWith('https://docs.rs/')) {
90+
return crateDocsLink;
91+
}
92+
93+
// if we know about a successful docs.rs build, we'll return a link to that
94+
let { docsRsLink } = this;
95+
if (docsRsLink) {
96+
return docsRsLink;
97+
}
98+
99+
// finally, we'll return the specified documentation link, whatever it is
100+
if (crateDocsLink) {
101+
return crateDocsLink;
102+
}
103+
104+
return null;
105+
}
106+
66107
@(task(function* () {
67108
let response = yield fetch(`/api/v1/crates/${this.crate.id}/${this.num}/yank`, { method: 'DELETE' });
68109
if (!response.ok) {

app/routes/crate/version.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ export default class VersionRoute extends Route {
4040
// ignored
4141
});
4242

43-
let { crate } = model;
43+
let { crate, version } = model;
4444
if (!crate.documentation || crate.documentation.startsWith('https://docs.rs/')) {
45-
controller.loadDocsBuildsTask.perform().catch(error => {
45+
version.loadDocsBuildsTask.perform().catch(error => {
4646
// report unexpected errors to Sentry and ignore `ajax()` errors
4747
if (!(error instanceof AjaxError)) {
4848
Sentry.captureException(error);

app/templates/crate/version.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
{{#if this.crate.mailing_list}}
2727
<li><a href="{{this.crate.mailing_list}}">Mailing list</a></li>
2828
{{/if}}
29-
{{#if this.documentationLink}}
30-
<li><a href="{{this.documentationLink}}" data-test-docs-link>Documentation</a></li>
29+
{{#if this.currentVersion.documentationLink}}
30+
<li><a href="{{this.currentVersion.documentationLink}}" data-test-docs-link>Documentation</a></li>
3131
{{/if}}
3232
{{#if this.crate.repository}}
3333
<li><a href="{{this.crate.repository}}">Repository</a></li>

0 commit comments

Comments
 (0)