Last active
August 29, 2015 14:27
-
-
Save gentunian/cadc9ef08f6aeae6705c to your computer and use it in GitHub Desktop.
tabular server-side selector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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