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
strings = %w[aa áb ac na ña ne oa]
# Provide alphabetical sort in Spanish, where accented vowels are sorted
# the same as unaccented vowels, and "ñ" is sorted directly after "n".
#
# This is accomplished by normalizing the string to Unicode decomposed form
# to break characters with diacritics into multiple characters, and then
# removing acute accents and diaresis: e.g. "á" becomes "a", "ü" becomes
# "u". These are the only two diacritics used for vowels in Spanish.
#
@norman
norman / fixtures_spec.rb
Created October 13, 2021 13:59
Test the validity of your Rails fixtures
# frozen_string_literal: true
require "rails_helper"
describe "Fixtures" do
fixture_table_names.each do |table_name|
model_class = table_name.singularize.classify.constantize
it "has valid #{table_name}" do
expect(model_class.all).to all be_valid
end
@norman
norman / keybase.md
Created March 22, 2018 13:08
keybase.md

Keybase proof

I hereby claim:

  • I am norman on github.
  • I am compay (https://keybase.io/compay) on keybase.
  • I have a public key whose fingerprint is 1A08 8ED4 32C3 612F 5FF5 677E 74BA BFD3 3159 0D75

To claim this, I am signing this object:

@norman
norman / etag.rb
Last active May 31, 2016 11:39
One way to make Rails consider the layout in an etag.
# By default, changes to the layout temnplate won't cause a Rails etag
# to change. I suppose this is to facilitate Turbolinks. Sometimes you
# might want to change an etag in response to changes in the layout -
# this is particularly true when working in development mode. This is
# one way to do it, there are probably other (read: likely better) ways
# to do this too.
class FooController < ApplicationController
# There's probably some API method to get the name of the layout
# rather than hard-coding it like I did here, but I'm too lazy to
# look it up right now.
### Keybase proof
I hereby claim:
* I am norman on github.
* I am compay (https://keybase.io/compay) on keybase.
* I have a public key whose fingerprint is 3059 0FBA AFDE E001 D557 C63A 298B 0120 ED7D 0E39
To claim this, I am signing this object:
@norman
norman / pg.txt
Last active August 29, 2015 14:04
Optimal development-only Postgres settings for my Macbook Pro with 8GB RAM.
#------------------------------------------------------------------------------
# CUSTOMIZED OPTIONS
#------------------------------------------------------------------------------
# Add settings for extensions here
#
# Some stupid, dangerous settings only used to speed things up on a local
# development machine, where if data is lost I'll just wipe and recreate.
# Never ever ever use these in production or anything approximating
# production.
According to my records your subscription will expire on 14 June 2014
LostCousins subscriptions are NOT renewed automatically unlike subscriptions for most other
genealogy sites - I prefer to depend on your generosity, not your forgetfulness.
The good news is that if you renew your subscription NOW it won't start until your existing
subscription expires - you'll get a full 12 months from the date shown above (so you don't have
to leave it until the last minute). Even better news is that the price hasn't changed since 2005 -
it's still just 10 pounds for a single subscription!
@norman
norman / gist:8300884
Created January 7, 2014 15:22
Benchmark comparing Haml 4.0.5 to Haml 4.1.0.beta.1

Haml 4.0.5

ruby 2.0.0p195 (2013-05-14 revision 40734) [x86_64-darwin12.4.0]
------------------------------------------------------------------------

1000 Iterations
Rehearsal --------------------------------------------------------
compiled haml pretty   0.550000   0.000000   0.550000 (  0.551526)
compiled haml ugly     0.270000   0.000000   0.270000 (  0.270457)

FriendlyId uses Rails's extending method to avoid overwriting your model's find method. Internally this is implemented similar to this:

def self.friendly
  all.extending(friendly_id_config.finder_methods)
end

This however comes with a performance impact, because extending invokes Ruby's extend, which blows away MRI's method cache. To work around this, FriendlyId lets you include a :finders module which overrides your model's find to increase performance.

@norman
norman / factories.rb
Last active December 22, 2015 07:39
A very minimal alternative to FactoryGirl?
module FlexMinder
module Factories
def factory(klass, attributes)
name = klass.to_s
underscored = name.underscore
class_eval(<<-END, __FILE__, __LINE__ + 1)
def build_#{underscored}(attributes = {})
attributes = valid_#{underscored}_attributes.deep_merge(attributes)
#{name}.new(attributes)