Skip to content

Instantly share code, notes, and snippets.

Avatar

David Furber dfurber

  • TrueCar Inc
  • Ithaca, NY
View GitHub Profile
View coffee_script_tricks.coffee
# These are my notes from the PragProg book on CoffeeScript of things that either
# aren't in the main CS language reference or I didn't pick them up there. I wrote
# them down before I forgot, and put it here for others but mainly as a reference for
# myself.
# assign arguments in constructor to properties of the same name:
class Thingie
constructor: (@name, @url) ->
# is the same as:
@dfurber
dfurber / a_turbolinks_spinner.md
Last active Jan 30, 2018
Add a loading indicator to turbolinks so the user knows when pages are loading.
View a_turbolinks_spinner.md
@dfurber
dfurber / ruby_tail_recursion.rb
Last active Nov 24, 2017
Experiments in tail recursion with Ruby, implementing group_by, index_by, in_groups_of, and sort_by without iteration or conditionals. Because why not?
View ruby_tail_recursion.rb
items = %w{foo bar baz goo gar gaz hoo har haz}
def recurse_array(ary, &block)
head, *tail = ary
head and block.call(head)
tail.any? and recurse_array(tail, &block)
end
puts "First the items in order:\n"
recurse_array items do |item|
View ActiveRecord_createCommand.php
<?php
class ActiveRecord extends CActiveRecord
{
/**
* Create and configure a CDbCommand instance based on current criteria.
*
* Think twice before using this method - use only in cases where being able to stream
* through the raw SQL record-sets is crucial, usually for performance reasons, when
* dealing with very large record sets.
@dfurber
dfurber / RadiusSearchBehavior.php
Last active Jan 3, 2016
Radius Search in Yii. Include WhereBehavior and RadiusSearchBehavior in your model in order to search byPointRadius or insideBounds.
View RadiusSearchBehavior.php
<?php
class RadiusSearchBehavior extends CActiveRecordBehavior
{
public function byPointRadius($lat, $lng, $radius)
{
$radius = (float)$radius;
$distance = "((ACOS(SIN(:lat * PI() / 180) * SIN(lat * PI() / 180) + COS(:lat * PI() / 180) * COS(lat * PI() / 180) * COS((:lng - lng) * PI() / 180)) * 180 / PI()) * 60 * 1.1515)";
$condition = "lat BETWEEN " . ($lat - $radius / 69.2) . " AND " . ($lat + $radius / 69.2) . " AND lng BETWEEN " . ($lng - $radius / 69.2) . " AND " . ($lng + $radius / 69.2) . " AND $distance<=:radius";
return $this->owner->where($condition, array('lat' => $lat, 'lng' => $lng, 'radius' => $radius));
@dfurber
dfurber / union_query.rb
Created Apr 20, 2012
Union Query in AR with Lazy Evaluation
View union_query.rb
module UnionQuery
extend ActiveSupport::Concern
included do
def self.union(relation_to_unite, type=nil)
LazyUnionQuery.new self, relation_to_unite, type
end
end
end
View union_queries.rb
module UnionQuery
extend ActiveSupport::Concern
included do
def self.union(query1, query2, params={}, type='all')
find_by_sql "#{query1.to_sql} UNION #{type} #{query2.to_sql}", params
end
def self.unite_with(query2)
@dfurber
dfurber / builder.coffee
Created Jun 13, 2011
CoffeeScript version of jQuery DOM Shortcut
View builder.coffee
do ->
setOptions = (el, attributes) ->
for own key, value of attributes
el.setAttribute key, value
el
_(['ol','ul','li','span','strong','p','h1','h2','h3','h4']).each (tag) ->
window["_#{tag}"] = (html, attributes, children...) ->
el = document.createElement tag
if attributes?
@dfurber
dfurber / templates.coffee
Created Jun 13, 2011
Javascript Template Rendering Engine
View templates.coffee
# Template rendering in JS
# Combines John Resig's simple js templates with Sproutcore's content binding concept.
# Define your templates in the templates array (or rewrite it to work however you like!)
#
# To render a template to an html string, call:
# render('example', dataObject)
# OR
# render('example', arrayOfDataObjects)
# OR if you have template that isn't bound to a data object:
# render('static_example')
@dfurber
dfurber / exception.php
Created Apr 11, 2011
If you use TextMate on your PHP project using the Yii application framework, place this file in protected/views/system. Then your stack trace will have clickable links to the code in question. Also it will summarize the $_REQUEST and $_SESSION variables a
View exception.php
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title><?php echo CHtml::encode($data['type'])?></title>
<style type="text/css">
/*<![CDATA[*/