Skip to content

Commit

Permalink
shuffle judges [finishes #16143455]
Browse files Browse the repository at this point in the history
  • Loading branch information
Visnu Pitiyanuvath committed Jul 23, 2011
1 parent 45f34f6 commit 9747b65
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
1 change: 1 addition & 0 deletions config/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ app.error(function(e, req, res, next) {

// utilities & hacks
require('../lib/render2');
require('../lib/underscore.shuffle');

// db
var mongoose = require('mongoose')
Expand Down
3 changes: 2 additions & 1 deletion controllers/judges.coffee
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
app = require '../config/app'
Person = app.db.model 'Person'
_ = require 'underscore'

# index
app.get '/judges', (req, res, next) ->
Person.find { role: 'judge' }, (err, judges) ->
return next err if err
res.render2 'judges', judges: judges
res.render2 'judges', judges: _.shuffle(judges)

app.get '/judges/nominations', (req, res, next) ->
Person.find { role: 'nomination' }, (err, judges) ->
Expand Down
16 changes: 3 additions & 13 deletions helpers/index.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
crypto = require 'crypto'
_ = require 'underscore'
markdown = require('markdown').parse

md5 = (str) ->
Expand All @@ -9,21 +10,10 @@ md5 = (str) ->
gravatar_url = (md5, size) ->
"http://gravatar.com/avatar/#{md5}?s=#{size}&d=retro"

shuffle = (obj) ->
shuffled = []
obj.forEach (value, index, list) ->
if index == 0
shuffled[0] = value
else
rand = Math.floor(Math.random() * (index + 1))
shuffled[index] = shuffled[rand]
shuffled[rand] = value
shuffled

module.exports = (app) ->
app.helpers
inspect: require('util').inspect
_: require('underscore')
_: _
markdown: (str) -> if str? then markdown(str) else ''
avatar_url: (person, size = 30) ->
if person.github?.gravatarId
Expand All @@ -35,7 +25,7 @@ module.exports = (app) ->
gravatar_url md5(person.email.trim().toLowerCase()), size
else
'/images/gravatar_fallback.png'
sponsors: (fn) -> shuffle(sponsors).forEach fn
sponsors: (fn) -> _.shuffle(sponsors).forEach fn

app.dynamicHelpers
session: (req, res) -> req.session
Expand Down
13 changes: 13 additions & 0 deletions lib/underscore.shuffle.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
_ = module.exports = require 'underscore'

_.mixin
shuffle: (array) ->
shuffled = []
_.each array, (value, index, list) ->
if index == 0
shuffled[0] = value
else
rand = Math.floor(Math.random() * (index + 1))
shuffled[index] = shuffled[rand]
shuffled[rand] = value
shuffled

0 comments on commit 9747b65

Please sign in to comment.