Skip to content

Instantly share code, notes, and snippets.

danielbeardsley / json_controller.rb
Created Nov 19, 2009
A Generalized Json Controller for use with extjs-mvc and Restful CRUD
View json_controller.rb
class JsonController < ApplicationController
rescue_from ActiveRecord::RecordNotFound do |exception|
render :json => { :success => false }, :status => :not_found
before_filter :find_record, :only => [ :get, :update, :destroy ]
layout 'base'
danielbeardsley / active_record_model_discovery.rb
Created May 17, 2010
Easily retrieve a list of all Models in your Ruby on Rails application
View active_record_model_discovery.rb
module ModelDiscovery
def self.models_hash
@active_record_classes ||= valid_active_record_classes.index_by do |model|
def self.model_file_names
@model_file_names ||= Dir['app/models/*.rb'].map {|f|
File.basename(f, '.*')
danielbeardsley / un_quoted_string.rb
Created Jun 14, 2010
A Ruby String Class that doesn't quote itself when .to_json is called
View un_quoted_string.rb
# Represents a string that isn't surrounded by quotes when converted to JSON.
# Originally created to be able to create ruby hashes containing javascript
# function definitions that could be converted to JSON easily.
# Use it like this:
# {:number = > 10,
# :date =>,
# :regular_string => "Some Quoted Text",
# :unquoted_string => UnQuotedString("function(){ alert('you can create a function'); }")
# }.to_json
danielbeardsley / capistrano_create_git_tags.rb
Created Mar 1, 2011
A simple capistrano setup that Auto-creates git tags on deploy, and supports multiple deployment environments
View capistrano_create_git_tags.rb
task :production do
# :deployment variable should match task name
set :deployment, 'production'
set :deploy_to, "/home/user/www/#{application}"
set :branch, "master"
task :staging do
# :deployment variable should match task name
danielbeardsley / merge_util.js
Created Mar 15, 2011
Javascript Merge functions: merge, mergeCopy, and mergeDeep (compatible with browsers and nodejs)
View merge_util.js
var util = exports || {};
util.mergeDeep = function (A, B, depth) {
var forever = depth == null;
for (var p in B) {
if (B[p] != null && B[p].constructor==Object && (forever || depth > 0)) {
A[p] = util.mergeDeep(
A.hasOwnProperty(p) ? A[p] : {},
danielbeardsley / acts_as_sequential.rb
Created Apr 4, 2011
Adds .previous and .next named scopes to your Active Record model, treating the records in your database like a sequential list with an order of your choosing.
View acts_as_sequential.rb
# Adds .previous and .next class methods to your model treating the records in your database like a sequential list
# Put this in your model definition:
# acts_as_sequential :column => :created_at # you can also use the :dir => 'DESC' option (ASC is the default)
# Then use it like this:
# -> next record in sequence
# Model.other_named_scope.previous(current_record) -> previous record in sequence
danielbeardsley / node-gzip.js
Created May 6, 2011
Simple nodejs interface to the gzip utility found on every *nix system
View node-gzip.js
/* Provides a simple interface to the gzip executable found on the system, should work in all *nix environments.
* Keep in mind that this spawns a process, so don't go using for every request, only use it in non-time-sensitive places
* Thanks mostly go to 3Rd-Eden who created the original code in a pull-request to Socket.IO-node
var child_process = require('child_process');
module.exports.gzip = function(buffer, callback){
var gzip = child_process.spawn('gzip', ['-9'])
, buffers = []
danielbeardsley / html_template.js
Created Jun 22, 2011
A template renderer for express.js that processes plain vanilla HTML and has the option to strip newlines.
View html_template.js
// Usage:
// express_app.register('html', htmlRenderer({stripNewlines: true}));
// ...
// express_app.get('/plain', function(req, res){
// res.render('./path_to_html_file');
// })
function htmlRenderer(opt){
var stripNewlines = opt && opt.stripNewlines;
danielbeardsley / compose_connect_middlware.js
Created Jun 22, 2011
Allows easy composition of a chain of connect.js-style middlewares
View compose_connect_middlware.js
// Allows easy composition of a chain of connect.js-style middlewares in node.js
// given:
// function A(req,res,next){}
// function B(req,res,next){}
// composeMiddleware(A,B) will return a function(req,res,next) that passes the request
// through both handlers, just like Connect. If a next() is called with an error
// the call chain is stopped and the error is passed to the topmost next()
// If next() is called with the second parameter set to 'break' "next(null, 'break')"
// The chain is halted and route control is passed back to the original next()
function composeMiddleware(){
danielbeardsley / grepReplace
Created Sep 26, 2011
Shell Script for grep-based search and replace recursively in files
View grepReplace
function ConfirmOrExit() {
echo -n "Continue? (y / n) :"
if [ $CONFIRM != 'y' ]