Skip to content

Commit

Permalink
Allow object to be passed in to publishAdd()
Browse files Browse the repository at this point in the history
  • Loading branch information
mikermcneil committed Dec 22, 2014
1 parent c499158 commit 83c2808
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions lib/hooks/pubsub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -816,22 +816,49 @@ module.exports = function(sails) {
* @param {[type]} socketToOmit [description]
*/

publishAdd: function(id, alias, idAdded, req, options) {
publishAdd: function(id, alias, added, req, options) {

// Make sure there's an options object
options = options || {};

// Enforce valid usage
var invalidId = !id || _.isObject(id);
var invalidAlias = !alias || !_.isString(alias);
var invalidAddedId = !idAdded || _.isObject(idAdded);
var invalidAddedId = !added || _.isArray(added);
if ( invalidId || invalidAlias || invalidAddedId ) {
return sails.log.error(
'Invalid usage of ' + this.identity +
'`publishAdd(id, alias, idAdded, [socketToOmit])`'
'`publishAdd(id, alias, idAdded|recordAdded, [socketToOmit])`'
);
}

// Get the model on the opposite side of the association
var reverseModel = sails.models[_.find(this.associations, {alias: alias}).collection];

// Determine whether `added` was provided as a pk value or an object
var idAdded;

// If it is a pk value, we'll turn it into `idAdded`:
if (!_.isObject(added)) {
idAdded = added;
added = undefined;
}
// Otherwise we'll leave it as `added` for use below, and determine `idAdded` by examining the object
// using our knowledge of what the name of the primary key attribute is.
else {
idAdded = added[reverseModel.primaryKey];

// If we don't find a primary key value, we'll log an error and return early.
if (!_.isString(idAdded) && !_.isNumber(idAdded)) {
sails.log.error(
'Invalid usage of publishAdd(): expected object provided '+
'for `recordAdded` to have a "%s" attribute', reverseModel.primaryKey
);
return;
}
}

// Lifecycle event
if (sails.util.isFunction(this.beforePublishAdd)) {
this.beforePublishAdd(id, alias, idAdded, req);
}
Expand All @@ -858,9 +885,6 @@ module.exports = function(sails) {

if (!options.noReverse) {

// Get the reverse association
var reverseModel = sails.models[_.find(this.associations, {alias: alias}).collection];

var data;

// Subscribe to the model you're adding
Expand Down

0 comments on commit 83c2808

Please sign in to comment.