Skip to content

Instantly share code, notes, and snippets.

View dfurber's full-sized avatar

David Furber dfurber

  • TrueCar Inc
  • Ithaca, NY
View GitHub Profile
@dfurber
dfurber / a_turbolinks_spinner.md
Last active January 30, 2018 07:32
Add a loading indicator to turbolinks so the user knows when pages are loading.
<?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 January 3, 2016 11:49
Radius Search in Yii. Include WhereBehavior and RadiusSearchBehavior in your model in order to search byPointRadius or insideBounds.
<?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 / ruby_tail_recursion.rb
Last active November 24, 2017 21:39
Experiments in tail recursion with Ruby, implementing group_by, index_by, in_groups_of, and sort_by without iteration or conditionals. Because why not?
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|
@dfurber
dfurber / union_query.rb
Created April 20, 2012 03:07
Union Query in AR with Lazy Evaluation
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
@dfurber
dfurber / union_queries.rb
Created April 19, 2012 18:20
Union Query in AR
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 June 13, 2011 20:13
CoffeeScript version of jQuery DOM Shortcut
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 June 13, 2011 20:05
Javascript Template Rendering Engine
# 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')
# 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 / exception.php
Created April 11, 2011 19:39
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
<!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[*/