mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
Merge pull request #6099 from acburdine/user-adapter
Implement custom user adapter to pull users by slug
This commit is contained in:
commit
431ccb16c9
7 changed files with 149 additions and 30 deletions
|
@ -1,17 +1,4 @@
|
||||||
import ApplicationAdapter from 'ghost/adapters/application';
|
import ApplicationAdapter from 'ghost/adapters/application';
|
||||||
import Ember from 'ember';
|
import SlugUrl from 'ghost/mixins/slug-url';
|
||||||
|
|
||||||
const {isBlank} = Ember;
|
export default ApplicationAdapter.extend(SlugUrl);
|
||||||
|
|
||||||
export default ApplicationAdapter.extend({
|
|
||||||
buildURL: function (_modelName, _id, _snapshot, _requestType, query) {
|
|
||||||
let url = this._super(...arguments);
|
|
||||||
|
|
||||||
if (query && !isBlank(query.slug)) {
|
|
||||||
url += `slug/${query.slug}/`;
|
|
||||||
delete query.slug;
|
|
||||||
}
|
|
||||||
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import ApplicationAdapter from 'ghost/adapters/application';
|
import ApplicationAdapter from 'ghost/adapters/application';
|
||||||
|
import SlugUrl from 'ghost/mixins/slug-url';
|
||||||
|
|
||||||
export default ApplicationAdapter.extend({
|
export default ApplicationAdapter.extend(SlugUrl, {
|
||||||
find: function (store, type, id) {
|
find: function (store, type, id) {
|
||||||
return this.findQuery(store, type, {id: id, status: 'all'});
|
return this.findQuery(store, type, {id: id, status: 'all'});
|
||||||
},
|
},
|
||||||
|
|
16
core/client/app/mixins/slug-url.js
Normal file
16
core/client/app/mixins/slug-url.js
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
import Ember from 'ember';
|
||||||
|
|
||||||
|
const {isBlank} = Ember;
|
||||||
|
|
||||||
|
export default Ember.Mixin.create({
|
||||||
|
buildURL: function (_modelName, _id, _snapshot, _requestType, query) {
|
||||||
|
let url = this._super(...arguments);
|
||||||
|
|
||||||
|
if (query && !isBlank(query.slug)) {
|
||||||
|
url += `slug/${query.slug}/`;
|
||||||
|
delete query.slug;
|
||||||
|
}
|
||||||
|
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
});
|
|
@ -39,7 +39,7 @@ Router.map(function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
this.route('team', {path: '/team'}, function () {
|
this.route('team', {path: '/team'}, function () {
|
||||||
this.route('user', {path: ':slug'});
|
this.route('user', {path: ':user_slug'});
|
||||||
});
|
});
|
||||||
|
|
||||||
this.route('settings.general', {path: '/settings/general'});
|
this.route('settings.general', {path: '/settings/general'});
|
||||||
|
|
|
@ -8,20 +8,11 @@ export default AuthenticatedRoute.extend(styleBody, CurrentUserSettings, {
|
||||||
classNames: ['team-view-user'],
|
classNames: ['team-view-user'],
|
||||||
|
|
||||||
model: function (params) {
|
model: function (params) {
|
||||||
var self = this;
|
return this.store.queryRecord('user', {slug: params.user_slug});
|
||||||
// TODO: Make custom user adapter that uses /api/users/:slug endpoint
|
},
|
||||||
// return this.store.find('user', { slug: params.slug });
|
|
||||||
|
|
||||||
// Instead, get all the users and then find by slug
|
serialize: function (model) {
|
||||||
return this.store.findAll('user', {reload: true}).then(function (result) {
|
return {user_slug: model.get('slug')};
|
||||||
var user = result.findBy('slug', params.slug);
|
|
||||||
|
|
||||||
if (!user) {
|
|
||||||
return self.transitionTo('error404', 'team/' + params.slug);
|
|
||||||
}
|
|
||||||
|
|
||||||
return user;
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
afterModel: function (user) {
|
afterModel: function (user) {
|
||||||
|
|
62
core/client/tests/unit/adapters/tag-test.js
Normal file
62
core/client/tests/unit/adapters/tag-test.js
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
/* jshint expr:true */
|
||||||
|
import { expect } from 'chai';
|
||||||
|
import {
|
||||||
|
describeModel,
|
||||||
|
it
|
||||||
|
} from 'ember-mocha';
|
||||||
|
import Pretender from 'pretender';
|
||||||
|
|
||||||
|
describeModel('tag', 'Unit: Adapter: tag', {
|
||||||
|
needs: [
|
||||||
|
'service:ghost-paths',
|
||||||
|
'service:session',
|
||||||
|
'adapter:tag',
|
||||||
|
'serializer:tag'
|
||||||
|
]
|
||||||
|
}, function () {
|
||||||
|
let server;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
server = new Pretender();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(function () {
|
||||||
|
server.shutdown();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('loads tags from regular endpoint when all are fetched', function (done) {
|
||||||
|
server.get('/ghost/api/v0.1/tags/', function () {
|
||||||
|
return [200, {'Content-Type': 'application/json'}, JSON.stringify({tags: [{
|
||||||
|
id: 1,
|
||||||
|
name: 'Tag 1',
|
||||||
|
slug: 'tag-1'
|
||||||
|
}, {
|
||||||
|
id: 2,
|
||||||
|
name: 'Tag 2',
|
||||||
|
slug: 'tag-2'
|
||||||
|
}]})];
|
||||||
|
});
|
||||||
|
|
||||||
|
this.store().findAll('tag', {reload: true}).then((tags) => {
|
||||||
|
expect(tags).to.be.ok;
|
||||||
|
expect(tags.objectAtContent(0).get('name')).to.equal('Tag 1');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('loads tag from slug endpoint when single tag is queried and slug is passed in', function (done) {
|
||||||
|
server.get('/ghost/api/v0.1/tags/slug/tag-1/', function () {
|
||||||
|
return [200, {'Content-Type': 'application/json'}, JSON.stringify({tags: [{
|
||||||
|
id: 1,
|
||||||
|
slug: 'tag-1',
|
||||||
|
name: 'Tag 1'
|
||||||
|
}]})];
|
||||||
|
});
|
||||||
|
|
||||||
|
this.store().queryRecord('tag', {slug: 'tag-1'}).then((tag) => {
|
||||||
|
expect(tag).to.be.ok;
|
||||||
|
expect(tag.get('name')).to.equal('Tag 1');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
62
core/client/tests/unit/adapters/user-test.js
Normal file
62
core/client/tests/unit/adapters/user-test.js
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
/* jshint expr:true */
|
||||||
|
import { expect } from 'chai';
|
||||||
|
import {
|
||||||
|
describeModel,
|
||||||
|
it
|
||||||
|
} from 'ember-mocha';
|
||||||
|
import Pretender from 'pretender';
|
||||||
|
|
||||||
|
describeModel('user', 'Unit: Adapter: user', {
|
||||||
|
needs: [
|
||||||
|
'service:ghost-paths',
|
||||||
|
'service:session',
|
||||||
|
'adapter:user',
|
||||||
|
'serializer:user'
|
||||||
|
]
|
||||||
|
}, function () {
|
||||||
|
let server;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
server = new Pretender();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(function () {
|
||||||
|
server.shutdown();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('loads users from regular endpoint when all are fetched', function (done) {
|
||||||
|
server.get('/ghost/api/v0.1/users/', function () {
|
||||||
|
return [200, {'Content-Type': 'application/json'}, JSON.stringify({users: [{
|
||||||
|
id: 1,
|
||||||
|
name: 'User 1',
|
||||||
|
slug: 'user-1'
|
||||||
|
}, {
|
||||||
|
id: 2,
|
||||||
|
name: 'User 2',
|
||||||
|
slug: 'user-2'
|
||||||
|
}]})];
|
||||||
|
});
|
||||||
|
|
||||||
|
this.store().findAll('user', {reload: true}).then((users) => {
|
||||||
|
expect(users).to.be.ok;
|
||||||
|
expect(users.objectAtContent(0).get('name')).to.equal('User 1');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('loads user from slug endpoint when single user is queried and slug is passed in', function (done) {
|
||||||
|
server.get('/ghost/api/v0.1/users/slug/user-1/', function () {
|
||||||
|
return [200, {'Content-Type': 'application/json'}, JSON.stringify({users: [{
|
||||||
|
id: 1,
|
||||||
|
slug: 'user-1',
|
||||||
|
name: 'User 1'
|
||||||
|
}]})];
|
||||||
|
});
|
||||||
|
|
||||||
|
this.store().queryRecord('user', {slug: 'user-1'}).then((user) => {
|
||||||
|
expect(user).to.be.ok;
|
||||||
|
expect(user.get('name')).to.equal('User 1');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Reference in a new issue