Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Another attempt at #81 #105

Merged
merged 1 commit into from
Jun 1, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@
client: 'sqlite3',
connection: {
filename: './core/shared/data/tests.db'
},
debug: true
}
// debug: true
},

development: {
Expand Down
5 changes: 3 additions & 2 deletions core/admin/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
api = require('../../shared/api'),

ghost = new Ghost(),
dataProvider = ghost.dataProvider,
adminNavbar,
adminControllers;

Expand Down Expand Up @@ -170,7 +171,7 @@
});
},
'dbpopulate': function (req, res) {
ghost.dataProvider().populateData(function (error) {
dataProvider.populateData(function (error) {
if (error) {
req.flash('error', error);
} else {
Expand All @@ -180,7 +181,7 @@
});
},
'newUser': function (req, res) {
ghost.dataProvider().addNewUser(req, function (error) {
dataProvider.addNewUser(req, function (error) {
if (error) {
req.flash('error', error);
} else {
Expand Down
41 changes: 13 additions & 28 deletions core/ghost.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
_ = require('underscore'),
Polyglot = require('node-polyglot'),

JsonDataProvider = require('./shared/models/dataProvider.json'),
jsonDataProvider = new JsonDataProvider(),
BookshelfDataProvider = require('./shared/models/dataProvider.bookshelf'),
bookshelfDataProvider = new BookshelfDataProvider(),
models = require('./shared/models'),
ExampleFilter = require('../content/plugins/exampleFilters'),
Ghost,
instance,
Expand Down Expand Up @@ -49,8 +46,14 @@
polyglot;

if (!instance) {
// this.init();
instance = this;

// Holds the filters
this.filterCallbacks = [];

// Holds the filter hooks (that are built in to Ghost Core)
this.filters = [];

plugin = new ExampleFilter(instance).init();

app = express();
Expand All @@ -64,8 +67,8 @@
_.extend(instance, {
app: function () { return app; },
config: function () { return config; },
globals: function () { return instance.globalsData; }, // there's no management here to be sure this has loaded
dataProvider: function () { return bookshelfDataProvider; },
globals: function () { return instance.globalConfig; }, // there's no management here to be sure this has loaded
dataProvider: models,
statuses: function () { return statuses; },
polyglot: function () { return polyglot; },
plugin: function () { return plugin; },
Expand All @@ -84,29 +87,11 @@
return instance;
};

Ghost.prototype.init = function() {
var initGlobals = jsonDataProvider.save(config.blogData).then(function () {
return jsonDataProvider.findAll().then(function (data) {
// We must have an instance to be able to call ghost.init(), right?
instance.globalsData = data;
});
});

return when.all([initGlobals, instance.dataProvider().init()]);
Ghost.prototype.init = function () {
this.globalConfig = config.blogData;
return when.all([instance.dataProvider.init()]);
};

/**
* Holds the filters
* @type {Array}
*/
Ghost.prototype.filterCallbacks = [];

/**
* Holds the filter hooks (that are built in to Ghost Core)
* @type {Array}
*/
Ghost.prototype.filters = [];

/**
* @param {string} name
* @param {Function} fn
Expand Down
21 changes: 11 additions & 10 deletions core/shared/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
_ = require('underscore'),

ghost = new Ghost(),
dataProvider = ghost.dataProvider,
posts,
users,
settings,
Expand All @@ -23,50 +24,50 @@
// takes filter / pagination parameters
// returns a list of posts in a json response
browse: function (options) {
return ghost.dataProvider().posts.findAll(options);
return dataProvider.Post.findAll(options);
},
// takes an identifier (id or slug?)
// returns a single post in a json response
read: function (args) {
return ghost.dataProvider().posts.findOne(args);
return dataProvider.Post.findOne(args);
},
// takes a json object with all the properties which should be updated
// returns the resulting post in a json response
edit: function (postData) {
return ghost.dataProvider().posts.edit(postData);
return dataProvider.Post.edit(postData);
},
// takes a json object representing a post,
// returns the resulting post in a json response
add: function (postData) {
return ghost.dataProvider().posts.add(postData);
return dataProvider.Post.add(postData);
},
// takes an identifier (id or slug?)
// returns a json response with the id of the deleted post
destroy: function (args) {
return ghost.dataProvider().posts.destroy(args.id);
return dataProvider.Post.destroy(args.id);
}
};

// # Users
users = {
add: function (postData) {
return ghost.dataProvider().users.add(postData);
return dataProvider.User.add(postData);
},
check: function (postData) {
return ghost.dataProvider().users.check(postData);
return dataProvider.User.check(postData);
}
};

// # Settings
settings = {
browse: function (options) {
return ghost.dataProvider().settings.browse(options);
return dataProvider.Setting.browse(options);
},
read: function (options) {
return ghost.dataProvider().settings.read(options.key);
return dataProvider.Setting.read(options.key);
},
edit: function (options) {
return ghost.dataProvider().settings.edit(options);
return dataProvider.Setting.edit(options);
}
};

Expand Down
2 changes: 1 addition & 1 deletion core/shared/data/migration/001.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


var when = require('when'),
knex = require('../../models/knex_init'),
knex = require('../../models/base').Knex,
fixtures = require('../fixtures/001'),
up,
down;
Expand Down
96 changes: 96 additions & 0 deletions core/shared/models/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
(function () {

"use strict";

var GhostBookshelf,
Bookshelf = require('bookshelf'),
config = require('../../../config');

// Initializes Bookshelf as its own instance, so we can modify the Models and not mess up
// others' if they're using the library outside of ghost.
GhostBookshelf = Bookshelf.Initialize('ghost', config.database[process.env.NODE_ENV || 'development']);

// The Base Model which other Ghost objects will inherit from,
// including some convenience functions as static properties on the model.
GhostBookshelf.Model = GhostBookshelf.Model.extend({

// Base prototype properties will go here

}, {

/**
* Naive find all
* @param options (optional)
*/
findAll: function (options) {
options = options || {};
return GhostBookshelf.Collection.forge([], {model: this}).fetch(options);
},

browse: function () {
return this.findAll.apply(this, arguments);
},

/**
* Naive find one where args match
* @param args
* @param options (optional)
*/
findOne: function (args, options) {
options = options || {};
return this.forge(args).fetch(options);
},

read: function () {
return this.findOne.apply(this, arguments);
},

/**
* Naive edit
* @param editedObj
* @param options (optional)
*/
edit: function (editedObj, options) {
options = options || {};
return this.forge({id: editedObj.id}).fetch(options).then(function (foundObj) {
return foundObj.set(editedObj).save();
});
},

update: function () {
return this.edit.apply(this, arguments);
},

/**
* Naive create
* @param editedObj
* @param options (optional)
*/
add: function (newObj, options) {
options = options || {};
return this.forge(newObj).save(options);
},

create: function () {
return this.add.apply(this, arguments);
},

/**
* Naive destroy
* @param _identifier
* @param options (optional)
*/
destroy: function (_identifier, options) {
options = options || {};
return this.forge({id: _identifier}).destroy(options);
},

'delete': function () {
return this.destroy.apply(this, arguments);
}

});

module.exports = GhostBookshelf;

}());
74 changes: 0 additions & 74 deletions core/shared/models/dataProvider.bookshelf.base.js

This file was deleted.

Loading