Skip to content

Instantly share code, notes, and snippets.

View neilmiddleton's full-sized avatar
🏠
Working from home

Neil Middleton neilmiddleton

🏠
Working from home
View GitHub Profile
@neilmiddleton
neilmiddleton / gist:2827467
Created May 29, 2012 11:01
constant_macros
module ConstantMacros
def with_constants(constants, &block)
saved_constants = {}
constants.each do |constant, val|
saved_constants[ constant ] = Object.const_get( constant )
Kernel::silence_warnings { Object.const_set( constant, val ) }
end
begin
@neilmiddleton
neilmiddleton / gist:2598073
Created May 4, 2012 22:17
Python unique followers count calc
import sys
import tweepy
CONSUMER_SECRET = 'xx'
CONSUMER_KEY = 'xx'
# Run this code first to get your auth keys from Twitter
# auth_url = auth.get_authorization_url()
#
# print 'Please authorise: ' + auth_url
@neilmiddleton
neilmiddleton / gist:1647768
Created January 20, 2012 15:03
Lead with Postgres
select months.month,
COALESCE(users.count,0) AS count,
lead(count,1) over (order by months) as prev_month,
(count - (lead(count,1) over (order by months)) ) as delta
from
(
SELECT generate_series(
date_trunc('year', min(created_at)),
now(),
'1 month'::interval
@neilmiddleton
neilmiddleton / gist:1331304
Created November 1, 2011 17:43
Adding custom assets to the precompile list
config.assets.precompile += ['admin.js']
@neilmiddleton
neilmiddleton / gist:1295384
Created October 18, 2011 13:11
Wildcard selectors in sass
To produce:
.blah[class*='span'] {
text-align: center;
}
use:
.blah
&[class*='span']
@neilmiddleton
neilmiddleton / gist:1226309
Created September 19, 2011 11:10
Silent Postgres
gem "silent-postgres"
@neilmiddleton
neilmiddleton / gist:1226307
Created September 19, 2011 11:10
Postgres min_messages
development:
adapter: postgresql
host: localhost
min_messages: warning
database: foo_development
@neilmiddleton
neilmiddleton / gist:1089985
Created July 18, 2011 16:16
jQuery validation helpers
jQuery.validator.addMethod "min_age", (value, element) ->
date = value.split '-'
dob = new Date date[0], date[1], date[2]
_today = new Date()
_18_years_ago = new Date _today.getYear() - 18, _today.getMonth(), _today.getDay()
return dob < _18_years_ago
, "You must be over 18 years of age to use Nutmeg"
jQuery.validator.addMethod "max_age", (value, element) ->
date = value.split '-'
@neilmiddleton
neilmiddleton / gist:1053981
Created June 29, 2011 14:47
Failing RSpec test
AboutYou.any_instance.stub!(:save).and_return(false)
post :create
assigns[:about_you].should be_new_record
flash[:notice].should be_nil
response.should render_template('new')
def create
@about_you = current_user.build_about_you(params[:about_you])
@neilmiddleton
neilmiddleton / gist:966257
Created May 11, 2011 10:34
Create class from hash
def initialize(hash)
hash.each do |k,v|
self.instance_variable_set("@#{k.underscore}", v)
self.class.send(:define_method, k.underscore, proc{self.instance_variable_get("@#{k.underscore}")})
self.class.send(:define_method, "#{k.underscore}=", proc{|v| self.instance_variable_set("@#{k.underscore}", v)})
end
end