Skip to content

Instantly share code, notes, and snippets.

@crapthings
Created December 1, 2013 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crapthings/7733187 to your computer and use it in GitHub Desktop.
Save crapthings/7733187 to your computer and use it in GitHub Desktop.
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