Last active
August 29, 2015 14:22
-
-
Save patrickml/68f64d927fba774a742d to your computer and use it in GitHub Desktop.
Simple Activity Log by Collection for MeteorJS
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
/** | |
* Example Document | |
{ | |
_id : 'xDvcFlsQws', | |
activity : 'Project Created', | |
createdAt : '1231213412312' | |
collection : 'projects', | |
documentId : 'xJ4iTyxDw2', | |
userId : 'pQ4iTyxDw7' | |
} | |
* @type {Mongo.Collection} | |
*/ | |
Activity = new Meteor.Collection('activity'); | |
Activity.allow({ | |
'insert': function(userId, doc) { | |
return true; | |
}, | |
'update': function(userId, docs, fields, modifier) { | |
return true; | |
}, | |
'remove': function(userId, docs) { | |
return true; | |
} | |
}); | |
Activity.before.insert(function (userId, doc) { | |
//Check if the document has the required entries | |
if(!doc.activity || !doc.collection || !doc.documentId) | |
return false; | |
//Only insert if ran on the server otherwise the server will deny the insert | |
if(Meteor.isServer) { | |
//Format the document | |
doc.createdAt = Date.now(); | |
doc.userId = userId; | |
} else { | |
return false; | |
} | |
}); | |
Activity.helpers({ | |
/** | |
* Gets the related document | |
* @returns {any} | |
*/ | |
'getDocument': function () { | |
return window[this.collection].findOne({ _id: this.documentId }); | |
}, | |
/** | |
* Gets the user who performed the activity | |
* @returns {any} | |
*/ | |
'getUser': function () { | |
return window[this.collection].findOne({ _id: this.userId }); | |
} | |
}); |
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
Projects.after.insert(function (userId, doc) { | |
if(Meteor.isServer) { | |
//Log the fact that a Project was created | |
Activity.insert({ | |
'activity' : 'Project Created', | |
'collection' : 'Projects', | |
'documentId' : doc._id | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment