Skip to content

Instantly share code, notes, and snippets.

View garth's full-sized avatar

Garth Williams garth

View GitHub Profile
@garth
garth / gist:4056917
Created November 12, 2012 00:28
Authorise middleware for use with passportjs or similar
//
// check that the user has the correct role for access to the route.
// to use, ensure that user has a roles array then add the following to routes
//
// app.get('/admin-route', app.authorise('admin', 'super-admin'), function (req, res) { ... })
//
app.authorise = app.authorize = function () {
var roles = Array.prototype.slice.call(arguments)
return function (req, res, next) {
if (req.user && _.any(roles, function (role) {
@garth
garth / BundleConfig.cs
Created November 8, 2012 14:40
Pre-compile emberjs handlebars templates with ASP.NET MVC Bundles
// use NuGet to add BundleTransformer to your project
// add ember.js and handlebars.js to the /Scripts folder
// in bundle config add something like this
bundles.Add(new ScriptBundle("~/bundles/templates").IncludeDirectory(
"~/templates", "*.handlebars"));
// or like this
bundles.Add(new ScriptBundle("~/bundles/teamplates").Include(
"~/templates/application.handlebars",
@garth
garth / gist:3966591
Created October 27, 2012 22:05
Pre-compile emberjs handlebars templates with browserify
var path = require('path')
var vm = require('vm')
var handlebarsjs = fs.readFileSync(__dirname + '/vendor/handlebars.js', 'utf8')
var emberjs = fs.readFileSync(__dirname + '/vendor/ember/ember.js', 'utf8')
browserify.register('handlebars', function (body, file) {
//dummy jQuery
var jQuery = function () { return jQuery }
jQuery.ready = function () { return jQuery }
jQuery.inArray = function () { return jQuery }
@garth
garth / Assetfile.rb
Created July 17, 2012 12:56 — forked from wagenet/Assetfile.rb
Ember Handlebars Precompile
require 'execjs'
class HandlebarsFilter < Filter
class << self
def contents
@@contents ||= [File.read("headless-ember.js"), File.read("ember.js")].join("\n")
end
def context
@@context ||= ExecJS.compile(contents)
@garth
garth / run-mocha.js
Created July 10, 2012 09:19 — forked from joeytrapp/run-mocha.js
JavaScript: PhantomJS Mocha Scrapper
/*global phantom:true, console:true, WebPage:true, Date:true*/
(function () {
var url, timeout, page, defer;
if (phantom.args.length < 1) {
console.log("Usage: phantomjs run-mocha.js URL [timeout]");
phantom.exit();
}
url = phantom.args[0];
@garth
garth / AutoCompleteIndex.cs
Created June 21, 2012 11:01
Create a fast auto complete index in memory with optional Soundex
/// <summary>
/// Creates a fast lookup for auto complete lists
/// </summary>
/// <typeparam name="T">Object type to to lookup</typeparam>
public class AutoComplete<T> {
private Dictionary<int, List<KeyValuePair<T, int>>> directory = new Dictionary<int, List<KeyValuePair<T, int>>>();
private int minMatchLength;
private bool enableSoundexMatching;
/// <summary>
@garth
garth / log.js
Created May 1, 2012 19:36
Wasted quite a bit of time today trying to get flatiron winston configured with custom levels and colors, so here's a working sample.
var winston = require('winston')
require('winston-mongodb')
// prepare some custom log levels
var customLevels = {
levels: {
debug: 0,
info: 1,
warning: 2,
error: 3
@garth
garth / gist:2477814
Created April 24, 2012 08:19
Configure IIS for iisnode
<configuration>
<system.webServer>
<handlers>
<add name="iisnode" path="app.js" verb="*" modules="iisnode" />
</handlers>
<!-- stop IIS from replacing non 200 response bodies -->
<httpErrors existingResponse="PassThrough" />
<!-- send all incoming requests to nodejs -->
@garth
garth / Jakefile.js
Created January 16, 2012 18:37 — forked from wagenet/Assetfile.rb
Precompile .handlebars templates with Jake (nodejs)
var fs = require('fs')
var vm = require('vm')
var handlebarsjs = fs.readFileSync('path/to/handlebars.js', 'utf8')
var emberjs = fs.readFileSync('path/to/ember.js', 'utf8')
var templatesDir = 'path/to/template/dir'
desc('Compile all .handlebars templates')
task({ 'handlebars': [] }, function () {
process.stdout.write('Compiling .handlebars templates')
@garth
garth / handlebars-defined-helper.js
Created December 1, 2011 14:05
Handlebars block helper for only outputting when a property is defined
// only output the block if the object property exists/is defined
//
// {{#defined property}}
// Property exists
// {{/defined}}
Handlebars.registerHelper('defined', function(property, block) {
return typeof(this[property]) !== 'undefined' ? block(this) : '';
});