Skip to content

Commit

Permalink
refactor canonicalURL into computed property
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinMalinda committed Jul 1, 2017
1 parent 93d466f commit 12b8d4f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 28 deletions.
32 changes: 7 additions & 25 deletions app/routes/project-version.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Ember from 'ember';
import _ from 'lodash';

const { inject, run } = Ember;
const { inject } = Ember;

export default Ember.Route.extend({
projectService: Ember.inject.service('project'),
Expand All @@ -21,35 +21,17 @@ export default Ember.Route.extend({
const id = `${params.project}-${version}`;
const projectVersion = await this.store.findRecord('project-version', id, { includes: 'project' });

this.setCanonicalURL(params, projectVersion);
this.injectDataIntoHeadModel(params, projectVersion);

return projectVersion;

},

setCanonicalURL({project_version}, versionModel) {
if (project_version !== 'lts' && project_version !== 'release') {


// run next is required so router.currentURL is present
// but it can be performance issue
// constructing the URL via href-to helper could be a good alternative
run.next(() => {
const currentURL = this.get('router.currentURL') || "";

if (versionModel.get('isRelease')) {
this.set('headData.canonicalURL', currentURL.replace(project_version, 'release'));
} else if (versionModel.get('isLTS')) {
this.set('headData.canonicalURL', currentURL.replace(project_version, 'lts'));
} else {
this.set('headData.canonicalURL', null);
}

});

} else {
this.set('headData.canonicalURL', null);
}
injectDataIntoHeadModel({project_version}, projectVersionModel) {
this.get('headData').setProperties({
projectVersionModel,
projectVersionParam: project_version
});
},

// Using redirect instead of afterModel so transition succeeds and returns 30
Expand Down
34 changes: 34 additions & 0 deletions app/services/head-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Ember from 'ember';
import HeadDataService from 'ember-cli-head/services/head-data';

const { computed, inject } = Ember;

export default HeadDataService.extend({

router: inject.service(),

projectVersionParam: null, //injected in project-version route
projectVersionModel: null, // injected in project-version route

canonicalURL: computed('projectVersionModel.{isRelease,isLTS}', 'router.currentURL', 'projectVersionParam', function() {
const versionParam = this.get('projectVersionParam');
if (versionParam !== 'release' && versionParam !== 'lts') {
const versionModel = this.get('projectVersionModel');
const currentURL = this.get('router.currentURL') || "";

if (versionModel) {
if (versionModel.get('isRelease')) {
return currentURL.replace(versionParam, 'release');
} else if (versionModel.get('isLTS')) {
return currentURL.replace(versionParam, 'lts');
}
}

return null;
}

return null;

})

});
4 changes: 1 addition & 3 deletions tests/acceptance/release-lts-project-versions-test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { test } from 'qunit';
import moduleForAcceptance from 'ember-api-docs/tests/helpers/module-for-acceptance';
import { click } from 'ember-native-dom-helpers';
import { click, visit } from 'ember-native-dom-helpers';
import Ember from 'ember';
import { visit } from 'ember-native-dom-helpers';


const {$} = Ember;

Expand Down

0 comments on commit 12b8d4f

Please sign in to comment.