Skip to content

Commit

Permalink
Adds PR#3228 (balderdashy#3228) on top of sails.js rel0.12-rc3
Browse files Browse the repository at this point in the history
  • Loading branch information
vazarely committed Dec 11, 2015
1 parent 74fcd0e commit 02f15b5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

Sails.js is a web framework that makes it easy to build custom, enterprise-grade Node.js apps. It is designed to resemble the MVC architecture from frameworks like Ruby on Rails, but with support for the more modern, data-oriented style of web app development. It's especially good for building realtime features like chat.

## NOTE  
2015-Dec-11: This fork adds the following PRs on top of sails.js rel 0.12-rc3
- [PR#3228: Added support for JSON arrays](https://github.com/balderdashy/sails/pull/3228)


## Installation  
**With [node](http://nodejs.org) [installed](http://sailsjs.org/#!documentation/new-to-nodejs):**
Expand Down
12 changes: 12 additions & 0 deletions lib/hooks/blueprints/actionUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,18 @@ var actionUtil = {
throw new Error('Invalid `req.options.values.blacklist`. Should be an array of strings (parameter names.)');
}

//Special handling if the body is an array, does what it would do usally but on each array object.
if(_.isArray(req.body)){
sails.log.silly("Reqest body is an array");
var values = [];
_.each(req.body, function(element, key){
values[key] = mergeDefaults(element, _.omit(req.options.values, 'blacklist'));
values[key] = _.omit(values[key], blacklist || []);
values[key] = _.omit(values[key], function (p){ if (_.isUndefined(p)) return true; });
});
return values;
}

// Merge params into req.options.values, omitting the blacklist.
var values = mergeDefaults(req.params.all(), _.omit(req.options.values, 'blacklist'));

Expand Down
2 changes: 1 addition & 1 deletion lib/hooks/blueprints/actions/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports = function createRecord (req, res) {
Model.subscribe(req, newInstance);
Model.introduce(newInstance);
}
Model.publishCreate(newInstance.toJSON(), !req.options.mirror && req);
Model.publishCreate(newInstance, !req.options.mirror && req);
}

// Send JSONP-friendly response if it's supported
Expand Down
22 changes: 21 additions & 1 deletion lib/hooks/pubsub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,26 @@ module.exports = function(sails) {

},

/**
* Publish the creation of model or an array of models
*
* @param {[Object]|Object} models
* - the data to publish
*
* @param {Request|Socket} req - Optional request for broadcast.
* @api private
*/
publishCreate: function(models, req, options){
var self = this;

// Pluralize so we can use this method regardless of it is an array or not
models = self.pluralize(models);

//Publish all models
_.each(models, function(values){
self.publishCreateSingle(values.toJSON(), req, options);
})
},

/**
* Publish the creation of a model
Expand All @@ -1088,7 +1108,7 @@ module.exports = function(sails) {
* @api private
*/

publishCreate: function(values, req, options) {
publishCreateSingle: function(values, req, options) {
var self = this;
var reverseAssociation;

Expand Down
15 changes: 15 additions & 0 deletions test/integration/router.APIScaffold.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,21 @@ describe('router :: ', function() {
});
});

describe('a post of JSON array request to /:controller/create', function() {
it('should return JSON array for a newly created instances of the test model', function (done) {
httpHelper.testRoute('post', {
url: 'empty/create',
json: true,
body: [{stuff: "bad"}, {stuff: "ghuud"}]
}, function (err, response) {
if (err) return done(new Error(err));

assert(response.body instanceof Array && response.body[1].stuff === "ghuud", Err.badResponse(response));
done();
});
});
});

describe('with pluralize turned on', function() {

before(function() {
Expand Down

0 comments on commit 02f15b5

Please sign in to comment.