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

Commit 6256daa

Browse files
committed
Merge pull request #29 from mphasize/through-assocs
Handle indexing of *hasMany-through* associations.
2 parents f706fc2 + d056a4d commit 6256daa

File tree

2 files changed

+13
-43
lines changed

2 files changed

+13
-43
lines changed

templates/advanced/api/blueprints/_util/actionUtil.js

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -38,42 +38,6 @@ module.exports = {
3838
return associations;
3939
},
4040

41-
/**
42-
* AccessControl proxy function, expects a Waterline query object for before* hooks and an array of records for after* hooks.
43-
* @param {Collection} model Waterline collectio, from parseModel
44-
* @param {String} action blueprint or hook, e.g. beforeFind, afterFind, beforeCreate, afterCreate --> translates to accessControlBeforeFind, etc.
45-
* @param {Object} options Options: query {Query} Waterline query object for before* hooks, records: {Array} records for after* hooks, {Model} current user record
46-
* @return {Query|Array} returns the modified query (with altered criteria) for before* or the filtered/extended record array for after*.
47-
*/
48-
accessControl: function ( model, action, options, callback ) {
49-
var primaryArgument = null;
50-
var capitaliseFirstLetter = function ( string ) {
51-
return string.charAt( 0 ).toUpperCase() + string.slice( 1 );
52-
};
53-
54-
if ( action.indexOf( "before" ) === 0 ) {
55-
if ( !options.criteria ) throw new Error( "accessControl before* hook needs option: criteria!" );
56-
primaryArgument = options.criteria;
57-
} else if ( action.indexOf( "after" ) === 0 ) { // turns out that access control after querying the database is a bad idea, because counting and pagination patterns are broken
58-
if ( !options.records ) throw new Error( "accessControl after* hook needs option: records!" );
59-
primaryArgument = options.records;
60-
} else {
61-
throw new Error( "accessControl action '" + action + "' doesn't fit into before/after pattern." );
62-
}
63-
64-
action = capitaliseFirstLetter( action );
65-
66-
if ( typeof model[ "control" + action ] === "function" ) {
67-
primaryArgument = model[ "control" + action ]( primaryArgument, options, callback );
68-
} else {
69-
console.log( "Dev: Model " + model.identity + " is missing control " + action + "!" );
70-
if ( callback ) {
71-
callback( null, primaryArgument );
72-
}
73-
}
74-
return primaryArgument;
75-
},
76-
7741
/**
7842
* helper function to populate a record with an array for indexes for associated models, running various Waterline queries on the join tables if neccessary ( defined as: include -> index )
7943
* @param {Waterine Collection} parentModel [description]
@@ -144,8 +108,6 @@ module.exports = {
144108
var aliasFilter = req.param( 'populate' );
145109
var shouldPopulate = _options.populate;
146110

147-
console.log( "Populate: ", aliasFilter, shouldPopulate );
148-
149111
// Convert the string representation of the filter list to an Array. We
150112
// need this to provide flexibility in the request param. This way both
151113
// list string representations are supported:

templates/advanced/api/services/Ember.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,19 @@ var Ember = {
7979
return filtered;
8080
}, [] );
8181
}
82-
if ( assoc.include === "index" && associatedRecords[ assoc.alias ] ) record[ assoc.alias ] = _.reduce( associatedRecords[ assoc.alias ], function ( filtered, rec ) {
83-
if ( rec[ emberModelIdentity ] === record.id ) filtered.push( rec.id );
84-
return filtered;
85-
}, [] );
82+
if ( assoc.include === "index" && associatedRecords[ assoc.alias ] ) {
83+
if ( assoc.through ) { // handle hasMany-Through associations
84+
if ( assoc.include === "index" && associatedRecords[ assoc.alias ] ) record[ assoc.alias ] = _.reduce( associatedRecords[ assoc.alias ], function ( filtered, rec ) {
85+
if ( rec[ emberModelIdentity ] === record.id ) filtered.push( rec[ assoc.collection ] );
86+
return filtered;
87+
}, [] );
88+
} else {
89+
record[ assoc.alias ] = _.reduce( associatedRecords[ assoc.alias ], function ( filtered, rec ) {
90+
if ( rec[ emberModelIdentity ] === record.id ) filtered.push( rec.id );
91+
return filtered;
92+
}, [] );
93+
}
94+
}
8695
// @todo if assoc.include startsWith index: ... fill contents from selected column of join table
8796
if ( assoc.include === "link" ) {
8897
links[ assoc.alias ] = sails.config.blueprints.prefix + "/" + modelPlural.toLowerCase() + "/" + record.id + "/" + assoc.alias;
@@ -139,7 +148,6 @@ var Ember = {
139148
if ( array.length > 0 ) {
140149
if ( !_.isNumber( array[ 0 ] ) && !_.isString( array[ 0 ] ) ) { // this is probably an array of records
141150
var model = sails.models[ pluralize( key, 1 ) ];
142-
console.log( "Sideloaded records model: ", model.identity );
143151
Ember.linkAssociations( model, array );
144152
}
145153
}

0 commit comments

Comments
 (0)