Created
December 1, 2013 12:43
-
-
Save crapthings/7733187 to your computer and use it in GitHub Desktop.
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
exports = @ | |
@App = {} | |
App.login = App.signin = Meteor.loginWithPassword | |
App.logout = App.signout = Meteor.logout | |
App.isLoggedIn = -> return true if Meteor.user() | |
class App.Collection | |
constructor: (collectionName) -> | |
# initialize a collection | |
collection = null | |
_capitalizeCollectionName = collectionName.capitalize() | |
_singularizeCollectionName = collectionName.singularize() | |
exports[_capitalizeCollectionName] = collection = new Meteor.Collection collectionName, | |
transform: (doc) -> | |
_.extend doc, | |
creator: Meteor.users.findOne doc.creatorId | |
# collection hooks | |
collection.before.find (userId) -> return false if userId | |
collection.before.findOne (userId) -> return false if userId | |
collection.before.insert (userId) -> return false if userId | |
collection.before.update (userId) -> return false if userId | |
collection.before.remove (userId) -> return false if userId | |
collection.before.insert (userId, doc) -> | |
_.extend doc, | |
createdAt: new Date() | |
timestamp: Date.now() | |
collection.before.insert (userId, doc) -> | |
if userId | |
_.extend doc, | |
creatorId: userId | |
# server | |
if Meteor.isServer | |
Meteor.publish collectionName, -> | |
exports[_capitalizeCollectionName].find {}, | |
sort: | |
timestamp: -1 | |
Meteor.publish _singularizeCollectionName, (docId) -> | |
check docId, Match.Optional(String) | |
exports[_capitalizeCollectionName].find _id: docId | |
if Match.isClient | |
Router.map -> | |
@route '' | |
return collection | |
# collection methods | |
Meteor.methods | |
insert: (collection, doc) -> | |
check collection, Match.Optional(String) | |
check doc, Match.Optional(Object) | |
collection = collection.capitalize() | |
exports[collection].insert doc | |
update: (collection, docId, doc) -> | |
check collection, Match.Optional(String) | |
check docId, Match.Optional(String) | |
check doc, Match.Optional(Object) | |
collection = collection.capitalize() | |
exports[collection].update docId, $set: doc | |
remove: (collection, docId) -> | |
check collection, Match.Optional(String) | |
check docId, Match.Optional(String) | |
collection = collection.capitalize() | |
exports[collection].remove doc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment