Skip to content

Instantly share code, notes, and snippets.

View danielbeardsley's full-sized avatar

Daniel Beardsley danielbeardsley

View GitHub Profile
@danielbeardsley
danielbeardsley / json_controller.rb
Created November 19, 2009 05:57
A Generalized Json Controller for use with extjs-mvc and Restful CRUD
class JsonController < ApplicationController
rescue_from ActiveRecord::RecordNotFound do |exception|
render :json => { :success => false }, :status => :not_found
end
before_filter :find_record, :only => [ :get, :update, :destroy ]
layout 'base'
@danielbeardsley
danielbeardsley / active_record_model_discovery.rb
Created May 17, 2010 22:14
Easily retrieve a list of all Models in your Ruby on Rails application
module ModelDiscovery
def self.models_hash
@active_record_classes ||= valid_active_record_classes.index_by do |model|
model.table_name
end
end
def self.model_file_names
@model_file_names ||= Dir['app/models/*.rb'].map {|f|
File.basename(f, '.*')
@danielbeardsley
danielbeardsley / un_quoted_string.rb
Created June 14, 2010 21:16
A Ruby String Class that doesn't quote itself when .to_json is called
# 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 => Time.now,
# :regular_string => "Some Quoted Text",
# :unquoted_string => UnQuotedString("function(){ alert('you can create a function'); }")
# }.to_json
@danielbeardsley
danielbeardsley / capistrano_create_git_tags.rb
Created March 1, 2011 16:25
A simple capistrano setup that Auto-creates git tags on deploy, and supports multiple deployment environments
task :production do
# :deployment variable should match task name
set :deployment, 'production'
set :deploy_to, "/home/user/www/#{application}"
set :branch, "master"
find_and_execute_task("deploy:tags:schedule_creation")
end
task :staging do
# :deployment variable should match task name
@danielbeardsley
danielbeardsley / merge_util.js
Created March 15, 2011 09:28
Javascript Merge functions: merge, mergeCopy, and mergeDeep (compatible with browsers and nodejs)
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] : {},
B[p],
@danielbeardsley
danielbeardsley / acts_as_sequential.rb
Created April 4, 2011 23:10
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.
# 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:
# Model.next(current_record) -> next record in sequence
# Model.other_named_scope.previous(current_record) -> previous record in sequence
#
@danielbeardsley
danielbeardsley / node-gzip.js
Created May 6, 2011 16:18
Simple nodejs interface to the gzip utility found on every *nix system
/* 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
danielbeardsley / html_template.js
Created June 22, 2011 20:26
A template renderer for express.js that processes plain vanilla HTML and has the option to strip newlines.
// 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
danielbeardsley / compose_connect_middlware.js
Created June 22, 2011 20:35
Allows easy composition of a chain of connect.js-style middlewares
// 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
danielbeardsley / grepReplace
Created September 26, 2011 23:29
Shell Script for grep-based search and replace recursively in files
#!/bin/sh
SEARCH=$1
REPLACE=$2
function ConfirmOrExit() {
echo -n "Continue? (y / n) :"
read CONFIRM
if [ $CONFIRM != 'y' ]
then
exit