Skip to content

Instantly share code, notes, and snippets.

View mrjjwright's full-sized avatar

John Wright mrjjwright

View GitHub Profile
@mrjjwright
mrjjwright / underscore.css.coffee
Created May 20, 2011 03:41
underscore.css : converts an object to CSS declarations
# Usage: _.toCSS(object)
# Returns properly formatted CSS.
# Object cannot be deeply nested as this is a simple object/CSS mapping and it reads better.
# If you want to do something dynamic to generate the CSS, do it in a JS function that returns the object.
#
_css =
toCSS: (obj) ->
propToCSS = (key, value) ->
css = ''
for selector of obj
@mrjjwright
mrjjwright / sources_view.coffee
Created May 18, 2011 14:32
Example CoffeeKup backbone view
template = CoffeeKup.compile ->
div class: 'section first sources', ->
div id: 'sources-overview', class: 'category', ->
a href: '/sources/overview', -> 'Overview'
div class: 'section', ->
for source in @sources
div id: "sources-#{source.uuid}", class: 'category source', ->
span class: 'searching'
a href: "/sources/#{source.uuid}", -> source.name
@mrjjwright
mrjjwright / Cakefile.coffee
Created May 15, 2011 17:05
Cake file that packages files from npm or locally. Looks in local node_modules for npm packages.
fs = require 'fs'
assets = require './frontend/js/assets'
exec = require('child_process').exec
task 'dev:js_lib', 'Rebuild the js libs', (options) ->
js = ''
js_local = assets.js_local
for key of assets.js
if assets.js[key] is "npm"
try
@mrjjwright
mrjjwright / package.coffee
Created May 15, 2011 14:13
Put this in Cakefile to write out package.coffee to NPM package.json
fs = require 'fs'
CoffeeScript = require 'coffee-script'
task "build:package", "Builds the package.json from it's coffee source", (options) ->
js = CoffeeScript.compile "return #{fs.readFileSync('package.coffee', 'utf8')}"
json = JSON.stringify eval(js), null, 2
fs.writeFileSync 'package.json', json
console.log('Successfully wrote out package.json')
@mrjjwright
mrjjwright / secure.coffee
Created April 30, 2011 04:31
My approach to node security
# Security code for a user
uuid = require "node-uuid"
crypto = require "crypto"
util = require("util")
systemSalt = "49caa18ae0d91e9ad610eba6bf6328172ae026a497ef6e79dcb0b7b1eb1ca534a047405eb8f0f05b2e513d548a3d97a0d4a2634593ac98d5e1db64212837b254"
# Default fingerprint function
fingerprint = (user) ->
return user.email
@mrjjwright
mrjjwright / address_all_or_nothing.rb
Created April 29, 2011 18:32
Address all or nothing validation
class Address < ActiveRecord::Base
attr_accessible :address1, :address2, :phone, :city, :state, :zip, :country, :website_url, :name
def validate
required_set = [:address1, :city, :state, :zip, :country]
matches = required_set.map {|my_method| self.send(my_method).present? }.reduce(0) {|a,b| a + (b == true ? 1 : 0) }
self.errors.add(:address1, "Must have a full address!") if matches.between?(1, 4)
end
end
@mrjjwright
mrjjwright / backbone_dnode.coffee
Created April 27, 2011 03:48
Backbone playing with Dnode
# Load the remote RB db object, our connection to the backend SQLite or MongoDB
DNode.connect (rbdb) ->
RB.db = rbdb
# Fetch the logged in Backbone user if any
RB.user.fetch success: -> Backbone.history.loadUrl()
# Overwrite the Backbone sync method to use Dnode and rbdb
# The Recordbook database has a MongoDB compatible API, even if it sits on SQLite
Backbone.sync = (method, model, success, error) ->
collectionName = model.collectionName()
@mrjjwright
mrjjwright / show_num_ordered.js
Created April 19, 2011 15:57
Shows the number ordered over a product item after an item has been added to a cart.
$('.product-order a').bind("ajax:success", function(e, lineItemQuantity){
var $this = $(this),
$confirmObj = $this.parent('.product-order').siblings('.product-image').children('.order-over');
if( !$confirmObj.is('visible') && !($confirmObj.is('animated')) ){
$confirmObj.fadeIn();
$confirmObj.children('p').children('.order-count').text(lineItemQuantity);
setTimeout( function(){$confirmObj.fadeOut();}, 3000);
}
});
@mrjjwright
mrjjwright / backbone.history.html5.coffee
Created March 1, 2011 16:55
Add pretty html5 history support to Backbone
#
# Replaces Backbone.History with support for HTML5 history API if browser supports it.
# To use, setup your controllers as usual and try it with a browser that supports the HTML5 history API.
# For browsers that don't support the HTML5 API, this will fall back to using the default Backbone hash routing.
#
# I have only tested this on my project in Firefox, Safari and Chrome so let me know.
#
pushSupport = window.history? and window.history.pushState?
if pushSupport and Backbone? and Backbone.History?
rootUrl = document.location.protocol+'//'+(document.location.host or document.location.hostname)
@mrjjwright
mrjjwright / _templatePlusLayout.coffee
Created February 23, 2011 18:06
Add partials, layouts to underscore.js templates
#
# Caches, renders, ands adds supports for partials and layout to underscore.js templating
#
# Pass in one options object to renderTemplate:
# dir: the root dir where all the templates are located, defaults to views. Paths are resolved as e.g dir/partial.html or dir/layout.html.
# partial(s): one or more partial names (without the *.html) to compile, cache, render, and optionally return.
# If layout is present, the partials will be passed to the layout template with keys as the partial names
# and the layout html will be returned.
# partialPrefix: (optional) a name to prepend to all partials. E.g. passing in "products." will load dir/products.partial.html
# layout: the name of a layout template (without the *.html). If missing the last or only partial html will be returned.