Skip to content

Instantly share code, notes, and snippets.

@gentunian
Last active August 29, 2015 14:27
Show Gist options
  • Save gentunian/cadc9ef08f6aeae6705c to your computer and use it in GitHub Desktop.
Save gentunian/cadc9ef08f6aeae6705c to your computer and use it in GitHub Desktop.
tabular server-side selector
selector: function( userId ) {
var roleNames = _.pluck(Roles.find({users: {$in: [userId]}}, {fields: {name: 1, _id: 0}}).fetch(), 'name');
var users = [];
var query;
if (_.contains(roleNames, 'manager')) {
// this wrapping is just for keeping the code readable
query = Meteor.users.find({}, { fields: {_id: 1 }, transform: function (doc) { doc.users = doc._id; return doc;}});
// or:
// query = { users: [ userId ] };
} else if (_.contains(roleNames, 'admin')) {
query = Groups.find({ users: { $in: [userId] }}, { fields: { users: 1, _id:0 }});
} else if (_.contains(roleNames, 'user')) {
// this may sound silly, but keeps the code readable
query = Meteor.users.find({_id: userId},{ fields: {_id: 1 }, transform: function (doc) { doc.users = doc._id; return doc;}});
}
users = _.flatten( _.pluck( query.fetch(), 'users' ));
return { createdBy: {$in: users }};
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment