Skip to content

Instantly share code, notes, and snippets.

@lwille
lwille / application_helper.rb
Created March 4, 2010 22:46
Single DB query tree generation with awesome_nested_set
module ApplicationHelper
# Mixin for tree generation based on a nested set, just place this in your application_helper.rb
module TreeMethods
attr_accessor :child_nodes
attr_accessor :parent_node
def after_initialize
@child_nodes = []
@parent_node = nil
end
def recursive_tree nodes=nil, level=0
# This relates to the issue described at:
# https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/4709
class String
def %(args)
if args.kind_of?(Hash)
dup.gsub(INTERPOLATION_PATTERN_WITH_ESCAPE) do |match|
if match == '%%'
'%'
else
class Object
def if_present?(&blk)
return self unless present?
if blk.arity == -1
instance_eval(&blk)
else
blk.call(self)
end
self
@mattmccray
mattmccray / render.js
Created November 1, 2010 19:44
Based on ICanHaz, this supports both Mustache.js and Handlebars.js
// Based on ICanHaz (http://github.com/andyet/ICanHaz.js/blob/master/ICanHaz.js)
// This version supports Mustache and Handlebars
// By M@ McCray
;var render = (function($, engine){
var cache = {},
methods = {};
$(function(){
$('script[type="text/html"]').each(function () {
var name = $(this).attr('id'),
@fnichol
fnichol / README.md
Created March 12, 2011 20:52
Download a cacert.pem for RailsInstaller

Why?

There is a long standing issue in Ruby where the net/http library by default does not check the validity of an SSL certificate during a TLS handshake. Rather than deal with the underlying problem (a missing certificate authority, a self-signed certificate, etc.) one tends to see bad hacks everywhere. This can lead to problems down the road.

From what I can see the OpenSSL library that Rails Installer delivers has no certificate authorities defined. So, let's go fetch some from the curl website. And since this is for ruby, why don't we download and install the file with a ruby script?

Installation

The Ruby Way! (Fun)

# Extend jQuery objects with Underscore collection methods.
#
# Each collection method comes in two flavors: one prefixed
# with _, which yields a bare DOM element, and one prefixed
# with $, which yields a jQuery-wrapped element.
#
# So if `this` is a jQuery object, instead of:
#
# _.max @, (el) -> $(el).height()
#
@mattbaker
mattbaker / object.prototype.js
Created June 14, 2011 18:54
Object.tap in Javascript
Object.prototype.tap = function(f){f.apply(this); return this;}
var x = {a:2};
x.tap(function(){console.log(this.a)}).a = 4; //Prints 2
console.assert(x.a == 4);
@ryanb
ryanb / issues_with_modules.md
Created November 29, 2012 22:38
Points on how modules can make code difficult to read.

My issues with Modules

In researching topics for RailsCasts I often read code in Rails and other gems. This is a great exercise to do. Not only will you pick up some coding tips, but it can help you better understand what makes code readable.

A common practice to organize code in gems is to divide it into modules. When this is done extensively I find it becomes very difficult to read. Before I explain further, a quick detour on instance_eval.

You can find instance_eval used in many DSLs: from routes to state machines. Here's an example from Thinking Sphinx.

class Article < ActiveRecord::Base
@shirosaki
shirosaki / 0-recipe.md
Created December 4, 2012 01:30
Build 1.9.3-p327 with falcon patch on Windows
# clone latest rubyinstaller from github
$ git clone https://github.com/oneclick/rubyinstaller.git
$ cd rubyinstaller

# download patches into resources\patches\ruby193 directory
$ mkdir resources\patches\ruby193
$ cd resources\patches\ruby193
$ curl -o 1-falcon.patch https://raw.github.com/gist/4136373/falcon.diff
$ curl -O https://raw.github.com/gist/4199712/2-fix-file-expand_path-globing.patch
@rwjblue
rwjblue / readme.md
Last active May 13, 2016 18:10
Guide to using drip with JRuby

#Overview drip is an awesome command line tool that can be used to dramatically lower perceived JVM startup time. It does this by preloading an entirely new JVM process\instance and allowing you to simply use the preloaded environment. This has extraordinary results with jruby.

We reduced time to run rake environment from 13 seconds to a mere 3.5 seconds. This is actually at or near MRI 1.9.3p327 (with falcon patch) speeds!

Adding a few addition jruby options will reduce startup time even further (down to 1.69 seconds).

#Install Drip Install drip if you haven't already (see https://github.com/flatland/drip)