Skip to content

Commit

Permalink
Make the library compatible with Sails 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jledentu authored Oct 17, 2019
2 parents ac5e01f + bbb9571 commit aafb177
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 35 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ Post.create({
});
```

Like any other attribute, you can use dynamic finders:
Like any other attribute, you can use it as criteria to find a given resource:

```js
Post.findOneBySlug('this-is-a-new-post')
Post.findOne({ slug: 'this-is-a-new-post' })
.then(function(post) {
// Use the post
})
Expand All @@ -98,4 +98,4 @@ separator | `string` | Separator to use in slugs. Defaults to `-`.

## License

MIT © 2017 Jérémie Ledentu
MIT © 2019 Jérémie Ledentu
4 changes: 2 additions & 2 deletions api/models/Post.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/

module.exports = {

attributes: {
id: { type: 'number', autoIncrement: true},
title: {
type: 'string',
required: true,
Expand All @@ -17,7 +17,7 @@ module.exports = {
type: 'string'
},
content: {
type: 'text'
type: 'string'
},
slug: {
type: 'slug',
Expand Down
23 changes: 15 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ module.exports = function(sails) {
});
}

function patchModels() {
for (let modelName of Object.keys(sails.models)) {
let model = sails.models[modelName];
function patchModels(models) {
for (let modelName of Object.keys(models)) {
let model = models[modelName];

for (let name of Object.keys(model.attributes)) {
let attr = model.attributes[name];
Expand Down Expand Up @@ -65,6 +65,7 @@ module.exports = function(sails) {
})(model.beforeCreate, attr.from, attr.blacklist);

delete attr.from;
delete attr.blacklist;
}
}
}
Expand All @@ -79,11 +80,17 @@ module.exports = function(sails) {
},
},

initialize: function(next) {
sails.after(['hook:moduleloader:loaded'], function() {
patchModels();

return next();
initialize(done) {
sails.after('hook:moduleloader:loaded', () => {
const originalLoadModels = sails.modules.loadModels;
sails.modules.loadModels = function(cb) {
originalLoadModels((error, models) => {
patchModels(models);
console.log(models);
cb(error, models);
done();
});
};
});
},
};
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
"eslint": "^4.8.0",
"mocha": "^5.0.0",
"nyc": "^11.2.1",
"sails": "^0.12.14",
"sails-disk": "^0.10.8",
"sails": "^1.2.3",
"sails-hook-orm": "^2.1.1",
"should": "^13.1.1"
},
"dependencies": {
"slugg": "^1.2.1",
"uuid": "^3.0.1"
}
}
}
3 changes: 3 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"env": {
"mocha": true
},
"globals": {
"sails": true
}
}
9 changes: 5 additions & 4 deletions test/bootstrap.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require('should');
var Sails = require('sails');
var sails = require('sails');

before(function(done) {

this.timeout(50000);
let config = {
log: {
level: 'info'
level: 'verbose'
},
hooks: {
slugs: require('../lib'),
Expand All @@ -18,14 +18,15 @@ before(function(done) {
}
};

Sails.lift(config, function (err, sails) {
sails.lift(config, function (err) {
if (err) {
return done(err);
}
global.sails = sails;
return done();
});
});

after(function(done) {
Sails.lower(done);
sails.lower(done);
});
30 changes: 15 additions & 15 deletions test/unit/models/Post.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ describe('PostModel', function() {

describe('#create()', function() {
it('should create a new post', function(done) {
Post.create({
sails.models.post.create({
title: 'This is a new post!!!',
content: 'Post content',
author: 'Jérémie Ledentu'
})
.fetch()
.then(function(post) {
post.should.have.property('title');
post.title.should.eql('This is a new post!!!');
Expand All @@ -27,11 +28,12 @@ describe('PostModel', function() {
});

it('should resolve slug conflicts', function(done) {
Post.create({
sails.models.post.create({
title: 'This is a new post!!',
content: 'Post content 2',
author: 'Jérémie Ledentu'
})
.fetch()
.then(function(post) {
post.should.have.property('title');
post.title.should.eql('This is a new post!!');
Expand All @@ -51,10 +53,11 @@ describe('PostModel', function() {

it('should not use a slug from global blacklist', (done) => {
sails.config.slugs.blacklist = ['new'];
Post.create({
sails.models.post.create({
title: 'New',
author: 'Jérémie Ledentu'
})
.fetch()
.then((post) => {
post.should.have.property('slug');
post.slug.should.match(/^new-/);
Expand All @@ -67,10 +70,11 @@ describe('PostModel', function() {
});

it('should not use a slug from local blacklist', (done) => {
Post.create({
sails.models.post.create({
title: 'My new post',
author: 'Profile'
})
.fetch()
.then((post) => {
post.should.have.property('author');
post.slugAuthor.should.match(/^profile-/);
Expand All @@ -82,15 +86,16 @@ describe('PostModel', function() {
it('should not use lowercase if lowercase === false in the config', done => {
sails.config.slugs.lowercase = false;

Post.create({
sails.models.post.create({
title: 'THIS IS A TITLE IN UPPERCASE',
content: 'Post content',
author: 'Jérémie Ledentu'
})
.fetch()
.then(post => {
post.should.have.property('slug');
post.slug.should.eql('THIS-IS-A-TITLE-IN-UPPERCASE');
post.slugAuthor.should.match(/^Jeremie-Ledentu-/);
post.slugAuthor.should.eql('Jeremie-Ledentu');

sails.config.slugs.lowercase = true;

Expand All @@ -102,11 +107,12 @@ describe('PostModel', function() {
it('should use separator defined in config', done => {
sails.config.slugs.separator = '_';

Post.create({
sails.models.post.create({
title: 'This is underscore',
content: 'Post content',
author: 'Jérémie Ledentu'
})
.fetch()
.then(post => {
post.should.have.property('slug');
post.slug.should.eql('this_is_underscore');
Expand All @@ -120,15 +126,9 @@ describe('PostModel', function() {
});
});

describe('#findOneBySlug()', function() {
it('should exist in the model', function(done) {
Post.should.have.property('findBySlug');
Post.findOneBySlug.should.be.a.Function();
done();
});

describe('#findOne({slug})', function() {
it('should return the entity with given slug', function(done) {
Post.findOneBySlug('this-is-a-new-post')
sails.models.post.findOne({ slug: 'this-is-a-new-post' })
.then(function(post) {
post.should.have.property('title');
post.title.should.eql('This is a new post!!!');
Expand Down

0 comments on commit aafb177

Please sign in to comment.