Skip to content
This repository has been archived by the owner on Aug 13, 2021. It is now read-only.

0.12 #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
71 changes: 64 additions & 7 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,73 @@
* WARNING:
* Unless you know what you're doing, you shouldn't change this file.
* Check out the `tasks` directory instead.
*
* For more information see:
* http://sailsjs.com/anatomy/Gruntfile.js
*/

module.exports = function(grunt) {

var loadGruntTasks = require('sails-hook-grunt/accessible/load-grunt-tasks');

// Load Grunt task configurations (from `tasks/config/`) and Grunt
// task registrations (from `tasks/register/`).
loadGruntTasks(__dirname, grunt);
// Load the include-all library in order to require all of our grunt
// configurations and task registrations dynamically.
var includeAll;
try {
includeAll = require('include-all');
} catch (e0) {
try {
includeAll = require('sails/node_modules/include-all');
} catch (e1) {
console.error('Could not find `include-all` module.');
console.error('Skipping grunt tasks...');
console.error('To fix this, please run:');
console.error('npm install include-all --save`');
console.error();

grunt.registerTask('default', []);
return;
}
}


/**
* Loads Grunt configuration modules from the specified
* relative path. These modules should export a function
* that, when run, should either load/configure or register
* a Grunt task.
*/
function loadTasks(relPath) {
return includeAll({
dirname: require('path').resolve(__dirname, relPath),
filter: /(.+)\.js$/,
excludeDirs: /^\.(git|svn)$/
}) || {};
}

/**
* Invokes the function from a Grunt configuration module with
* a single argument - the `grunt` object.
*/
function invokeConfigFn(tasks) {
for (var taskName in tasks) {
if (tasks.hasOwnProperty(taskName)) {
tasks[taskName](grunt);
}
}
}



// Load task functions
var taskConfigurations = loadTasks('./tasks/config'),
registerDefinitions = loadTasks('./tasks/register');

// (ensure that a default task exists)
if (!registerDefinitions.default) {
registerDefinitions.default = function(grunt) {
grunt.registerTask('default', []);
};
}

// Run task functions to configure Grunt.
invokeConfigFn(taskConfigurations);
invokeConfigFn(registerDefinitions);

};
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
A sample [Sails](http://sailsjs.com) application, generated using:

```
npm install -g sails@balderdashy/sails#87624e7aa9aedddb3c8ee75decd37e4ff2a4ee83
npm install -g sails
```


Expand Down
50 changes: 50 additions & 0 deletions api/hooks/myHook/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module.exports = function(sails) {

// This var will be private
// var foo = 'bar';

// This var will be public
// this.abc = 123;

return {

// Pause sails lifting until this hook has completed initializing
// ready: false,

// set up the options of your hook
defaults:{
},

// do stuff before intialize the hook
configure: function(done){
return done();
},

// the logic of your hook
initialize: function(done){
// This will be available in app code as sails.hooks.myhook.numRequestsSeen
this.numRequestsSeen = 0;
// This will be available in app code as sails.hooks.myhook.numUnhandledRequestsSeen
this.numUnhandledRequestsSeen = 0;
return done();
},

routes: {
before: {
// This route will be matched before any routes in config/routes.js
'GET /*': function (req, res, next) {
this.numRequestsSeen++;
return next();
}
},
after: {
// This route will be matched after any routes in config/routes.js
'GET /*': function (req, res, next) {
this.numUnhandledRequestsSeen++;
return next();
}
}
}

};
};
10 changes: 5 additions & 5 deletions api/models/User.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* User.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/#!documentation/models
*/
* User.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/documentation/concepts/models-and-orm/models
*/

module.exports = {

Expand Down
23 changes: 11 additions & 12 deletions api/policies/sessionAuth.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
/**
* sessionAuth
*
* A simple policy that allows any request from an authenticated user.
* @module :: Policy
* @description :: Simple policy to allow any authenticated user
* Assumes that your login action in one of your controllers sets `req.session.authenticated = true;`
* @docs :: http://sailsjs.org/#!/documentation/concepts/Policies
*
* For more about how this policy works and how to use it, see:
* http://sailsjs.com/anatomy/api/policies/session-auth-js
*/
module.exports = function sessionAuth(req, res, next) {
module.exports = function(req, res, next) {

// If `req.session.userId` is set, then we know that this request originated
// from a logged-in user. So we can safely proceed to the next policy--
// or, if this is the last policy, the relevant action.
if (req.session.userId) {
// User is allowed, proceed to the next policy,
// or if this is the last policy, the controller
if (req.session.authenticated) {
return next();
}

//--•
// Otherwise, this request did not come from a logged-in user.
return res.forbidden();

// User is not allowed
// (default res.forbidden() behavior can be overridden in `config/403.js`)
return res.forbidden('You are not permitted to perform this action.');
};
76 changes: 76 additions & 0 deletions api/responses/badRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* 400 (Bad Request) Handler
*
* Usage:
* return res.badRequest();
* return res.badRequest(data);
* return res.badRequest(data, 'some/specific/badRequest/view');
*
* e.g.:
* ```
* return res.badRequest(
* 'Please choose a valid `password` (6-12 characters)',
* 'trial/signup'
* );
* ```
*/

module.exports = function badRequest(data, options) {

// Get access to `req`, `res`, & `sails`
var req = this.req;
var res = this.res;
var sails = req._sails;

// Set status code
res.status(400);

// Log error to console
if (data !== undefined) {
sails.log.verbose('Sending 400 ("Bad Request") response: \n',data);
}
else sails.log.verbose('Sending 400 ("Bad Request") response');

// Only include errors in response if application environment
// is not set to 'production'. In production, we shouldn't
// send back any identifying information about errors.
if (sails.config.environment === 'production' && sails.config.keepResponseErrors !== true) {
data = undefined;
}

// If the user-agent wants JSON, always respond with JSON
// If views are disabled, revert to json
if (req.wantsJSON || sails.config.hooks.views === false) {
return res.jsonx(data);
}

// If second argument is a string, we take that to mean it refers to a view.
// If it was omitted, use an empty object (`{}`)
options = (typeof options === 'string') ? { view: options } : options || {};

// Attempt to prettify data for views, if it's a non-error object
var viewData = data;
if (!(viewData instanceof Error) && 'object' == typeof viewData) {
try {
viewData = require('util').inspect(data, {depth: null});
}
catch(e) {
viewData = undefined;
}
}

// If a view was provided in options, serve it.
// Otherwise try to guess an appropriate view, or if that doesn't
// work, just send JSON.
if (options.view) {
return res.view(options.view, { data: viewData, title: 'Bad Request' });
}

// If no second argument provided, try to serve the implied view,
// but fall back to sending JSON(P) if no view can be inferred.
else return res.guessView({ data: viewData, title: 'Bad Request' }, function couldNotGuessView () {
return res.jsonx(data);
});

};

60 changes: 60 additions & 0 deletions api/responses/created.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* 201 (CREATED) Response
*
* Usage:
* return res.created();
* return res.created(data);
* return res.created(data, 'auth/login');
*
* @param {Object} data
* @param {String|Object} options
* - pass string to render specified view
*/

module.exports = function created (data, options) {

// Get access to `req`, `res`, & `sails`
var req = this.req;
var res = this.res;
var sails = req._sails;

sails.log.silly('res.created() :: Sending 201 ("CREATED") response');

// Set status code
res.status(201);

// If appropriate, serve data as JSON(P)
// If views are disabled, revert to json
if (req.wantsJSON || sails.config.hooks.views === false) {
return res.jsonx(data);
}

// If second argument is a string, we take that to mean it refers to a view.
// If it was omitted, use an empty object (`{}`)
options = (typeof options === 'string') ? { view: options } : options || {};

// Attempt to prettify data for views, if it's a non-error object
var viewData = data;
if (!(viewData instanceof Error) && 'object' == typeof viewData) {
try {
viewData = require('util').inspect(data, {depth: null});
}
catch(e) {
viewData = undefined;
}
}

// If a view was provided in options, serve it.
// Otherwise try to guess an appropriate view, or if that doesn't
// work, just send JSON.
if (options.view) {
return res.view(options.view, { data: viewData, title: 'Created' });
}

// If no second argument provided, try to serve the implied view,
// but fall back to sending JSON(P) if no view can be inferred.
else return res.guessView({ data: viewData, title: 'Created' }, function couldNotGuessView () {
return res.jsonx(data);
});

};
Loading