Skip to content

Instantly share code, notes, and snippets.

@julianlconnor
julianlconnor / scratch.rb
Last active May 6, 2016 17:49
postgresql order by field(id)
# ensures that your models return with the same ordering as ids
# e.g., [1,2,3] yields [<YourModel id=1>, <YourModel id=2>, <YourModel id=3>]
# via: https://gist.github.com/cpjolicoeur/3590737, brilliant solution
values = ids.each_with_index.map { |id, index| "(#{id}, #{index + 1})" }.join(",")
YourModel.unscoped.where(id: ids).joins("join (VALUES #{values}) as x(id, ordering) ON listings.id = x.id").order("x.ordering")
@julianlconnor
julianlconnor / rack_attack.rb
Last active April 21, 2016 16:02
rack attack, whitelist cloudflare
class Rack::Attack
CLOUDFLARE_SUBNETS = [
IPAddr.new("103.21.244.0/22"),
IPAddr.new("103.22.200.0/22"),
IPAddr.new("103.31.4.0/22"),
IPAddr.new("104.16.0.0/12"),
IPAddr.new("108.162.192.0/18"),
IPAddr.new("131.0.72.0/22"),
IPAddr.new("141.101.64.0/18"),
@julianlconnor
julianlconnor / user.rb
Last active April 20, 2016 15:59
Heroku, CloudFlare, Devise, Trackable
# NOTE: need to override Devise's Trackable `update_tracked_fields` when using CloudFlare & Heroku.
def update_tracked_fields(request)
super
# NOTE: need to pull the first IP out of a list of IPs since Heroku
# will append CloudFlare's IP.
old_current = self.current_sign_in_ip
new_current = if request.headers["X-Forwarded-For"].present?
request.headers["X-Forwarded-For"][/[0-9\.]+/]
else
@julianlconnor
julianlconnor / user.js
Last active August 29, 2015 14:19
example user class
function User(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
User.prototype.fullName = function() {
return this.firstName + ' ' + this.lastName;
}
userObjects.each(function(user) {

Keybase proof

I hereby claim:

  • I am muffs on github.
  • I am muffs (https://keybase.io/muffs) on keybase.
  • I have a public key whose fingerprint is CC12 CABA 54A4 E437 B21D CB4E A1CA 4E4D 7C49 EE2B

To claim this, I am signing this object:

@julianlconnor
julianlconnor / testable.js
Last active August 29, 2015 14:01
Testable React CSS Transition Group
/** @jsx React.DOM */
define([
'react',
], function(React) {
var ReactCSSTransitionGroup = React.addons.CSSTransitionGroup;
var TestableCSSTransitionGroup = React.createClass({
@julianlconnor
julianlconnor / eg.js
Last active August 29, 2015 14:00
findRenderedComponentWithType example
var ExampleComponent = React.createClass({
getInitialState: function() {
return {
shouldDisplayElement: true
}
},
toggle: function() {
@julianlconnor
julianlconnor / route.js
Created April 24, 2014 19:56
hydrating route proto
_hydrate: function() {
var args = arguments;
var keys = _.keys(this.requires);
var values = _.values(this.requires);
/**
* Create a list of promises.
*/
var promises = values.map(function(fn) {
return fn.apply(this, args);
#!/usr/bin/env node
var q = require('q');
var exec = require('child_process').exec;
var exit = process.exit;
var fs = require('fs');
var readline = require('readline');
var grep = function(pattern) {
var dfd = q.defer();
q.all([only(), debug()])
.then(flatten)
.then(function(lines) {
var dfd = q.defer();
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question(lines[0] + ' -- is this cool? (n)o , (y)es >', function(answer) {