Permalink
Browse files

Adding support for more complex installers

  • Loading branch information...
1 parent e101cfc commit 8bc156f25ef6dea8a7cda6c64c43e15b989e77dc @fakedarren committed Apr 13, 2013
Showing with 89 additions and 30 deletions.
  1. +6 −2 app.js
  2. +25 −17 backend/controllers/backend/backend.js
  3. +3 −3 backend/install/assets.js
  4. +41 −8 backend/install/pages.js
  5. +1 −0 backend/models/page.js
  6. +13 −0 backend/models/section.js
View
8 app.js
@@ -1,8 +1,11 @@
var express = require('express'),
app = express(),
controller = require('./backend/classes/controller.class'),
- plugins = require('./backend/classes/plugins.class.js');
-
+ plugins = require('./backend/classes/plugins.class.js'),
+ config = require('./backend/config/configuration'),
+ mongoose = require('mongoose'),
+ db = mongoose.connect(config.database);
+
app.set('view engine', 'jade');
@@ -16,6 +19,7 @@ app.use(express.session({
global.plugins = plugins.initialize(app);
+global.db = db;
controller.initialize(app);
@@ -1,7 +1,5 @@
-var config = require('../../config/configuration'),
- mongoose = require('mongoose'),
+var mongoose = require('mongoose'),
Schema = mongoose.Schema,
- db = mongoose.connect(config.database),
_ = require('lodash');
@@ -43,25 +41,35 @@ module.exports = {
install.forEach(function(module){
var item = require(path + module),
- name = item.model.name,
- schema = item.model.schema;
+ models = item.models;
- mongoose.model(name, schema);
-
- var ModelObject = db.model(name),
- m = new ModelObject();
-
- m.collection.drop();
+ models.forEach(function(model){
+ mongoose.model(model.name, model.schema);
- _(item.records).each(function(record){
- var m = new ModelObject();
- for (var i in record){
- m[i] = record[i];
+ var ModelObject = db.model(model.name),
+ m = new ModelObject();
+
+ m.collection.drop();
+
+ state.push('Added ' + model.name + ' collection');
+
+ if (item[model.name + 'Records']){
+ _(item[model.name + 'Records']).each(function(record){
+ var m = new ModelObject();
+ for (var i in record){
+ m[i] = record[i];
+ }
+ m.save();
+ });
+ state.push('Added ' + item[model.name + 'Records'].length + ' records to ' + model.name + ' collection');
}
- m.save();
});
- state.push('Added ' + name + ' collection and ' + item.records.length + ' records');
+ if (item.installer){
+ item.installer.call(item);
+ state.push('Ran the installer method');
+ }
+
});
state.push('Install script complete!');
@@ -1,12 +1,12 @@
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId,
- model = require('../models/asset');
+ asset = require('../models/asset');
module.exports = {
- model: model,
- records: [
+ models: [asset],
+ assetsRecords: [
{
name: 'Pic 1',
src: 'http://placekitten.com/200/300',
View
@@ -1,17 +1,50 @@
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId,
- model = require('../models/page');
+ section = require('../models/section'),
+ page = require('../models/page'),
+ _ = require('lodash');
module.exports = {
- model: model,
- records: [
+ models: [section, page],
+ data: [
{
- title: 'My Page'
- },
- {
- title: 'My Other Page'
+ title: 'Main Navigation',
+ pages: [
+ {
+ title: 'My Page'
+ },
+ {
+ title: 'My Other Page'
+ },
+ {
+ title: 'My Third Page'
+ }
+ ]
}
- ]
+ ],
+ installer: function(){
+ var db = global.db;
+ var SectionObject = db.model(section.name),
+ sections = new SectionObject(),
+ PageObject = db.model(page.name),
+ pages = new PageObject();
+
+ sections.collection.drop();
+ pages.collection.drop();
+
+ _(this.data).each(function(sectionObj){
+ var s = new SectionObject();
+ s.title = sectionObj.title;
+ s.save();
+
+ _(sectionObj.pages).each(function(pageObj){
+ var p = new PageObject();
+ p.sectionID = s._id;
+ p.title = pageObj.title;
+ p.save();
+ });
+ });
+ }
};
View
@@ -4,6 +4,7 @@ var mongoose = require("mongoose"),
var page = new Schema({
pageID: ObjectId,
+ sectionID: String,
title: String
});
View
@@ -0,0 +1,13 @@
+var mongoose = require("mongoose"),
+ Schema = mongoose.Schema,
+ ObjectId = Schema.ObjectId;
+
+var section = new Schema({
+ pageID: ObjectId,
+ title: String
+});
+
+module.exports = {
+ name: 'sections',
+ schema: section
+};

0 comments on commit 8bc156f

Please sign in to comment.