Skip to content

Instantly share code, notes, and snippets.

View olivernn's full-sized avatar

Oliver Nightingale olivernn

View GitHub Profile
@olivernn
olivernn / active_model_errors_patch.rb
Created January 27, 2011 13:13
stop invalid json after calling to_json on active model errors
module ActiveModel
class Errors
def as_json(options=nil)
self
to_hash
end
def to_hash
hash = ActiveSupport::OrderedHash.new
each { |k, v| (hash[k] ||= []) << v }
class Admin::ReportsController < AdminController
respond_to :csv
def trips
respond_with TripReport.new
end
end
require 'rubygems'
require 'sinatra'
get "/" do
erb :index
end
get "/slow_network" do
sleep(2)
"Done"
@olivernn
olivernn / bill.js
Created August 24, 2011 11:22
js model validations
var Bill = Model('bill', function () {
this.use(Model.validations)
this.validatesPresenceOf(['attribute1', 'attribute2'])
this.validatesPresenceOf('another_attribute', {
'condition': function () {
// some condition if you need it
return true
}
})
@olivernn
olivernn / Post.js
Created January 10, 2012 16:47
The Plugin
var Post = Model('post', function () {
this.use(Model.Lunr(function () {
this.field('body')
this.field('title', {
'multiplier': 10
})
}))
})
@olivernn
olivernn / gist:1640750
Created January 19, 2012 15:59
ajaz setup yo
$.ajaxSetup({
beforeSend: function(xhr) {
xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'));
}
});
@olivernn
olivernn / cart_app.js
Created May 2, 2012 21:34
multiple 'apps' within one davis app
var cartApp = function () {
this.scope('/cart', function () {
this.post('/items', function (req) {
// create an item
})
this.del('/items/:id', function (req) {
// delete a specific item from your cart
})
@olivernn
olivernn / app.js
Created June 25, 2012 09:21
Simple Davis.js Example
var app = Davis(function () {
this.get('/foo', function () {
alert('you clicked on foo!')
})
})
var app = Davis(function () {
this.configure(function () {
this.linkSelector = 'nav a'
this.formSelector = 'nav form'
})
this.get('/', function (req) {
showHomePage()
})
@olivernn
olivernn / app.js
Created June 26, 2012 09:18
davis with hash routing
Davis.extend(Davis.hashRouting({ forceHashRouting: true }))
app = Davis(function () {
this.settings.handleRouteNotFound = true
this.get('#/foo', function () {
console.log('foo')
})