Skip to content

Instantly share code, notes, and snippets.

View norman's full-sized avatar
🪕
Scruggs not drugs

Norman Clarke norman

🪕
Scruggs not drugs
View GitHub Profile
@norman
norman / asset_precompile.rb
Created March 26, 2012 18:59
A brutal hack to allow Rails 3.2.2's asset pipeline to precompile files with only 1 extension (like foo.less, bar.sass, etc.).
# A brutal hack to allow Rails 3.2.2's asset pipeline to precompile files with
# only 1 extension (like foo.less, bar.sass, etc.). The problem is when you use
# `@import` to keep your code reasonably DRY, and rely on variables declared in
# an @imported file. Sprockets, rather than skipping these files and only including
# then when they are @imported, will still try to compile them, and fails when it
# hits an undefined variable.
module Sprockets
class StaticCompiler
def compile
manifest = {}
@norman
norman / light.diff
Created March 9, 2012 14:26
Low Fat Rails.
diff --git a/Gemfile b/Gemfile
index 7764763..dec33fd 100644
--- a/Gemfile
+++ b/Gemfile
@@ -1,26 +1,26 @@
source 'https://rubygems.org'
-gem 'rails', '3.2.2'
+# gem 'rails', '3.2.2'
@norman
norman / bench.txt
Created March 7, 2012 17:59
Performance difference before geocoder gem's default SQL and optimized Postgres-specific SQL on a database with 2 million records.
Geocoder ActiveRecord:
0.020000 0.000000 0.020000 ( 7.083117)
Geocoder Postgres:
0.010000 0.000000 0.010000 ( 0.044410)
# coding: utf-8
require "spanish"
class String
def rhymes_with?(string)
mine = Spanish.get_syllables(to_s).last.rime.map(&:to_s)
yours = Spanish.get_syllables(string).last.rime.map(&:to_s)
mine == yours
end
# coding: utf-8
require "spanish"
class String
def rhymes_with?(string)
mine = Spanish.get_syllables(to_s).last.rime.map(&:to_s)
yours = Spanish.get_syllables(string).last.rime.map(&:to_s)
mine == yours
end
=begin
As far as we can tell, Active Record doesn't currently let you add an ORDER
at the end of all all ORDER clauses.
This isn't usually a problem since you can just concatenate ORDERs in the, well,
order you want.
But in the case of default scopes, for example, it's sometimes useful to force
an ORDER to always come last.
class Module
def self.included(base)
raise "FAIL"
end
def self.extended(base)
raise "FAIL"
end
end
@norman
norman / area_codes.rb
Created January 13, 2012 19:57
This is what I have to do just to get a respectable database of Argentina area codes.
# coding: utf-8
# Before running this, make sure you run the downloader script to get all the
# HTML files from www.cnc.gov.ar. Then, download and extract AR.txt from
# Geonames at http://download.geonames.org/export/dump/AR.zip
require "utf8_utils"
require "unicode"
$pattern = /center">([\d]*)<\/d.*\n.*ss="txtTabla">(.*)<\/s.*\n.*Tabla">(.*)<\/s.*\n/
@norman
norman / earthdistance.rb
Last active November 3, 2022 21:20
Geographic Searches With Postgres's Earthdistance and Cube Extensions
#!/usr/bin/env ruby
=begin
= Geographic Searches With Postgres's Earthdistance and Cube Extensions
This program shows how to easily create a Postgres database that uses the Cube
and Earthdistance extensions to perform fast queries on geographic data.
Briefly, the problem this code solves is "show me all places within 50
kilometers of New York City."
@norman
norman / benchmark.txt
Created December 20, 2011 14:52
FriendlyId 4.0.0.rc2 on Rails 3.1 vs 3.2
$ BUNDLE_GEMFILE=gemfiles/Gemfile.rails-3.1.rb bundle exec rake bench
------------------------------------------------------------------------
Using ruby 1.9.3 AR 3.1.3 with sqlite3 (in-memory)
Rehearsal ---------------------------------------------------------------
find (without FriendlyId) 0.380000 0.000000 0.380000 ( 0.394020)
find (in-table slug) 0.450000 0.010000 0.460000 ( 0.473896)
find (external slug) 0.940000 0.000000 0.940000 ( 0.971482)
insert (without FriendlyId) 0.860000 0.010000 0.870000 ( 0.916956)
insert (in-table-slug) 2.630000 0.070000 2.700000 ( 2.769267)
insert (external slug) 4.600000 0.080000 4.680000 ( 4.690644)