Created
August 19, 2010 07:16
-
-
Save visnup/537280 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
commit e7f8e63585ae24968a01a58ff181674187601e57 | |
Author: Visnu Pitiyanuvath <[email protected]> | |
Date: Thu Aug 19 00:12:41 2010 -0700 | |
team slugs | |
diff --git a/db/slug.js b/db/slug.js | |
new file mode 100644 | |
index 0000000..f56a883 | |
--- /dev/null | |
+++ b/db/slug.js | |
@@ -0,0 +1,6 @@ | |
+db.Team.find().forEach(function(t) { | |
+ t.slug = t.name.toLowerCase().replace(/\W+/g, '-'); | |
+ db.Team.save(t); | |
+}); | |
+ | |
+db.Team.ensureIndex({ slug: 1 }); | |
diff --git a/models/models.coffee b/models/models.coffee | |
index 0f567b4..ccf0501 100644 | |
--- a/models/models.coffee | |
+++ b/models/models.coffee | |
@@ -36,11 +36,12 @@ class Team | |
errors.concat _.compact _.flatten [member.validate() for member in @members] | |
beforeSave: (fn) -> | |
- threads = @members.length | |
- return fn() unless threads | |
- for member in @members | |
- member.save (error, res) -> | |
- fn() if --threads is 0 | |
+ @generateSlug => | |
+ threads = @members.length | |
+ return fn() unless threads | |
+ for member in @members | |
+ member.save (error, res) -> | |
+ fn() if --threads is 0 | |
beforeInstantiate: (fn) -> | |
query = { _id: { $in: _.pluck @members, '_id' }} | |
@@ -68,6 +69,14 @@ class Team | |
member.inviteTo this, -> | |
fn() if --threads is 0 | |
+ generateSlug: (fn, attempt) -> | |
+ @slug = attempt || @name.toLowerCase().replace(/\W+/g, '-') | |
+ Team.fromParam @slug, (error, existing) => | |
+ if !existing? or existing.id() == @id() | |
+ fn() # no conflicts | |
+ else | |
+ @generateSlug fn, @slug + '-' # try with another - | |
+ | |
_.extend Team, { | |
joyentTotal: (teams) -> | |
_.reduce _.pluck(teams, 'joyent_count'), 0, (memo, num) -> | |
@@ -215,4 +224,9 @@ nko.Person = Person | |
Mongo.blessAll nko | |
+Team.prototype.toParam = -> @slug | |
+ | |
+Team.fromParam = (id, options, fn) -> | |
+ @first { slug: id }, options, fn | |
+ | |
_.extend exports, nko |
what part? all of the callback stuff would still exist.
and actually, coffee-script has cleaned up the alternative too, which is pretty ugly whenever I type it out in plain javascript.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you guys need to kinda give up on coffee script for now