Skip to content

Commit

Permalink
Fixing tests and adding settings provider.
Browse files Browse the repository at this point in the history
  • Loading branch information
jgable committed May 25, 2013
1 parent cc45be6 commit 601e261
Show file tree
Hide file tree
Showing 15 changed files with 535 additions and 110 deletions.
19 changes: 16 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
node: true,
browser: true,
nomen: true,
todo: true
todo: true,
unparam: true
},
files: [
// Lint files in the root, including Gruntfile.js
Expand All @@ -26,6 +27,17 @@
api: ['core/test/ghost/test-api.js']
},

mochaTest: {
options: {
ui: "bdd",
reporter: "spec"
},

all: {
src: ['core/test/**/*_spec.js']
}
},

// Compile all the SASS!
compass: {
options: {
Expand All @@ -40,17 +52,18 @@

grunt.loadNpmTasks("grunt-jslint");
grunt.loadNpmTasks("grunt-contrib-nodeunit");
grunt.loadNpmTasks("grunt-mocha-test");
grunt.loadNpmTasks("grunt-contrib-compass");

// Prepare the project for development
// TODO: Git submodule init/update (https://github.com/jaubourg/grunt-update-submodules)?
grunt.registerTask("init", ["compass:admin"]);

// Run API tests only
grunt.registerTask("test-api", ["nodeunit:api"]);
grunt.registerTask("test-api", ["nodeunit:api", "mochaTest:all"]);

// Run tests and lint code
grunt.registerTask("validate", ["jslint", "nodeunit:all"]);
grunt.registerTask("validate", ["jslint", "mochaTest:all"]);
};

module.exports = configureGrunt;
Expand Down
2 changes: 1 addition & 1 deletion content/themes/casper
5 changes: 3 additions & 2 deletions core/admin/assets/js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@
title: document.getElementById('entry-title').value,
content: editor.getValue()
},
urlSegments = window.location.pathname.split('/');
urlSegments = window.location.pathname.split('/'),
id;

if (urlSegments[2] === 'editor' && urlSegments[3] && /^[a-zA-Z0-9]+$/.test(urlSegments[2])) {
var id = urlSegments[3];
id = urlSegments[3];
$.ajax({
url: '/api/v0.1/posts/' + id,
method: 'PUT',
Expand Down
1 change: 0 additions & 1 deletion core/admin/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
var Ghost = require('../../ghost'),
_ = require('underscore'),
fs = require('fs'),
when = require('when/node/function'),
api = require('../../shared/api'),

ghost = new Ghost(),
Expand Down
8 changes: 6 additions & 2 deletions core/ghost.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,18 @@
* @param {Function} fn
* @return {*}
*/
Ghost.prototype.registerTheme = function (name, fn) {};
Ghost.prototype.registerTheme = function (name, fn) {
return this;
};

/**
* @param {string} name
* @param {Function} fn
* @return {*}
*/
Ghost.prototype.registerPlugin = function (name, fn) {};
Ghost.prototype.registerPlugin = function (name, fn) {
return this;
};

/**
* @param {string} name
Expand Down
50 changes: 31 additions & 19 deletions core/shared/models/dataProvider.bookshelf.base.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
(function () {
"use strict";

var _ = require('underscore'),
BookshelfBase;

/**
* The base class for interacting with bookshelf models/collections.
* Provides naive implementations of CRUD/BREAD operations.
*/
var BookshelfBase = function (model, collection) {
BookshelfBase = function (model, collection) {
// Bind the 'this' value for all our functions since they get messed
// up by the when.call
_.bindAll(this, 'findAll', 'browse', 'findOne', 'read', 'edit', 'add', 'destroy');

this.model = model;
this.collection = collection;
};

/**
* Naive find all
* @param args
* @param args (optional)
* @param callback
*/
BookshelfBase.prototype.findAll = function (args, callback) {
args = args || {};
this.collection.forge().fetch().then(function (results) {
BookshelfBase.prototype.findAll = BookshelfBase.prototype.browse = function (args, callback) {
if (_.isFunction(args)) {
// Curry the optional args parameter
callback = args;
args = {};
}

this.collection.forge(args).fetch().then(function (results) {
callback(null, results);
}, callback);
};
Expand All @@ -27,42 +39,42 @@
* @param args
* @param callback
*/
BookshelfBase.prototype.findOne = function (args, callback) {
BookshelfBase.prototype.findOne = BookshelfBase.prototype.read = function (args, callback) {
this.model.forge(args).fetch().then(function (result) {
callback(null, result);
}, callback);
};

/**
* Naive add
* @param newObj
* @param callback
*/
BookshelfBase.prototype.add = function (newObj, callback) {
this.model.forge(newObj).save().then(function (createdObj) {
callback(null, createdObj);
}, callback);
};

/**
* Naive edit
* @param editedObj
* @param callback
*/
BookshelfBase.prototype.edit = function (editedObj, callback) {
BookshelfBase.prototype.edit = BookshelfBase.prototype.update = function (editedObj, callback) {
this.model.forge({id: editedObj.id}).fetch().then(function (foundObj) {
foundObj.set(editedObj).save().then(function (updatedObj) {
callback(null, updatedObj);
}, callback);
});
};

/**
* Naive add
* @param newObj
* @param callback
*/
BookshelfBase.prototype.add = BookshelfBase.prototype.create = function (newObj, callback) {
this.model.forge(newObj).save().then(function (createdObj) {
callback(null, createdObj);
}, callback);
};

/**
* Naive destroy
* @param _identifier
* @param callback
*/
BookshelfBase.prototype.destroy = function (_identifier, callback) {
BookshelfBase.prototype.destroy = BookshelfBase.prototype.delete = function (_identifier, callback) {
this.model.forge({id: _identifier}).destroy().then(function () {
callback(null);
});
Expand Down
33 changes: 3 additions & 30 deletions core/shared/models/dataProvider.bookshelf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@
var knex = require('./knex_init'),
PostsProvider = require('./dataProvider.bookshelf.posts'),
UsersProvider = require('./dataProvider.bookshelf.users'),
models = require('./models'),
bcrypt = require('bcrypt'),
when = require("when"),
_ = require("underscore"),
SettingsProvider = require('./dataProvider.bookshelf.settings'),
DataProvider,
instance;

DataProvider = function () {
if (!instance) {
instance = this;
knex.Schema.hasTable('posts').then(null, function () {
// Simple boostraping of the data model for now.
// Simple bootstraping of the data model for now.
var migration = require('../data/migration/001');

migration.down().then(function() {
Expand All @@ -36,31 +33,7 @@

DataProvider.prototype.posts = new PostsProvider();
DataProvider.prototype.users = new UsersProvider();

// ## Settings
DataProvider.prototype.settings = function () { };

DataProvider.prototype.settings.browse = function (_args, callback) {
models.Settings.forge(_args).fetch().then(function (settings) {
callback(null, settings);
}, callback);
};

DataProvider.prototype.settings.read = function (_key, callback) {
models.Setting.forge({ key: _key }).fetch().then(function (setting) {
callback(null, setting);
}, callback);
};

DataProvider.prototype.settings.edit = function (_data, callback) {
when.all(_.map(_data, function (value, key) {
return models.Setting.forge({ key: key }).fetch().then(function (setting) {
return setting.set('value', value).save();
});
})).then(function (settings) {
callback(null, settings);
}, callback);
};
DataProvider.prototype.settings = new SettingsProvider();

module.exports = DataProvider;
}());
1 change: 0 additions & 1 deletion core/shared/models/dataProvider.bookshelf.posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"use strict";

var util = require('util'),

models = require('./models'),
BaseProvider = require('./dataProvider.bookshelf.base'),
PostsProvider;
Expand Down
42 changes: 42 additions & 0 deletions core/shared/models/dataProvider.bookshelf.settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
(function() {
"use strict";

var _ = require('underscore'),
when = require('when'),
util = require('util'),
models = require('./models'),
BaseProvider = require('./dataProvider.bookshelf.base'),
SettingsProvider;

/**
* The Posts data provider implementation for Bookshelf.
*/
SettingsProvider = function () {
BaseProvider.call(this, models.Setting, models.Settings);
};

util.inherits(SettingsProvider, BaseProvider);

SettingsProvider.prototype.read = function(_key, callback) {
// Allow for just passing the key instead of attributes
if (_.isString(_key)) {
_key = { key: _key };
}

BaseProvider.prototype.read.call(this, _key, callback);
};

SettingsProvider.prototype.edit = function(_data, callback) {
var self = this;

when.all(_.map(_data, function (value, key) {
return self.model.forge({ key: key }).fetch().then(function (setting) {
return setting.set('value', value).save();
});
})).then(function (settings) {
callback(null, settings);
}, callback);
};

module.exports = SettingsProvider;
}());
Loading

0 comments on commit 601e261

Please sign in to comment.